三、方案实现 1. 加密过程 介绍:加密按钮EnButtom回调函数 function EnButtonPushed(app, event)
ifstrcmp(app.Plaintext_En.Value,'')
logRefresh_func_En(app,'请输入明文信息');
return
end
ifstrcmp(app.Key_En.Value,'')
logRefresh_func_En(app,'请输入密钥');
return
end
%初始化维吉尼亚密码表
app.alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
%初始化密码表
fbar=waitbar(0,'初始化密码表');
pause(0.3)
for i=1:26
waitbar(i/26,fbar,'初始化中,请稍后');
pause(0.01)
for j=1:26
app.Mat(i,j)=' ';
end
end
waitbar(1,fbar,'密码表初始化完成');
pause(0.3)
logRefresh_func_En(app,'密码表初始化完成')
%填充26*26的密码表
waitbar(0,fbar,'密码表填充');
pause(0.3)
cat='';
for i=1:26
waitbar(i/26,fbar,'密码表填充中,请稍后');
pause(0.01)
if i==1
app.Mat(i,:)=app.alphabet;
else
app.Mat(i,:)=strcat(app.alphabet(i:end),cat);
end
cat=strcat(cat,app.alphabet(i));
end
waitbar(1,fbar,'密码表填充完成');
pause(0.3)
logRefresh_func_En(app,'密码表填充完成')
Key_text=upper(app.Key_En.Value);
Plaintext=app.Plaintext_En.Value;
Plaintext=upper(Plaintext);
waitbar(0,fbar,'明文字符串处理中,请稍后');
pause(0.3)
%删除明文中的空格
Bridge='';
for i=1:length(Plaintext)
waitbar(i/length(Plaintext),fbar,'明文字符串处理中,请稍后');
pause(0.01)
ifPlaintext(i)==' '
continue
else
Bridge=strcat(Bridge,Plaintext(i));
end
end
waitbar(0,fbar,'明文字符串处理完毕');
pause(0.3)
logRefresh_func_En(app,'明文字符串处理完毕')
Plaintext=Bridge;
keytext=Plaintext;
%确定好映射关系
for i=1:length(Plaintext)
%判断该字符对应着密钥中的哪一个字符
if mod(i,length(Key_text))==0
keytext(i)=Key_text(length(Key_text));
else
keytext(i)=Key_text(mod(i,length(Key_text)));
end
end
waitbar(0,fbar,'加密中,请稍后');
pause(0.3)
%开始加密
Ciphertext='';
for i=1:length(Plaintext)
waitbar(i/length(Plaintext),fbar,'加密中,请稍后');
pause(0.01)
x=find(app.alphabet==Plaintext(i));
y=find(app.alphabet==keytext(i));
Ciphertext=strcat(Ciphertext,app.Mat(x,y));
end
waitbar(1,fbar,'加密完成');
pause(0.3)
close(fbar)
logRefresh_func_En(app,'加密完成')
app.Crypt_En.Value=Ciphertext;
app.Crypt_De.Value=Ciphertext;
end
2. 解密过程 介绍:解密按钮DeButtom回调函数 function DeButtonPushed(app, event)
ifstrcmp(app.Crypt_De.Value,'')
logRefresh_func_De(app,'请输入密文信息');
return
end
ifstrcmp(app.Key_De.Value,'')
logRefresh_func_De(app,'请输入密钥');
return
end
%初始化维吉尼亚密码表
app.alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
fbar=waitbar(0,'初始化密码表');
pause(0.3)
for i=1:26
waitbar(i/26,fbar,'初始化中,请稍后');
pause(0.01)
for j=1:26
app.Mat(i,j)=' ';
end
end
waitbar(1,fbar,'密码表初始化完成');
pause(0.3)
logRefresh_func_De(app,'密码表初始化完成')
%填充26*26的密码表
waitbar(0,fbar,'密码表填充');
pause(0.3)
cat='';
for i=1:26
waitbar(i/26,fbar,'密码表填充中,请稍后');
pause(0.01)
if i==1
app.Mat(i,:)=app.alphabet;
else
app.Mat(i,:)=strcat(app.alphabet(i:end),cat);
end
cat=strcat(cat,app.alphabet(i));
end
waitbar(1,fbar,'密码表填充完成');
pause(0.3)
logRefresh_func_De(app,'密码表填充完成')
%获取密钥以及密文
Key_text_De=upper(app.Key_De.Value);
Ciphertext=upper(app.Crypt_De.Value);
keytext_De=Ciphertext;
%确定密文和密钥之间映射关系
for i=1:length(Ciphertext)
%判断该字符对应着密钥中的哪一个字符
if mod(i,length(Key_text_De))==0
keytext_De(i)=Key_text_De(length(Key_text_De));
else
keytext_De(i)=Key_text_De(mod(i,length(Key_text_De)));
end
end
%开始解密
pause(0.3)
Plaintext='';
for i=1:length(Ciphertext)
waitbar(i/length(Ciphertext),fbar,'解密中,请稍后');
pause(0.01)
y=find(app.alphabet==keytext_De(i));
x=find(app.Mat(y,:)==Ciphertext(i));
Plaintext=strcat(Plaintext,app.alphabet(x));
end
waitbar(1,fbar,'解密完成');
pause(0.3)
close(fbar)
logRefresh_func_De(app,'解密完成')
app.Plaintext_De.Value=Plaintext;
end
3. 信息输出 介绍:加密界面信息输出函数logRefresh_func_En function logRefresh_func_En(app,StrArrayNew)
app.Ptime=datestr(now);
app.LOG=strcat('[',app.Ptime(end-7:end),']');
StrArrayNew=strcat(app.LOG,StrArrayNew);
app.StrArray_En=[app.StrArray_En,StrArrayNew,newline];
app.Process_En.Value=app.StrArray_En;
end
介绍:解密界面信息输出函数logRefresh_func_De function logRefresh_func_De(app,StrArrayNew)
app.Ptime=datestr(now);
app.LOG=strcat('[',app.Ptime(end-7:end),']');
StrArrayNew=strcat(app.LOG,StrArrayNew);
app.StrArray_De=[app.StrArray_De,StrArrayNew,newline];
app.Process_De.Value=app.StrArray_De;
end
4. 交互界面 Matlab2019b的mlapp开发环境
|