C语⾔实现DES加密解密详解,原理+代码
解密加密
加密
DES加密算法其实分为两个部分,⼀部分对密钥进⾏处理 ,⼀部分对明⽂进⾏处理。
下⾯从⼀个例⼦说明:
64位明⽂:
M=0011000000110001001100100011001100110100001101010011011000110111
64位密钥:
K=0011000100110010001100110011010000110101001101100011011100111000
密钥
K=00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000
置换选择
置换选择1(PC_1)8*7矩阵:
int pc_1[56]={
57,49,41,33,25,17,9,
1,58,50,42,34,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
};
在密钥当中,前⼋位00110001,第8位的1是奇偶校验位
这个表有两个作⽤,⼀是去掉8个奇偶校验位,⼆是其他位打乱重排,即得到的56位密钥(64-8)中第1位的数是原来64位的第57位的数,第2位的数是原来的第49位的数。。。
其实不⽤特意去掉奇偶校验位,因为按PC_1换位置之后剩下来的8位就是奇偶校验位,直接舍去就可以。
经过上⾯的变换我们就可以得到56位的密钥:
K(56)=00000000000000001111111111110110011001111000100000001111
再将前28位存储到C0,后28位存储到D0,即:
C0(28)=0000000000000000111111111111
D0(28)=0110011001111000100000001111
循环左移
为了得到16个⼦密钥,我们需要有16对C和D。
C1为C0循环左移得到,D1为D0循环左移得到,C2为C1循环左移得到,D2为D1循环左移得到、、、循环左移的位数如图所⽰:
循环左移1位的意思是把第⼀位保留下来,其他位左移,再将保留的第⼀位填到最后空出来的1位。
可以得到结果:
第⼀轮:
C1=0000000000000001111111111110
D1=1100110011110001000000011110
第⼆轮:
C2=0000000000000011111111111100
D2=1001100111100010000000111101
…
第16轮:
C16=0000000000000000111111111111
D16=0110011001111000100000001111
左移完成后,将每对拼接起来构成56位的密钥
K1(56)=00000000000000011111111111101100110011110001000000011110
接下来进⾏第⼆次置换
置换选择2(PC_2)6*8矩阵:
int pc_2[48]={
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
};
将56位变为48位,和PC_1原理⼀样,最终得到16个⼦密钥:
k1(48)=010100000010110010101100010101110010101011000010
k2(48)=010100001010110010100100010100001010001101000111
k3(48)=110100001010110000100110111101101000010010001100
k4(48)=111000001010011000100110010010000011011111001011
k5(48)=111000001001011000100110001111101111000000101001
k6(48)=111000001001001001110010011000100101110101100010
k7(48)=101001001101001001110010100011001010100100111010
k8(48)=101001100101001101010010111001010101111001010000
k9(48)=001001100101001101010011110010111001101001000000
k10(48)=001011110101000101010001110100001100011100111100
k11(48)=000011110100000111011001000110010001111010001100
k12(48)=000111110100000110011001110110000111000010110001
k13(48)=000111110000100110001001001000110110101000101101
k14(48)=000110110010100010001101101100100011100110010010
k15(48)=000110010010110010001100101001010000001100110111
k16(48)=010100010010110010001100101001110100001111000000
对密钥的处理完毕,下⾯开始对明⽂处理。
明⽂
M=00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 IP置换
IP置换表(IP)8*8:
//IP置换表
int IP[64]={
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
将明⽂打乱顺序,得到
M_IP(64)=0000000011111111111100001010101000000000111111110000000011001100
再将其分为两部分L0、R0
L0(32)=00000000111111111111000010101010
R0(32)=00000000111111110000000011001100
接下来,从L0R0开始推L1R1,⼀共循环16次,推出L1-L16
根据下⾯的运算规则:
Ln=R(n-1);
Rn=L(n-1)异或P( S (( E (R(n-1))异或 Kn )));
L很好得到,就是L1=R0;L2=R1;L3=R2…
L16R16的运算
拓展置换
为了得到R1,⾸先执⾏最⾥⾯的E(R0)
选择运算/拓展置换(E)8*6:
//r数组拓展置换表
int E[48]={
32,1,2,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
};
将32位的R0拓展到48位
R0(32)=00000000111111110000000011001100
R0(48)=000000000001011111111110100000000001011001011000
接下来进⾏ E(R0)异或K1(48)
k1(48)=010100000010110010101100010101110010101011000010
E(R0)异或K1(48)=010100000011101101010010110101110011110010011010
S盒
接下来进⾏ S(E(R0)异或K1(48))
代替函数S盒(S_box):
//l数组s盒置换表
int s_box[8][4][16]={
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,//s1
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,
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,//s2
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,
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,//s3
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,
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,//s4
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,
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,//s5
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,
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,//s6
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,
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,//s7
明解c语言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,15,14,2,3,12,
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,//s8
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
};
⾸先将上⼀步的结果:
E(R0)异或K1(48)=010100000011101101010010110101110011110010011010
分为8个6位的数据块
1:0101002:0000113:1011014:010010
5:1101016:1100117:1100108:011010
对第1块处理,将6位中的第1位和第6位组成⼀个⼆进制数00,转为⼗进制数X=0*2+0=0 。再将中间四位1010组成⼆进制数,转为⼗进制Y=1*8+0*4+1*2+0*1=10 。X作为⾏数,Y作为列数,第⼏块就去第⼏盒,那这个块的结果就是S1的0⾏10列,即S_box[0][0][10],结果为6,将6转为⼆进制0110 。
再对其余7块处理完成后,可以得到4*8=32位的结果:
S(E(R0)异或K1(48))(32)=01101101100000100000111011110000
P置换
再进⾏P(S(E(R0)异或K1(48)))
置换运算(P)8*4:
//P盒置换
int p[32]={
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
};
这个跟置换选择就⼀样了,可以得到结果:
P(S(E(R0)异或K1(48))(32))(32)=00010010011110001100011100011001最后将这个结果和L0进⾏异或操作:
R1 = L0 异或P(S(E(R0)异或K1(48))(32))(32)
L0(32)=00000000111111111111000010101010
R1(32)=00010010100001110011011110110011
L1(32)=R0=00000000111111110000000011001100
按照拓展置换、S置换、P置换的顺序退出来L16和R16:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论