三、方案实现 1. 加密过程 介绍:加密按钮EnButtom回调函数 function EnButtonPushed(app, event)
if strcmp(app.Plaintext_En.Value,'')
logRefresh_func_En(app,'请输入明文');
return
end
if strcmp(app.KeyWord.Value,'')
logRefresh_func_En(app,'请输入密钥');
return
end
%创建矩阵
% %将J替换成I
alphabet='ABCDEFGHIKLMNOPQRSTUVWXYZ';
plain_origion='';
Key=app.KeyWord.Value;
Key=upper(Key);
fbar=waitbar(0,'密钥字符串预处理中');
pause(0.3)
%将所有J换成I
for i=1:length(Key)
if strcmp(Key(i),'J')
Key(i)='I';
end
end
%字符串填入矩阵,跳过重复的字符以及空格
waitbar(0.25,fbar,'矩阵化密钥字符')
pause(0.3)
for i=1:length(Key)
%如果字符已经填入或者遇到了空格
if ismember(Key(i),plain_origion)||Key(i)==' '
continue
else
plain_origion(i)=Key(i);
end
end
logRefresh_func_En(app,'矩阵化密钥字符完成');
%转换,明文信息填入一维矩阵
waitbar(0.5,fbar,'密钥信息填入矩阵')
pause(0.3)
str='';
for i=1:length(plain_origion)
if ismember(plain_origion(i),alphabet)
str=strcat(str,plain_origion(i));
else
continue
end
end
logRefresh_func_En(app,'密钥信息填入矩阵成功');
%将剩余字母依次填入一维矩阵(1*25)
waitbar(0.5,fbar,'填充矩阵信息中,请稍后')
pause(0.3)
for i=1:length(alphabet)
if ismember(alphabet(i),str)
continue
else
str=strcat(str,alphabet(i));
end
end
logRefresh_func_En(app,'矩阵填充完成');
%将矩阵转换成5*5的明文矩阵
waitbar(0.75,fbar,'转置')
app.PlaintextMat=reshape(str,5,5)';
waitbar(1,fbar,'矩阵初始化完成')
pause(0.3)
logRefresh_func_En(app,'矩阵初始化完成');
close(fbar)
%处理明文字符串
%1.当字符串为奇数个时,再最后一个明文字母后面补充其下一位字母
%2.对字符串两两分组,出现一组中字符相同时,在相同的字符中间插入其下一位字符
Pre_plaintext=app.Plaintext_En.Value;
Pre_plaintext=upper(Pre_plaintext);
%将所有J换成I
for i=1:length(Pre_plaintext)
if strcmp(Pre_plaintext(i),'J')
Pre_plaintext(i)='I';
end
end
%首先去除明文中的空格
fbar=waitbar(0,'正在去除空格,请稍后');
pause(0.3)
Bridge='';
for i=1:length(Pre_plaintext)
waitbar(i/length(Pre_plaintext),fbar,'删除空格中,请稍后');
pause(0.01);
if Pre_plaintext(i)==' '
continue
else
Bridge=strcat(Bridge,Pre_plaintext(i));
end
end
logRefresh_func_En(app,'空格删除完毕');
Pre_plaintext=Bridge;
%如果字符串长度为奇数,则填充下一位字符
if mod(length(Pre_plaintext),2)~=0
location=find(alphabet==Pre_plaintext(length(Pre_plaintext)));
if location==25
Pre_plaintext=strcat(Pre_plaintext,'A');
else
Pre_plaintext=strcat(Pre_plaintext,alphabet(location+1));
end
end
waitbar(1,fbar,'奇偶检查完毕');
logRefresh_func_En(app,'奇偶检查完毕');
pause(0.3);
%如果出现了重复的字母则再重复字母中间插入重复字母的下一位字母
%先查找字符串中重复的组别个数
Num=0;
for i=1:length(Pre_plaintext)-1
if Pre_plaintext(i)==Pre_plaintext(i+1)
Num=Num+1;
end
i=i+1;
end
%插入新字符
for i=1:length(Pre_plaintext)+Num-1
waitbar(i/length(Pre_plaintext)+Num-1,fbar,'字符插入中');
pause(0.01);
if Pre_plaintext(i)==Pre_plaintext(i+1)
Pre_plaintext(i+2:end+1)=Pre_plaintext(i+1:end);
address=find(alphabet==Pre_plaintext(i));
if address==25
Pre_plaintext(i+1)='A';
else
Pre_plaintext(i+1)=alphabet(address+1);
end
end
i=i+1;
end
logRefresh_func_En(app,'字符插入完成');
%字符串处理完毕开始加密信息
app.Ciphertext='';
for i=1:2:length(Pre_plaintext)-1
waitbar(i/length(Pre_plaintext)-1,fbar,'加密中,请稍后');
pause(0.01);
%获取相邻两个字符的在 PlaintextMat 中的位置
[M,N]=find(app.PlaintextMat==Pre_plaintext(i));
[m,n]=find(app.PlaintextMat==Pre_plaintext(i+1));
%如果在同一行
if M==m
%处理第一个字符,检查mod(N,5)是否为零
if mod(N,5)==0
%如果为零则需将此处置换为本行第一个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(M,1));
else
%否则置换为矩阵中同行的下一位字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(M,N+1));
end
%处理第二个字符,检查mod(n,5)是否为零
if mod(n,5)==0
%如果为零则需将此处置换为本行第一个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(m,1));
else
%否则置换为矩阵中同行的下一位字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(m,n+1));
end
%如果在同一列
elseif N==n
%处理第一个字符,检查mod(M,5)是否为零
if mod(M,5)==0
%如果为零,则需要将此处置换为本列的第一个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(1,N));
else
%否则置换为矩阵中同列的下一位字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(M+1,N));
end
%处理第二个字符,检查mod(m,5)是否为零
if mod(m,5)==0
%如果为零,则需要将此处置换为本列的第一个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(1,n));
else
%否则置换为矩阵中同列的下一位字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(m+1,n));
end
%如果位置行列相互错开
else
%处理第一个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(M,n));
%处理第二个字符
app.Ciphertext=strcat(app.Ciphertext,app.PlaintextMat(m,N));
end
end
waitbar(1,fbar,'加密完成');
pause(0.3);
logRefresh_func_En(app,'加密完成')
close(fbar);
app.Crypt_En.Value=app.Ciphertext;
app.Crypt_De.Value=app.Ciphertext;
end
2. 解密过程 介绍:解密按钮DeButtom回调函数 function DeButtonPushed(app, event)
ifstrcmp(app.Crypt_De.Value,'')
logRefresh_func_De(app,'请输入密文');
return
end
ifstrcmp(app.KeyWord_De.Value,'')
logRefresh_func_De(app,'请输入密钥');
return
end
%创建矩阵
% %将J替换成I
alphabet='ABCDEFGHIKLMNOPQRSTUVWXYZ';
plain_origion='';
Key=app.KeyWord_De.Value;
Key=upper(Key);
fbar=waitbar(0,'密钥字符串预处理中');
pause(0.3)
%将所有J换成I
for i=1:length(Key)
ifstrcmp(Key(i),'J')
Key(i)='I';
end
end
%字符串填入矩阵,跳过重复的字符以及空格
waitbar(0.25,fbar,'矩阵化密钥字符')
pause(0.3)
for i=1:length(Key)
%如果字符已经填入或者遇到了空格
ifismember(Key(i),plain_origion)||Key(i)==' '
continue
else
plain_origion(i)=Key(i);
end
end
logRefresh_func_De(app,'矩阵化密钥字符完成');
%转换,明文信息填入一维矩阵
waitbar(0.5,fbar,'密钥信息填入矩阵')
pause(0.3)
str='';
for i=1:length(plain_origion)
ifismember(plain_origion(i),alphabet)
str=strcat(str,plain_origion(i));
else
continue
end
end
logRefresh_func_De(app,'密钥信息填入矩阵成功');
%将剩余字母依次填入一维矩阵(1*25)
waitbar(0.5,fbar,'填充矩阵信息中,请稍后')
pause(0.3)
for i=1:length(alphabet)
ifismember(alphabet(i),str)
continue
else
str=strcat(str,alphabet(i));
end
end
logRefresh_func_De(app,'矩阵填充完成');
%将矩阵转换成5*5的明文矩阵
waitbar(0.75,fbar,'转置')
app.PlaintextMatDe=reshape(str,5,5)';
waitbar(1,fbar,'矩阵初始化完成')
pause(0.3)
logRefresh_func_De(app,'矩阵初始化完成');
close(fbar)
%获取密文
fbar=waitbar(0,'获取密文中');
Ciphertext_De=app.Ciphertext;
app.Plaintext='';
logRefresh_func_De(app,'解密中,请稍后')
for i=1:2:length(Ciphertext_De)-1
waitbar(i/length(Ciphertext_De)-1,fbar,'解密中,请稍后');
pause(0.01);
%获取相邻两个字符的在PlaintextMat 中的位置
[M,N]=find(app.PlaintextMatDe==Ciphertext_De(i));
[m,n]=find(app.PlaintextMatDe==Ciphertext_De(i+1));
%如果在同一行
ifM==m
%处理第一个字符,检查mod(N,5)是否为零
ifN==1
%如果为零则需将此处置换为本行尾字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(M,5));
else
%否则置换为矩阵中同行的上一位字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(M,N-1));
end
%处理第二个字符,检查mod(n,5)是否为零
ifn==1
%如果为零则需将此处置换为本行尾字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(m,5));
else
%否则置换为矩阵中同行的上一位字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(m,n-1));
end
%如果在同一列
elseifN==n
%处理第一个字符,检查mod(M,5)是否为零
ifM==1
%如果为零,则需要将此处置换为本列的尾巴字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(5,N));
else
%否则置换为矩阵中同列的上一位字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(M-1,N));
end
%处理第二个字符,检查mod(m,5)是否为零
ifm==1
%如果为零,则需要将此处置换为本列的尾巴字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(5,n));
else
%否则置换为矩阵中同列的上一位字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(m-1,n));
end
%如果位置行列相互错开
else
%处理第一个字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(M,n));
%处理第二个字符
app.Plaintext=strcat(app.Plaintext,app.PlaintextMatDe(m,N));
end
end
waitbar(1,fbar,'解密完成');
pause(0.3);
logRefresh_func_De(app,'解密完成')
close(fbar);
app.Plaintext_De.Value=app.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开发环境
|