DES算法Matlab代码

上传人:人*** 文档编号:508264550 上传时间:2023-08-26 格式:DOCX 页数:5 大小:21.72KB
返回 下载 相关 举报
DES算法Matlab代码_第1页
第1页 / 共5页
DES算法Matlab代码_第2页
第2页 / 共5页
DES算法Matlab代码_第3页
第3页 / 共5页
DES算法Matlab代码_第4页
第4页 / 共5页
DES算法Matlab代码_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《DES算法Matlab代码》由会员分享,可在线阅读,更多相关《DES算法Matlab代码(5页珍藏版)》请在金锄头文库上搜索。

1、DES算法Matlab代码functionC=DES(D,K,M)%输入16进制数表示的字符串格式明文D和密钥K,M=1表示加密%数据初始化%将以字符串形式输入的明文和密钥转换成01数字串DB=;fori=1:16Di=D(i);DBi=0000,dec2bin(hex2dec(Di);DBi=DBi(end-3:end);DBi=str2num(DBi(1),str2num(DBi(2),str2num(DBi(3),str2num(DBi(4);DB=DB,DBi;endD=DB;KB=;fori=1:16Ki=K(i);KBi=0000,dec2bin(hex2dec(Ki);KBi=K

2、Bi(end-3:end);KBi=str2num(KBi(1),str2num(KBi(2),str2num(KBi(3),str2num(KBi(4);KB=KB,KBi;endK=KB;%64位数据IP置换表IP=585042342618102;605244362820124;625446383022146;645648403224168;57494133251791;595143352719113;615345372921135;635547393123157;IP0=reshape(IP,1,64);%专换成行向量D_IP=D(IP0);%对D初始置换%E盒为数据扩展E=32,1,2

3、,3,4,5;4,5,6,7,8,9;8,9,10,11,12,13;12,13,14,15,16,17;16,17,18,19,20,21;20,21,22,23,24,25;24,25,26,27,28,29;28,29,30,31,32,1;S1=14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7;0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8;4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0;15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13;S2=15,1,8,14,6,11,3,4

4、,9,7,2,13,12,0,5,10;3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5;0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15;13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9;S3=10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8;13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1;13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7;1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12;S4=7,13,14,

5、3,0,6,9,10,1,2,8,5,11,12,4,15;13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9;10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4;3, 15,0,6,10,1,13,8,9,4,5,11,12,7,2,14;S5=2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9;14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6;4, 2,1,11,10,13,7,8,15,9,12,5,6,3,0,14;11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3

6、;S6=12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11;10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8;9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6;4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13;S7=4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1;13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6;1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2;6, 11,13,8,1,4,10,7,9,5,0,1

7、5,14,2,3,12;S8=13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7;1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2;7, 11,4,1,9,12,14,2,0,6,10,13,15,3,5,8;2, 1,14,7,4,10,8,13,15,12,9,0,3,5,6,11;%P盒置换P=16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25;PC1=57,49,41,33,25,17,9,1,58,50,42,34

8、,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4;PC2=14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32;%产生子密钥Ki=zeros(16,48);K_PC1=K(PC1

9、);%经过密钥初始置换C0=K_PC1(1:28);%分为两部分D0=K_PC1(29:56);fori=1:16ifi=1|i=2|i=9|i=16%第1、2、9、16轮循环左移1位C0=C0(2:end),C0(1);D0=D0(2:end),D0(1);else%其它轮循环左移2位C0=C0(3:end),C0(1:2);D0=D0(3:end),D0(1:2);endK_LS=C0,D0;Ki(i,:)=K_LS(PC2);end%16轮循环加密L=D_IP(1:32);%输入的左半部分明文R=D_IP(33:64);%输入的右半部分明文fori=1:16E0=reshape(E,1,

10、48);%:巴E盒转换成行向量R_E=R(E0);%E盒扩展%与子密钥相加ifM=1%选择加密与解密,M=1表示加密R_Ki=mod(R_E+Ki(i,:),2);elseR_Ki=mod(R_E+Ki(17-i,:),2);end%经S1盒B=R_Ki(1:6);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S1(x,y);C=C(end-3:end);C1=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);%经S2盒B=R_Ki(7:12);x=B(1)*2+B

11、(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S2(x,y);C=C(end-3:end);C2=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);%经S3盒B=R_Ki(13:18);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S3(x,y);C=C(end-3:end);C3=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);%经S4盒B=R_Ki(1

12、9:24);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S4(x,y);C=C(end-3:end);C4=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);%经S5盒B=R_Ki(25:30);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S5(x,y);C=C(end-3:end);C5=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(

13、4);%经S6盒B=R_Ki(31:36);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S6(x,y);C=C(end-3:end);C6=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);%经S7盒B=R_Ki(37:42);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S7(x,y);C=C(end-3:end);C7=str2num(C(1),str2num(C(2),str2num

14、(C(3),str2num(C(4);%经S8盒B=R_Ki(43:48);x=B(1)*2+B(6)+1;y=B(2)*8+B(3)*4+B(4)*2+B(5)+1;C=0000,dec2bin(S8(x,y);C=C(end-3:end);C8=str2num(C(1),str2num(C(2),str2num(C(3),str2num(C(4);C=C1,C2,C3,C4,C5,C6,C7,C8;%经过8个S盒的结果合并R_P=C(P);%P盒置换%交换左右两半部分TEMP=L;L=R;R=mod(TEMP+R_P,2);end%交换左右两半部分TEMP=L;L=R;R=TEMP;%输出密文C=L,R;%逆初始置换IP_1=408481656246432;397471555236331;386461454226230;375451353216129;364441252206028;353431151195927;342421050185826;33141949175725;IP_10=reshape(IP_1,1,64);C=C(IP_10);CS=;C=num2str(C);pos=find(C=);C=C(pos);fori=1:4:61Ci=

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 营销创新

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号