⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 维吉尼亚密码应用系统matlab程序.m

📁 维吉尼亚密码应用系统MATLAB程序。基于MATLAB编写
💻 M
📖 第 1 页 / 共 2 页
字号:

if TextNumber ~= length( TextString )

    error('暴力破解密钥(DecodeKeyDirectly)出错,the length must be the same')
end


global StateNum ;
EnableTap = 2 ;


% init the color of the figure when StateNum == 3
FigureColor3 = [0.6 0.9 0.8] ;

if StateNum ~= 3
   
    EnablePushbuttonPosition = get(PushbuttonHandle(3),'position') ;
    
    EnablePushbuttonPosition(4) =  ...
        EnablePushbuttonPosition(4) + EnableTap ;
    
    AvildColor = get(PushbuttonHandle(3),'backgroundcolor') ;
    
    EnableColor = AvildColor * 2/3 ;
    
    set(PushbuttonHandle(3),...
        'backgroundcolor',EnableColor,...
        'position',EnablePushbuttonPosition ) ;

    set(FigureHandle ,'color',FigureColor3 ) ;

    for num = 1:2:TextNumber
        
        set(TextHandle(num),'backgroundcolor',FigureColor3) ;
    end

    for num = 1:TextNumber
        
        set(TextHandle(num),'string',TextString(num) ) ;
    end

    TempPushbuttonPosition = get(PushbuttonHandle(StateNum),'position') ;
    TempPushbuttonPosition(4) = TempPushbuttonPosition(4) - EnableTap ;
    
    set(PushbuttonHandle(StateNum),...
        'backgroundcolor',AvildColor,...
        'position',TempPushbuttonPosition ) ;

end

StateNum = 3 ;


%-------------------------------------------------------------------------
function CloseFun( FigureHandle )
close;
%退出


%-------------------------------------------------------------------------
function VigenereRun( FigureHandle)
% this is a function for calculate that...
% order ot the selection of StateNumfrom 1,2,3

%get the the handle of five pushbuttons and six texts.

PushbuttonHandle = getappdata( FigureHandle,'PushbuttonHandle') ;

TextHandle = getappdata( FigureHandle,'TextHandle') ;

global StateNum ;

String2 = get( TextHandle(2),'string') ;
String4 = get( TextHandle(4),'string') ;
String2 = char( String2 ) ;
String4 = char( String4 ) ;
String2Indx = find( String2 == 32 ) ;
String4Indx = find( String4 == 32 ) ;
String2( String2Indx ) = [];
String4( String4Indx ) = [];

String6 = '运算失败,未得出结果' ;


if isempty( String2 )
    
    if StateNum == 3
        
        feval('Vigenere',...
            'ErrorFun',...
            '你还没有输入密文,请输入密文 !','维吉尼亚密码应用系统出错提示')
    else

        feval('Vigenere',...
            'ErrorFun',...
            '你还没有输入密钥,请输入密钥 !','维吉尼亚密码应用系统出错提示')
    end

elseif isempty( String4 )
    
    if StateNum == 2   
        
        feval('Vigenere','ErrorFun','你还没有输入密文,请输入密文 !')
    else

        feval('Vigenere','ErrorFun','你还没有输入明文,请输入明文 !')
    end
  
else

    switch StateNum
        case 1,            
            String6 = feval( 'Vigenere',...
                'PlainCode2ConfCodeFun',String2,String4 ) ;
        
        case 2,
            String6 = feval( 'Vigenere',...
                'ConfCode2PlainCodeFun',String2,String4 ) ;
       
        case 3,
           
            if length( String2 )~=length( String4 )
                
                feval('Vigenere',...
                    'ErrorFun','密文与明文长度必须相同,请重新输入 !')  ;
            else

                String6 = feval( 'Vigenere',...
                    'DecodeKeyDirectlyFun',String2,String4 ) ;
            end

    end

set(TextHandle(6),'string',String6,'enable','on',...
    'foregroundcolor',[0.8 0.2 0.2]) ;

end



%-------------------------------------------------------------------------
function ConfCode = PlainCode2ConfCodeFun( Key,PlainCode )

%vigenere  加  密
%用密钥Key对明文PlainCode加密,结果密文为ConfCode
%不对空格处理,不处理大小写.

KeyLen = length(Key) ;

PlainCodeLen = length(PlainCode) ;

KeyMat = [repmat(Key,1,floor(PlainCodeLen/KeyLen)),...
    Key(1:mod(PlainCodeLen,KeyLen))] ;

ConfCode =char( mod( (PlainCode + KeyMat -2 *'a'),26)+'a') ; %密文
   
%display('结果密文为:');   %display(ConfCode);


%-------------------------------------------------------------------------
function PlainCode = ConfCode2PlainCodeFun( Key,ConfCode )

%vigenere  解  密
%用密钥KEY对密文CONFCODE进行解密,结果明文为:PlainCode
%不对空格处理,不处理大小写.

KeyLen = length(Key) ;

ConfCodeLen = length(ConfCode) ;

KeyMat = [repmat(Key,1,floor(ConfCodeLen/KeyLen)),...
    Key(1:mod(ConfCodeLen,KeyLen))] ;

PlainCode = char( mod( (ConfCode - KeyMat),26)+'a' ) ;   %明文
  
%display('结果明文为:');   %display(PlainCode);


%-------------------------------------------------------------------------
function Key = DecodeKeyDirectlyFun( ConfCode ,PlainCode )

%vigenere  破  译
%用暴力破解法破译密钥
% ConfCode ='cbihgbdmvprjcbupzv';   %密文
% PlainCode ='attackbeginsatfive';   %明文
% ConfCode = 'zgtpbamdegavjechrdhsyufpgamzyghfzfhzlphcypolfvyyoiz';
% PlainCode = 'scientiststrytoanswerquestionsabouttheworldaroundus';

Number = 20 ;
count = 0 ;
CountReq = 5 ;

KeyMat = char(mod((ConfCode - PlainCode),26) + 'a' );
KeyMatLen = length( KeyMat ) ;

if KeyMatLen/2 <Number

    Number = KeyMatLen/2 ;
end

for num = 1:Number

    for index = 1:num:(KeyMatLen - num)

        if KeyMat(index:(index +num-1)) ...
                == KeyMat((index+num):(index+2*num-1))

            count = count +1;

            if count == CountReq

                Key = KeyMat(1:num) ;
                break;
            end

        else

            break ;
        end

        LastLen = KeyMatLen - (index-1) ;

        if LastLen <=  3*num

            if KeyMat( (index+num):end ) ...
                    == KeyMat(1:(LastLen-num))

                Key = KeyMat(1:num);
                Number = 0;
                break;

            end

        end

    end

    if Number == 0 |count == CountReq

        break;

    end

end

if Number == 0|count == CountReq

    Key = strcat(KeyMat,'(',Key,')') ;
else

    Key = KeyMat ;
end

%display('密钥为:');   %display(Key) ;


%=------------------------------------------------------------------------
function varargout = ErrorFun( varargin )

%出错提示窗口

% define the default value of the title and message,
% or get the value of the title and message.
if nargin < 2
    
    title = '维吉尼亚密码应用系统出错提示' ;
    
    if nargin < 1
       
        message = '注意:你执行的是非法操作' ;
    else

        message = varargin{1} ;
    end
    
else

    message = varargin{1} ;
    title = varargin{2} ;

end

message = ['[_',message,'_]'] ;


%init the value of the color Property of figure
FigureColor = [ 0.6333    0.8333    0.8333 ] ;

TabSpace = 10 ;

% init the parameter of the position of errorfigure .

ErrorFigureWidth = 320 ;
ErrorFigureHeight = 100 ;


% creat the errorfigure .
ErrorFigureHandle = figure('Units' , 'pixels',...
    'Position', [0 0 ErrorFigureWidth ErrorFigureHeight], ...
    'Name', title, 'Tag', 'ErrorFigure', ...
    'Resize', 'off', 'windowstyle', 'modal',...
    'numbertitle','off','MenuBar','none',...
     'color',FigureColor,'Visible', 'off') ;


 % init the parameter of the position of message text

MessageXPos = 2 *TabSpace ;
MessageYPos = 2*TabSpace ;
MessageWidth = 200 ;
MessageHeight =  ErrorFigureHeight - 2* MessageYPos ;

MessagePosition = [MessageXPos MessageYPos MessageWidth MessageHeight] ;

% creat the message text
MessageTextHandle = uicontrol(...
    'parent' ,ErrorFigureHandle,...
    'style','text','string',message,...
    'HorizontalAlignment','left',...
    'fontsize',12,...
    'fontname','宋体',...
    'backgroundcolor',FigureColor,...
    'position',MessagePosition );


% init the parameter of the position of pushbutton
PushbuttonXPos = MessageXPos + MessageWidth +TabSpace ;
PushbuttonYPos = MessageYPos + 2 ;
PushbuttonWidth = ErrorFigureWidth - TabSpace - PushbuttonXPos ;
PushbuttonHeight = MessageHeight - 4-20;

PushbuttonPosition = ...
    [PushbuttonXPos,PushbuttonYPos,PushbuttonWidth,PushbuttonHeight] ;



% creat the pushbutton 
PushbuttonHandle = uicontrol(...
    'parent',ErrorFigureHandle,...
    'style','pushbutton','string','确  定', ...
    'HorizontalAlignment','center','fontsize',14,...
    'fontname','宋体',...
    'position',PushbuttonPosition,...
    'callback','close') ;


movegui(ErrorFigureHandle,'center') ;
set(ErrorFigureHandle ,'visible','on') ;
 

if nargout > 0
    
    varargout{1} = ErrorFigureHandle ;
end



%-----------------------------------------
function HelpFun

%
%有待完善,在右上角加一个帮助
%
%想在界面右上角,在关闭按钮的前面加上一个帮助按钮,功能是在用户按下之后
%出现一个帮助界面,以显示下面这些帮助信息。
%
%可是我还不知道怎么来创建这样的按钮。所以下次再来完善了,今天是星期一了,
%要上课了,我写这个界面花了我整整两天的时间,还有作业还没做呢。
% 
%还望大家多多指教,提出改进意见。不断完善,追求完美是我们共同的目标。
%



%---
%---    **************   Vigenere(维吉尼亚)密码原理简介    **************
%---    
%---        Vigenere密码是16世纪法国著名密码学家Blaise de Vigenere 于1568
%---    年发明的,它是最著名的多表代替密码的例子。Vigenere密码使用一个词组作
%---    为密钥,密钥中每一个字母用来确定一个代替表,每个密钥字母被用来加密一
%---    个明文字母,第一个密钥字母加密明文的第一个字母,第二个密钥字母加密明
%---    文的第二个字母,等所有密钥字母使用完后,密钥又再循环使用。Vigenere密
%---    码算法如下:
%---    
%---    设  密钥 : K=(k1,k2,... ,km),
%---        明文 : P=(p1,p2,... , pn),
%---        密文 : C=(c1,c2,... , cr),
%---    
%---    加密变换为:
%---            ci=fi(pi)=pi+ki(mod26)
%---    
%---    解密变换为:
%---            pi=(fi_1)(ci)=ci -ki(mod26)
%---    
%---    例如:
%---    用cipher对明文attcak begins at five进行加密
%---    
%---              attcak begins at five    明文
%---         +    cipher cipher ci pher    密钥
%---       -----------------------------------
%---             =cbihgb dmvprj cb upzv    密文
%---     去除空格后为:cbihgbdmvprjcbupzv  
%---    


%---    *************   Vigenere(维吉尼亚)密码系统使用说明   ***************
%---    
%---    这是一个维吉尼亚密码应用系统,
%---    有三部功能包括:加密,解密和暴力破解密钥.
%---    
%---    界面的上面一行有四个按钮,其对应的功能分别为:加密,解密,暴力破解密
%---    钥和退出。
%---    
%---    1、加密:在对应的编辑框中输入密钥与明文,按运行按钮则得到密文;
%---    
%---    2、解密:在对应的编辑框中输入密钥与密文,按运行按钮则得到明文;
%---    
%---    3、暴力破解密钥:密文与明文的长度必须相同,否则出错,会提示重新输入,
%---    
%---                    在对应的编辑框中输入密文与明文,按运行按钮则得到密文;
%---    
%---    4、退出:退出系统。
%---    
%---    5、右边还有一运行按钮,按下就会执行的要求的计算。若你的输入正确则得到
%---       结果。
%---    



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -