https://www.jmqz1000.com

用vb实现DES加解密算法(三)(2)





For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L7,R7
R7(i) = L6(i) Xor CodeP(i)
L7(i) = R6(i)
Next



''进行第8次迭代
For i = 0 To 47
CodeE(i) = R7(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K9(i) ''与K9按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)

''S盒运算32位结果
For i = 0 To 7
RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
Next


For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L8,R8
R8(i) = L7(i) Xor CodeP(i)
L8(i) = R7(i)
Next



''进行第9次迭代
For i = 0 To 47
CodeE(i) = R8(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K8(i) ''与K8按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)

''S盒运算32位结果
For i = 0 To 7
RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
Next


For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L9,R9
R9(i) = L8(i) Xor CodeP(i)
L9(i) = R8(i)
Next



''进行第10次迭代
For i = 0 To 47
CodeE(i) = R9(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K7(i) ''与K7按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)

''S盒运算32位结果
For i = 0 To 7
RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
Next


For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L10,R10
R10(i) = L9(i) Xor CodeP(i)
L10(i) = R9(i)
Next



''进行第11次迭代
For i = 0 To 47
CodeE(i) = R10(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K6(i) ''与K6按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)

''S盒运算32位结果
For i = 0 To 7
RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
Next


For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L11,R11
R11(i) = L10(i) Xor CodeP(i)
L11(i) = R10(i)
Next


''进行第12次迭代
For i = 0 To 47
CodeE(i) = R11(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K5(i) ''与K5按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
S(3) = S4(CodeS4(5) + CodeS4(0) * 2, CodeS4(4) + CodeS4(3) * 2 + CodeS4(2) * 4 + CodeS4(1) * 8)
S(4) = S5(CodeS5(5) + CodeS5(0) * 2, CodeS5(4) + CodeS5(3) * 2 + CodeS5(2) * 4 + CodeS5(1) * 8)
S(5) = S6(CodeS6(5) + CodeS6(0) * 2, CodeS6(4) + CodeS6(3) * 2 + CodeS6(2) * 4 + CodeS6(1) * 8)
S(6) = S7(CodeS7(5) + CodeS7(0) * 2, CodeS7(4) + CodeS7(3) * 2 + CodeS7(2) * 4 + CodeS7(1) * 8)
S(7) = S8(CodeS8(5) + CodeS8(0) * 2, CodeS8(4) + CodeS8(3) * 2 + CodeS8(2) * 4 + CodeS8(1) * 8)

''S盒运算32位结果
For i = 0 To 7
RetS(i * 4 + 0) = (S(i) And &H8) \ &H8
RetS(i * 4 + 1) = (S(i) And &H4) \ &H4
RetS(i * 4 + 2) = (S(i) And &H2) \ &H2
RetS(i * 4 + 3) = (S(i) And &H1) \ &H1
Next


For i = 0 To 31
''P变换
CodeP(i) = RetS(P(i))

''产生L12,R12
R12(i) = L11(i) Xor CodeP(i)
L12(i) = R11(i)
Next

''进行第13次迭代
For i = 0 To 47
CodeE(i) = R12(E(i))\t\t ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K4(i) ''与K4按位作不进位加法运算
Next

''分8组
For i = 0 To 5
CodeS1(i) = CodeE(i)
CodeS2(i) = CodeE(i + 6)
CodeS3(i) = CodeE(i + 12)
CodeS4(i) = CodeE(i + 18)
CodeS5(i) = CodeE(i + 24)
CodeS6(i) = CodeE(i + 30)
CodeS7(i) = CodeE(i + 36)
CodeS8(i) = CodeE(i + 42)
Next

''S盒运算,得到8个数

S(0) = S1(CodeS1(5) + CodeS1(0) * 2, CodeS1(4) + CodeS1(3) * 2 + CodeS1(2) * 4 + CodeS1(1) * 8)
S(1) = S2(CodeS2(5) + CodeS2(0) * 2, CodeS2(4) + CodeS2(3) * 2 + CodeS2(2) * 4 + CodeS2(1) * 8)
S(2) = S3(CodeS3(5) + CodeS3(0) * 2, CodeS3(4) + CodeS3(3) * 2 + CodeS3(2) * 4 + CodeS3(1) * 8)
&n

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读