三、方案实现 1. 加密过程 介绍:加密按钮EnButtom回调函数 function EnButtonPushed(app, event)
ifstrcmp(app.Plaintext_En.Value,'')
logRefresh_func_En(app,'请输入明文');
return
end
ifapp.K1_En.Value==0||app.K2_En.Value==0
logRefresh_func_En(app,'请输入密钥');
return
end
if ~ismember(app.K1_En.Value,app.key)
logRefresh_func_En(app,'请输入gcd(k1,26)=1的k1');
return
end
plaintext=upper(app.Plaintext_En.Value);
fbar=waitbar(0,'明文字符串处理中,请稍后');
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(i/length(plaintext),fbar,'字符串处理完成')
pause(0.01);
logRefresh_func_En(app,'字符串处理完成')
plaintext=Bridge;
waitbar(0,fbar,'加密中,请稍后')
pause(0.3);
%依次读取字符,经过仿射运算确定其加密后的字符
ciphertext='';
k1=app.k1_en;
k2=app.k2_en;
for i=1:length(plaintext)
waitbar(i/length(plaintext),fbar,'加密中,请稍后')
pause(0.01);
location=find(app.alphabet==plaintext(i))-1;
location_en=mod(k1*location+k2,26);
iflocation_en==0
ciphertext=strcat(ciphertext,'A');
else
ciphertext=strcat(ciphertext,app.alphabet(location_en+1));
end
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
ifapp.K1_De.Value==0||app.K2_De.Value==0
logRefresh_func_De(app,'请输入密钥');
return
end
if ~ismember(app.K1_De.Value,app.key)
logRefresh_func_De(app,'请输入gcd(k1,26)=1的k1');
return
end
ciphertext=app.Crypt_De.Value;
plaintext='';
k1=app.k1_de;
k2=app.k2_de;
%获取k1的逆元
address=find(app.key==k1);
fbar=waitbar(0,'解密中,请稍后');
pause(0.3);
for i=1:length(ciphertext)
waitbar(i/length(ciphertext),fbar,'解密中,请稍后');
pause(0.01);
location=find(app.alphabet==ciphertext(i))-1;
location_de=mod(app.niyuan(address)*(location-k2),26);
iflocation_de==0
plaintext=strcat(plaintext,'A');
else
plaintext=strcat(plaintext,app.alphabet(location_de+1));
end
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开发环境
|