%function C=SM4(X,K,M)
%M为1时进行加密,M为0时进行解密操作,X为明文/密文输入,K为密钥输入
X='0123456789abcdeffedcba9876543210';
%X='681edf34d206965e86b3e94f536e4246';
K='0123456789abcdeffedcba9876543210';
M=1;
%****************X输入明文*************
XB=[];
A=zeros(36,32);
for i=1:32
Xi=X(i);
XBi=['0000',dec2bin(hex2dec(Xi))];
XBi=XBi(end-3:end);
XBi=[str2num(XBi(1)),str2num(XBi(2)),str2num(XBi(3)),str2num(XBi(4))];
XB=[XB,XBi];
if rem(i,8)==0
A(ceil(i/8),:)=XB;
XB=[];
end
end
%****************K输入密钥***********
KB=[];
MK=zeros(4,32);
for i=1:32
Ki=K(i);
KBi=['0000',dec2bin(hex2dec(Ki))];
KBi=KBi(end-3:end);
KBi=[str2num(KBi(1)),str2num(KBi(2)),str2num(KBi(3)),str2num(KBi(4))];
KB=[KB,KBi];
if rem(i,8)==0
MK(ceil(i/8),:)=KB;
KB=[];
end
end
%*****************FK获得******************
F='A3B1BAC656AA3350677D9197B27022DC';
FB=[];
FK=zeros(4,32);
for i=1:32
Fi=F(i);
FBi=['0000',dec2bin(hex2dec(Fi))];
FBi=FBi(end-3:end);
FBi=[str2num(FBi(1)),str2num(FBi(2)),str2num(FBi(3)),str2num(FBi(4))];
FB=[FB,FBi];
if rem(i,8)==0
FK(ceil(i/8),:)=FB;
FB=[];
end
end
%**************K初始化***********
K=zeros(36,32);
for i=1:4
K(i,:)=mod(MK(i,:)+FK(i,:),2);
end
%***************CK获得**********
C=zeros(32,4);
CK=zeros(32,32);
for i=0:31
for j=0:3
C(i+1,j+1)=mod((4*i+j)*7,256);
end
end
c=[];
for i=1:32
for j=1:4
ci=['00000000',dec2bin(C(i,j))];
ci=ci(end-7:end);
ci=[str2num(ci(1)),str2num(ci(2)),str2num(ci(3)),str2num(ci(4)),str2num(ci(5)),str2num(ci(6)),str2num(ci(7)),str2num(ci(8))];
c=[c,ci];
end
CK(i,:)=c;
c=[];
end
%**********************S盒****************************
S={'d6','90','e9','fe','cc','e1','3d','b7','16','b6','14','c2','28','fb','2c','05';
'2b','67','9a','76','2a','be','04','c3','aa','44','13','26','49','86','06','99';
'9c','42','50','f4','91','ef','98','7a','33','54','0b','43','ed','cf','ac','62';
'e4','b3','1c','a9','c9','08','e8','95','80','df','94','fa','75','8f','3f','a6';
'47','07','a7','fc','f3','73','17','ba','83','59','3c','19','e6','85','4f','a8';
'68','6b','81','b2','71','64','da','8b','f8','eb','0f','4b','70','56','9d','35';
'1e','24','0e','5e','63','58','d1','a2','25','22','7c','3b','01','21','78','87';
'd4','00','46','57','9f','d3','27','52','4c','36','02','e7','a0','c4','c8','9e';
'ea','bf','8a','d2','40','c7','38','b5','a3','f7','f2','ce','f9','61','15','a1';
'e0','ae','5d','a4','9b','34','1a','55','ad','93','32','30','f5','8c','b1','e3';
'1d','f6','e2','2e','82','66','ca','60','c0','29','23','ab','0d','53','4e','6f';
'd5','db','37','45','de','fd','8e','2f','03','ff','6a','72','6d','6c','5b','51';
'8d','1b','af','92','bb','dd','bc','7f','11','d9','5c','41','1f','10','5a','d8';
'0a','c1','31','88','a5','cd','7b','bd','2d','74','d0','12','b8','e5','b4','b0';
'89','69','97','4a','0c','96','77','7e','65','b9','f1','09','c5','6e','c6','84';
'18','f0','7d','ec','3a','dc','4d','20','79','ee','5f','3e','d7','cb','39','48'};
% T=[1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0];
%************************rk轮密钥生成************************
rk=zeros(32,32);
SB=[];
for j=1:32
T=mod(K(j+1,:)+K(j+2,:)+K(j+3,:)+CK(j,:),2);
for i=1:8:25
b1=T(i)*8+T(i+1)*4+T(i+2)*2+T(i+3)+1;
b2=T(i+4)*8+T(i+5)*4+T(i+6)*2+T(i+7)+1;
s=S(b1,b2);
si=['00000000',dec2bin(hex2dec(s))];
si=si(end-7:end);
si=[str2num(si(1)),str2num(si(2)),str2num(si(3)),str2num(si(4)),str2num(si(5)),str2num(si(6)),str2num(si(7)),str2num(si(8))];
SB=[SB,si];
end
B1=SB;
B2=[SB(14:end),SB(1:13)];
B3=[SB(24:end),SB(1:23)];
B=mod(B1+B2+B3,2);
SB=[];
K(j+4,:)=mod(K(j,:)+B(1,:),2);
rk(j,:)=K(j+4,:);
end
%******************加解密操作**********************************
if M==1
for j=1:32
T=mod(A(j+1,:)+A(j+2,:)+A(j+3,:)+rk(j,:),2);
for i=1:8:25
b1=T(i)*8+T(i+1)*4+T(i+2)*2+T(i+3)+1;
b2=T(i+4)*8+T(i+5)*4+T(i+6)*2+T(i+7)+1;
s=S(b1,b2);
si=['00000000',dec2bin(hex2dec(s))];
si=si(end-7:end);
si=[str2num(si(1)),str2num(si(2)),str2num(si(3)),str2num(si(4)),str2num(si(5)),str2num(si(6)),str2num(si(7)),str2num(si(8))];
SB=[SB,si];
end
B1=SB;
B2=[SB(3:end),SB(1:2)];
B3=[SB(11:end),SB(1:10)];
B4=[SB(19:end),SB(1:18)];
B5=[SB(25:end),SB(1:24)];
B=mod(B1+B2+B3+B4+B5,2);
SB=[];
A(j+4,:)=mod(A(j,:)+B(1,:),2);
end
else
if M==0
for j=1:32
T=mod(A(j+1,:)+A(j+2,:)+A(j+3,:)+rk(33-j,:),2);
for i=1:8:25
b1=T(i)*8+T(i+1)*4+T(i+2)*2+T(i+3)+1;
b2=T(i+4)*8+T(i+5)*4+T(i+6)*2+T(i+7)+1;
s=S(b1,b2);
si=['00000000',dec2bin(hex2dec(s))];
si=si(end-7:end);
si=[str2num(si(1)),str2num(si(2)),str2num(si(3)),str2num(si(4)),str2num(si(5)),str2num(si(6)),str2num(si(7)),str2num(si(8))];
SB=[SB,si];
end
B1=SB;
B2=[SB(3:end),SB(1:2)];
B3=[SB(11:end),SB(1:10)];
B4=[SB(19:end),SB(1:18)];
B5=[SB(25:end),SB(1:24)];
B=mod(B1+B2+B3+B4+B5,2);
SB=[];
A(j+4,:)=mod(A(j,:)+B(1,:),2);
end
end
end
%**************反序变换*******************
Y=[];
for i=1:4
for j=1:8:25
l=[num2str(A(37-i,j)),num2str(A(37-i,j+1)),num2str(A(37-i,j+2)),num2str(A(37-i,j+3)),num2str(A(37-i,j+4)),num2str(A(37-i,j+5)),num2str(A(37-i,j+6)),num2str(A(37-i,j+7))];
Y=[Y,dec2hex(bin2dec(l))];
end
end
Y
v