DES加密matlab实现_精品文档.docx
- 文档编号:953550
- 上传时间:2022-10-14
- 格式:DOCX
- 页数:12
- 大小:32.03KB
DES加密matlab实现_精品文档.docx
《DES加密matlab实现_精品文档.docx》由会员分享,可在线阅读,更多相关《DES加密matlab实现_精品文档.docx(12页珍藏版)》请在冰豆网上搜索。
%主程序:
是Scriptm文件
%%===DES===
%%===initialdata===
LS=[1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1];%子密钥左移次数
%%===inputdata(请输入四个汉字)===
key_in=input('请输入密钥(四个汉字用单引号括起):
');
Text_in=input('请输入明文(四个汉字用单引号括起):
');
%%===charactersconverttobinary===
key=cha2bit(key_in);%functioncha2bit()在后面定义,把汉字转为二进制
Text=cha2bit(Text_in);
%%===producingthesubciphercode(48bit)===
%==PC-1==
k=PC1(key);%functionPC1()在后面定义,做PC1置换,生成56位的有效密钥
%==循环生成子密钥==
sub_k=zeros(16,48);%保存所有的子密钥
%把有效密钥均分为两份
L=k(1:
28);
R=k(29:
56);
fori=1:
16
n=LS(i);%获得每次循环的左循环移位的次数
%左循环移位
L=l_bitmov(L,LS(i));%functionbitmov()在后面定义,左循环移位
R=l_bitmov(R,LS(i));
%PC-2置换
k=[L,R];
sub_k(i,:
)=PC2(k);
end
%sub_k%==testcode==
%%===DES===
%==初始IP置换==
Text=IP(Text);%functionIP()在后面定义,IP置换
%==feistel==
L=zeros(1,32);
R=zeros(1,32);
fori=1:
32%明文均分成两份
L(i)=Text(i);
R(i)=Text(i+32);
end
fori=1:
16
tmp=E(R);%funcitonE()在后面定义,E-扩展,得48bitR
tmp=bitxor(tmp,sub_k(i,:
));%functionbitxor()是matlab内建函数,异或运算,RK的异或运算,得48bit
tmp=S(tmp);%funcitonS()在后面定义,S-盒,得32bit
tmp=P(tmp);%funcitonp()在后面定义,P置换,得32bit
tmp=bitxor(L,tmp);%LF(R,K)的异或运算,得32bit
L=R;
R=tmp;
end
%==IP逆置换==
k=[R,L];%交换LR
Text_out=IP_1(k);%functionIP_1()在后面定义,IP逆置换
%%===output===
%==binary==
Text_o='';
n=length(Text_out);
fori=1:
n%转化为字符输出
Text_o=[Text_o,num2str(Text_out(i))];
end
Text_o
bin2hex(Text_o)%functionbin2hex()在后面定义,把二进制转为十六进制
%cha2bit():
是functionm文件
function[bin,out16]=cha2bit(str)
%function:
汉字转换为二进制和十六进制
%str:
thecharacter
%binorhex:
thecodeofGB2312
str=unicode2native(str,'GB2312');%编码转换,把unicode转为GB2312
str=dec2hex(str);%functiondec2hex(),matlab内建函数,十进制转为十六进制
bin='';
len=length(str);
fori=1:
len%对十六进制重排列,方便运算,保存到bin
bin=[bin,str(i),str(len+i)];
end
tmp=bin;%16进制
if(1==nargout)
bin=hex2bin(bin);%functionhex2bin()在后面定义,16进制转换为2进制
elseif(2==nargout)
bin=hex2bin(bin);
out16=tmp;
else
hex2bin(bin)
end
end
%hex2bin():
是functionm文件
function[S]=hex2bin(str)
%十六进制转为二进制
%strisachar
%Sisachar
%%===
len=length(str);
S='';
fori=1:
len
switchstr(i)
case'0'
S=[S,'0000'];
case'1'
S=[S,'0001'];
case'2'
S=[S,'0010'];
case'3'
S=[S,'0011'];
case'4'
S=[S,'0100'];
case'5'
S=[S,'0101'];
case'6'
S=[S,'0110'];
case'7'
S=[S,'0111'];
case'8'
S=[S,'1000'];
case'9'
S=[S,'1001'];
case{'A','a'}
S=[S,'1010'];
case{'B','b'}
S=[S,'1011'];
case{'C','c'}
S=[S,'1100'];
case{'D','c'}
S=[S,'1101'];
case{'E','e'}
S=[S,'1110'];
case{'F','f'}
S=[S,'1111'];
otherwise
disp('错误:
hex2bin输入错误!
');
return;
end
end
%bin2hex():
是functionm文件
function[hex]=bin2hex(str)
%2进制to16进制
if(mod(length(str),4)~=0)
disp('输入错误!
');
end
hex='';
len=length(str)/4;
fori=1:
len
switchstr((i-1)*4+1:
i*4)
case'0000'
hex=[hex,'0'];
case'0001'
hex=[hex,'1'];
case'0010'
hex=[hex,'2'];
case'0011'
hex=[hex,'3'];
case'0100'
hex=[hex,'4'];
case'0101'
hex=[hex,'5'];
case'0110'
hex=[hex,'6'];
case'
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DES 加密 matlab 实现 精品 文档