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

📄 vigenere_system.txt

📁 用matlab实现vigenere密码的加密解密和暴力解密
💻 TXT
📖 第 1 页 / 共 2 页
字号:
function varargout = Vigenere( varargin )

%这是一个维吉尼亚密码应用系统,
%有三部功能包括:加密,解密和暴力破解密钥.


% init the state for working
% when StateNum == 1 for 加密
% when StateNum == 2 for 解密
% when stateNum == 3 for 暴力破解密钥

global StateNum ;


if nargin == 0
    
    % find out the figure handle .
    % and close the same figure haved aleady opened
    OldFigure = findobj(0, 'type', 'figure',...
        'name', '维吉尼亚密码应用系统(加密|解密|暴力破解)') ;
 
    close( OldFigure ) ;

    %DELETE(H) deletes the graphics object with handle H. If the object
    %is a window, ...
    %the window is closed and deleted without confirmation.(证实,证明)



    FigureHandle = figure('visible','off') ;

    GenerateFigureContent( FigureHandle ) ;

    InitFigureState ( FigureHandle ) ;

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

    if nargout > 0
        varargout{1} = FigureHandle ;
    end
    
elseif ischar(varargin{1})
    
    try
        [varargout{1:nargout}] = feval(varargin{:});
    catch
        disp(lasterr);
    end

end


%-------------------------------------------------------------------

function GenerateFigureContent( FigureHandle )
% generate the content for figure .

%define the distance for unit (basic,elemental)between the objects.
TabSpace = 10 ;

%define the code for number from 1 to 10.
TextNumber = 6 ; PushbuttonNumber = 4;

%init the color for working.
EnableColor = [0.0 0.6 0.0] ;

%init the parameter of the position of figure.
FigureXPos = 200 ;
FigureYPos = 100 ;
FigureWidth = 600 ;
FigureHeight = 400 ;

FigurePosition = [FigureXPos,FigureYPos,FigureWidth,FigureHeight] ;

%init the value of the color Property of figure
FigureColor = [0.6 0.9 0.9] ;

%init the parameter of the position of uicontrol.
PushbuttonWidth = ...
    ( FigureWidth - (2 *2*TabSpace +(PushbuttonNumber-1)*TabSpace ) )...
    /PushbuttonNumber ;

PushbuttonHeight = 40 ;
PushbuttonXPos = 2* TabSpace ;
PushbuttonYPos = FigureHeight - (4+2)* TabSpace ;

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

%init the value of some properties of pushbutton.
PushbuttonString = {'加     密','解     密','暴力破解密钥','退     出'} ;

PushbuttonCallback = ...
    {'feval(''Vigenere'',''PlainCode2ConfCode'',gcf)',...
    'feval(''Vigenere'',''ConfCode2PlainCode'',gcf)',...
    'feval(''Vigenere'',''DecodeKeyDirectly'',gcf)',...
    'feval(''Vigenere'',''CloseFun'',gcf)'} ;
            %% ConfCode is ConfidentialCode in brief (simple)

%init the parameter of the position of text.
TextHeight = 25 ;
TextXPos = 2* TabSpace ;
TextYPos = PushbuttonYPos - 2* TabSpace - TextHeight ;
TextWidth = FigureWidth - TextXPos -3* TabSpace - PushbuttonWidth ;

TextPosition = [TextXPos TextYPos TextWidth TextHeight] ;

%init the value of some properties of text.
TextString = {'请输入密钥:','','请输入明文:','','明文加密后得到密文为:',''} ;
TextStyle = {'text','edit','text','edit','text','edit'} ;
TextTabSpace = ...
    (TextYPos - TextHeight *(TextNumber -1) - 4 *TabSpace)/(TextNumber -1);


%set the value of figure's propaty and init.
set(FigureHandle,'toolbar','none',...
    'numbertitle','off',...
    'position',FigurePosition,...
    'MenuBar','none',...
    'Resize', 'off',...
    'windowstyle', 'modal',...
    'color',FigureColor,...
    'name','维吉尼亚密码应用系统(加密|解密|暴力破解)',...
    'visible','off');

%mark the following command ba active...
%when the figure color isn't the color defined by FigureColor
%get the value of the color property of the figure.
    %%FigureColor = get(FigureHandle,'color') ;


%creat four pushbuttons and generate the contect in them.
PushbuttonHandle = zeros(1,PushbuttonNumber) ;

for num = 1:PushbuttonNumber

    TempPushbuttonPosition = PushbuttonPosition ;

    TempPushbuttonPosition(1) = TempPushbuttonPosition(1) ...
        + ( num-1 ) * (PushbuttonWidth + TabSpace ) ;

    PushbuttonHandle(num) = uicontrol(...
        'parent',FigureHandle,...
        'string',PushbuttonString{num},...
        'style','pushbutton','fontsize',12,...
        'backgroundcolor','g','fontname','宋体',...
        'callback',PushbuttonCallback{num},...
        'position',TempPushbuttonPosition ) ;

end

%creat six text and generate the contect in them.
TextHandle = zeros(1,TextNumber) ;  %creat the varage TextHandle

EditColor = [.9 .9 .9] ;

TextColor = ...
    [ FigureColor;EditColor;FigureColor;EditColor;FigureColor;EditColor ] ;

for num = 1:TextNumber

    TempTextPosition = TextPosition ;

    TempTextPosition(4) = TempTextPosition(4) + 30 *mod(num-1,2) ;

    TempTextPosition(2) = ...
        TempTextPosition(2) - (num-1) *( TextTabSpace +TextHeight) ;

    TextHandle(num)=uicontrol('parent',FigureHandle,...
        'string',TextString{num},...
        'HorizontalAlignment','left','fontsize',12,...
        'style',TextStyle{num},'fontname','宋体',...
        'backgroundcolor',TextColor(num,:),...
        'position',TempTextPosition);
end

set(TextHandle(TextNumber),'enable','off') ;


%init the parameter of the position of the fifth pushbutton (run).
RunPushbuttonXPos = FigureWidth + 1* TabSpace - PushbuttonWidth ;
RunPushbuttonYPos = TempTextPosition(2) + TabSpace ;
RunPushbuttonWidth = PushbuttonWidth - 4* TabSpace ;
RunPushbuttonHeight = ...
    TextYPos - TextTabSpace - TabSpace -RunPushbuttonYPos ;

RunPushbuttonPosition = [RunPushbuttonXPos RunPushbuttonYPos ...
    RunPushbuttonWidth RunPushbuttonHeight ] ;

%creat the fifth  pushbutton (run).
PushbuttonHandle(PushbuttonNumber+1) = uicontrol(...
    'parent',FigureHandle,'string','!',...
    'style','pushbutton','fontsize',64,...
    'Horizontalalignment','center',...
    'fontname','幼圆','foregroundColor',[0.6 0.1 0.1],...
    'position',RunPushbuttonPosition,...
    'callback','feval(''Vigenere'',''VigenereRun'',gcf)') ;


% save all handles on FigureHandle.

setappdata(FigureHandle,'PushbuttonHandle',PushbuttonHandle) ;

setappdata(FigureHandle,'TextHandle',TextHandle) ;



%  handles = guihandles( FigureHandle )
%  guidata( FigureHandle,handles )

% feval('CloseFun') ;
% feval('PlainCode2ConfCode') ;
% feval('ConfCode2PlainCode') ;
% feval('DecodeKeyDirectly') ;


%-------------------------------------------------------------------------

function InitFigureState( FigureHandle )
%init the state for working

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

PushbuttonHandle = getappdata( FigureHandle,'PushbuttonHandle') ;

TextHandle = getappdata( FigureHandle,'TextHandle') ;

TextNumber = length( TextHandle ) ;

TextString = {'请输入密钥:','','请输入明文:','','明文加密后得到密文为:',''} ;


if TextNumber ~= length( TextString )
    error('加密(PlainCode2ConfCode)出错,the length must be the same')
end


% init the add height for working state.

EnableTap = 2 ;

global StateNum ;

EnablePushbuttonPosition = get(PushbuttonHandle(1),'position') ;

EnablePushbuttonPosition(4) =  EnablePushbuttonPosition(4) + EnableTap ;

AvildColor = get(PushbuttonHandle(1),'backgroundcolor') ;

EnableColor = AvildColor * 2/3 ;

set(PushbuttonHandle(1),'backgroundcolor',EnableColor,...
    'position',EnablePushbuttonPosition ) ;

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

StateNum = 1 ;




%--------------------------------------------------------------------------
%--------------------------------------------------------------------------


function PlainCode2ConfCode( FigureHandle )
%加密功能
%this is a function to translate the plain code to confidential code.

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

PushbuttonHandle = getappdata( FigureHandle,'PushbuttonHandle') ;

TextHandle = getappdata( FigureHandle,'TextHandle') ;

TextNumber = length( TextHandle ) ;

TextString = {'请输入密钥:','','请输入明文:','','明文加密后得到密文为:',''} ;


if TextNumber ~= length( TextString )
    
    error('加密(PlainCode2ConfCode)出错,the length must be the same')
end


% init the add height for working state.

EnableTap = 2 ;

global StateNum ;

% init the color of the figure when StateNum == 1
FigureColor1 = [0.6 0.85 0.9] ;


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

    set(FigureHandle ,'color',FigureColor1) ;
    
    for num = 1:2:TextNumber
        
        set(TextHandle(num),'backgroundcolor',FigureColor1) ;
    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 = 1 ;



%-------------------------------------------------------------------------


function ConfCode2PlainCode( FigureHandle )
%解密功能
%this is a function to translate the confidential code to plain code.

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

PushbuttonHandle = getappdata( FigureHandle,'PushbuttonHandle') ;

TextHandle = getappdata( FigureHandle,'TextHandle') ;

TextNumber = length( TextHandle ) ;

TextString = {'请输入密钥:','','请输入密文:','','明文加密后得到明文为:',''} ;


if TextNumber ~= length( TextString )

    error('解密功能(ConfCode2PlainCode)出错,the length must be the same')
end


global StateNum ;

% init the add height for working state.
EnableTap = 2 ;


% init the color of the figure when StateNum == 2
FigureColor2 = [0.6 0.85 0.63] ;

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

    set(FigureHandle ,'color',FigureColor2 ) ;
    
    for num = 1:2:TextNumber
        
        set(TextHandle(num),'backgroundcolor',FigureColor2) ;
    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 = 2 ;


%-------------------------------------------------------------------------

function DecodeKeyDirectly( FigureHandle )
%暴力破解密钥
%this is a function to decode the key of the vigenere code directly.

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

PushbuttonHandle = getappdata( FigureHandle,'PushbuttonHandle') ;

TextHandle = getappdata( FigureHandle,'TextHandle') ;

TextNumber = length( TextHandle ) ;

TextString = {'请输入密文:','','请输入明文:','','暴力破解后得到密钥为:',''} ;

⌨️ 快捷键说明

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