日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MATLAB实现滚动密钥密码

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MATLAB实现滚动密钥密码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

滾動密鑰密碼

對于周期代換密碼,當密鑰的長度d和明文一樣長時,就成為滾動密鑰密碼。

具體可見如下表所示:

明文meetatnineintheevening
密鑰beijingmeetatnineinthe
密文NIMCIGTURIBNMUMRZMABUK

MATLAB代碼如下:

function keyT = getKey(key,plaintext) %getKey:滾動密碼的密鑰生成函數 %key:密鑰 %plaintext:明文 %keyT:密鑰對照表p_long = length(plaintext); key=upper(key); for i=1:p_longkeyT(1,i)=plaintext(i);if i<=length(key)keyT(2,i)=key(i);endif i>length(key)keyT(2,i)=plaintext(i-length(key));end end end function ciphertext = encryption(keyT,plaintext) %encryption:滾動密碼的加密算法 %keyT:密鑰 %plaintext:明文 %ciphertext:密文ciphertext = ''; p_long=length(plaintext); for i=1:p_longif abs(plaintext(i))<=65+26&&abs(plaintext(i))>=65temp=mod(abs(keyT(2,i))+abs(plaintext(i))-65-65,26);ciphertext=strcat(ciphertext,char(temp+97));endif abs(plaintext(i))>=97&&abs(plaintext(i))<=97+26temp=mod(abs(keyT(2,i))+abs(plaintext(i))-97-65,26);ciphertext=strcat(ciphertext,char(temp+65));end end end function plaintext = decrypt(keyT,ciphertext) %decrypt:解密算法 %ciphertext:密文 %plaintext:明文 %keyT:密鑰c_long = length(ciphertext); plaintext=''; for i=1:c_longif abs(ciphertext(i))<=65+26&&abs(ciphertext(i))>=65temp=mod(-abs(keyT(2,i))+abs(ciphertext(i))-65-65+26,26);plaintext=strcat(plaintext,char(temp+97));endif abs(ciphertext(i))>=97&&abs(ciphertext(i))<=97+26temp=mod(-abs(keyT(2,i))+abs(ciphertext(i))-97-65+26,26);plaintext=strcat(plaintext,char(temp+65));end end end

測試代碼如下:

disp('密匙短語如下:');key='abcd';disp(key);disp('明文如下:');plaintext='csdghfnjsdfgYBGUGY';disp(plaintext);keyT = getKey(key,plaintext); ciphertext = encryption(keyT,plaintext);disp('密文如下:');disp(ciphertext); plaintext = decrypt(keyT,ciphertext);disp('解密后明文如下:');disp(plaintext);

測試結果如下:

>> ceshi 密匙短語如下: abcd 明文如下: csdghfnjsdfgYBGUGY 密文如下: CTFJPDWVFOYVwkrgez 解密后明文如下: csdghfnjsdfgYBGUGY

密鑰如下:

'csdghfnjsdfgYBGUGY''ABCDcsdghfnjsdfgYB'

總結

以上是生活随笔為你收集整理的MATLAB实现滚动密钥密码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。