用VB实现DES加解密算法(二)(1)
''sKey 密钥字串(前8位有效)
Public Sub DES_Encode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)
Attribute DES_Encode.VB_Description = "加密 sCode:明文16进制串 sKey:密钥文本,前8位有效 bReturn:密文16进制串"
Dim tempKey() As Byte ''存放密钥
Dim BinKey(63) As Byte ''64位二进制原始密钥
Dim KeyPC_1(55) As Byte ''存放56位密钥
Dim tempCode(7) As Byte ''存放8位原始明文
Dim tempReturn(7) As Byte ''存放8位密文
Dim BinCode(63) As Byte ''存放64位明文
Dim CodeIP(63) As Byte ''存放IP置换结果
Dim CodeE(47) As Byte ''E膨胀结果
Dim CodeP(31) As Byte ''P变换结果
Dim RetS(47) As Byte ''S盒运算32位结果
Dim S(7) As Byte ''S盒运算8个结果
Dim CodeS1(5) As Byte: Dim CodeS2(5) As Byte: Dim CodeS3(5) As Byte: Dim CodeS4(5) As Byte
Dim CodeS5(5) As Byte: Dim CodeS6(5) As Byte: Dim CodeS7(5) As Byte: Dim CodeS8(5) As Byte
Dim L0(31) As Byte: Dim R0(31) As Byte
Dim L1(31) As Byte: Dim R1(31) As Byte
Dim L2(31) As Byte: Dim R2(31) As Byte
Dim L3(31) As Byte: Dim R3(31) As Byte
Dim L4(31) As Byte: Dim R4(31) As Byte
Dim L5(31) As Byte: Dim R5(31) As Byte
Dim L6(31) As Byte: Dim R6(31) As Byte
Dim L7(31) As Byte: Dim R7(31) As Byte
Dim L8(31) As Byte: Dim R8(31) As Byte
Dim L9(31) As Byte: Dim R9(31) As Byte
Dim L10(31) As Byte: Dim R10(31) As Byte
Dim L11(31) As Byte: Dim R11(31) As Byte
Dim L12(31) As Byte: Dim R12(31) As Byte
Dim L13(31) As Byte: Dim R13(31) As Byte
Dim L14(31) As Byte: Dim R14(31) As Byte
Dim L15(31) As Byte: Dim R15(31) As Byte
Dim L16(31) As Byte: Dim R16(31) As Byte
Dim C0(27) As Byte: Dim D0(27) As Byte ''16个密钥
Dim C1(27) As Byte: Dim D1(27) As Byte
Dim C2(27) As Byte: Dim D2(27) As Byte:
Dim C3(27) As Byte: Dim D3(27) As Byte:
Dim C4(27) As Byte: Dim D4(27) As Byte:
Dim C5(27) As Byte: Dim D5(27) As Byte:
Dim C6(27) As Byte: Dim D6(27) As Byte:
Dim C7(27) As Byte: Dim D7(27) As Byte:
Dim C8(27) As Byte: Dim D8(27) As Byte:
Dim C9(27) As Byte: Dim D9(27) As Byte:
Dim C10(27) As Byte: Dim D10(27) As Byte:
Dim C11(27) As Byte: Dim D11(27) As Byte:
Dim C12(27) As Byte: Dim D12(27) As Byte:
Dim C13(27) As Byte: Dim D13(27) As Byte:
Dim C14(27) As Byte: Dim D14(27) As Byte:
Dim C15(27) As Byte: Dim D15(27) As Byte:
Dim C16(27) As Byte: Dim D16(27) As Byte:
Dim C_D(55) As Byte ''Cn,Dn合并后的存放处
Dim K1(47) As Byte: Dim K2(47) As Byte: Dim K3(47) As Byte: Dim K4(47) As Byte:
Dim K5(47) As Byte: Dim K6(47) As Byte: Dim K7(47) As Byte: Dim K8(47) As Byte:
Dim K9(47) As Byte: Dim K10(47) As Byte: Dim K11(47) As Byte: Dim K12(47) As Byte:
Dim K13(47) As Byte: Dim K14(47) As Byte: Dim K15(47) As Byte: Dim K16(47) As Byte:
Dim i As Integer
Dim j As Integer
''取密钥的前8字节
tempKey = StrConv(sKey, vbFromUnicode)
ReDim Preserve tempKey(7)
For i = 0 To 7
BinKey(i * 8 + 0) = (tempKey(i) And &H80) \ &H80
BinKey(i * 8 + 1) = (tempKey(i) And &H40) \ &H40
BinKey(i * 8 + 2) = (tempKey(i) And &H20) \ &H20
BinKey(i * 8 + 3) = (tempKey(i) And &H10) \ &H10
BinKey(i * 8 + 4) = (tempKey(i) And &H8) \ &H8
BinKey(i * 8 + 5) = (tempKey(i) And &H4) \ &H4
BinKey(i * 8 + 6) = (tempKey(i) And &H2) \ &H2
BinKey(i * 8 + 7) = (tempKey(i) And &H1) \ &H1
Next
''PC_1转换
For i = 0 To 55
KeyPC_1(i) = BinKey(PC_1(i))
Next
''生成C0,D0
For i = 0 To 27
C0(i) = KeyPC_1(i)
D0(i) = KeyPC_1(i + 28)
Next
''***************************************************K1
''生成C1,D1
For i = 0 To 26
C1(i) = C0(i + Lsi(1))
D1(i) = D0(i + Lsi(1))
Next
C1(27) = C0(0)
D1(27) = D0(0)
''组合C1,D1成C_D
For i = 0 To 27
C_D(i) = C1(i)
C_D(i + 28) = D1(i)
Next
''PC_2转换,生成K1
For i = 0 To 47
K1(i) = C_D(PC_2(i))
Next
''***************************************************K2
''生成C2,D2
For i = 0 To 26
C2(i) = C1(i + Lsi(2))
D2(i) = D1(i + Lsi(2))
Next
C2(27) = C1(0)
D2(27) = D1(0)
''组合C2,D2成C_D
For i = 0 To 27
C_D(i) = C2(i)
C_D(i + 28) = D2(i)
Next
''PC_2转换,生成K2
For i = 0 To 47
K2(i) = C_D(PC_2(i))
Next
''***************************************************K3
''生成C3,D3
For i = 0 To 25
C3(i) = C2(i + Lsi(3))
D3(i) = D2(i + Lsi(3))
Next
C3(26) = C2(0)
D3(26) = D2(0)
C3(27) = C2(1)
D3(27) = D2(1)
''组合C3,D3成C_D
For i = 0 To 27
C_D(i) = C3(i)
C_D(i + 28) = D3(i)
Next
''PC_2转换,生成K3
For i = 0 To 47
K3(i) = C_D(PC_2(i))
Next
''***************************************************K4
''生成C4,D4
For i = 0 To 25
C4(i) = C3(i + Lsi(4))
D4(i) = D3(i + Lsi(4))
Next
C4(26) = C3(0)
D4(26) = D3(0)
C4(27) = C3(1)
D4(27) = D3(1)
''组合C4,D4成C_D
For i = 0 To 27
C_D(i) = C4(i)
C_D(i + 28) = D4(i)
Next
''PC_2转换,生成K4
For i = 0 To 47
K4(i) = C_D(PC_2(i))
Next
''***************************************************K5
''生成C5,D5
For i = 0 To 25
C5(i) = C4(i + Lsi(5))
D5(i) = D4(i + Lsi(5))
Next
C5(26) = C4(0)
D5(26) = D4(0)
C5(27) = C4(1)
D5(27) = D4(1)
''组合C5,D5成C_D
For i = 0 To 27
C_D(i) = C5(i)
C_D(i + 28) = D5(i)
Next
''PC_2转换,生成K5
For i = 0 To 47
K5(i) = C_D(PC_2(i))
Next
''***************************************************K6
''生成C6,D6
For i = 0 To 25
C6(i) = C5(i + Lsi(6))
D6(i) = D5(i + Lsi(6))
Next
C6(26) = C5(0)
D6(26) = D5(0)
C6(27) = C5(1)
D6(27) = D5(1)
''组合C6,D6成C_D
For i = 0 To 27
C_D(i) = C6(i)
C_D(i + 28) = D6(i)
Next
''PC_2转换,生成K6
For i = 0 To 47
K6(i) = C_D(PC_2(i))
Next
''***************************************************K7
''生成C7,D7
For i = 0 To 25
C7(i) = C6(i + Lsi(7))
D7(i) = D6(i + Lsi(7))
Next
C7(26) = C6(0)
D7(26) = D6(0)
C7(27) = C6(1)
D7(27) = D6(1)
''组合C7,D7成C_D
For i = 0 To 27
C_D(i) = C7(i)
C_D(i + 28) = D7(i)
Next
''PC_2转换,生成K7
For i = 0 To 47
K7(i) = C_D(PC_2(i))
Next
''***************************************************K8
''生成C8,D8
For i = 0 To 25
C8(i) = C7(i + Lsi(8))
D8(i) = D7(i + Lsi(8))
Next
C8(26) = C7(0)
D8(26) = D7(0)
C8(27) = C7(1)
D8(27) = D7(1)
''组合C8,D8成C_D
For i = 0 To 27
C_D(i) = C8(i)
C_D(i + 28) = D8(i)
Next
''PC_2转换,生成K8
For i = 0 To 47
K8(i) = C_D(PC_2(i))
Next
''***************************************************K9
''生成C9,D9
For i = 0 To 26
C9(i) = C8(i + Lsi(9))
D9(i) = D8(i + Lsi(9))
Next
C9(27) = C8(0)
D9(27) = D8(0)
''组合C9,D9成C_D
For i = 0 To 27
C_D(i) = C9(i)
C_D(i + 28) = D9(i)
Next
''PC_2转换,生成K9
For i = 0 To 47
K9(i) = C_D(PC_2(i))
Next
''***************************************************K10
''生成C10,D10
For i = 0 To 25
C10(i) = C9(i + Lsi(10))
D10(i) = D9(i + Lsi(10))
Next
C10(26) = C9(0)
D10(26) = D9(0)
C10(27) = C9(1)
D10(27) = D9(1)
''组合C10,D10成C_D
For i = 0 To 27
C_D(i) = C10(i)
C_D(i + 28) = D10(i)
Next
''PC_2转换,生成K10
For i = 0 To 47
K10(i) = C_D(PC_2(i))
Next
''***************************************************K11
''生成C11,D11
For i = 0 To 25
C11(i) = C10(i + Lsi(11))
D11(i) = D10(i + Lsi(11))
Next
C11(26) = C10(0)
D11(26) = D10(0)
C11(27) = C10(1)
D11(27) = D10(1)
''组合C11,D11成C_D
For i = 0 To 27
C_D(i) = C11(i)
C_D(i + 28) = D11(i)
Next
''PC_2转换,生成K11
For i = 0 To 47
K11(i) = C_D(PC_2(i))
Next
''***************************************************K12
''生成C12,D12
For i = 0 To 25
C12(i) = C11(i + Lsi(12))
D12(i) = D11(i + Lsi(12))
Next
C12(26) = C11(0)
D12(26) = D11(0)
C12(27) = C11(1)
D12(27) = D11(1)
''组合C12,D12成C_D
For i = 0 To 27
C_D(i) = C12(i)
C_D(i + 28) = D12(i)
Next
''PC_2转换,生成K12
For i = 0 To 47
K12(i) = C_D(PC_2(i))
Next
''***************************************************K13
''生成C13,D13
For i = 0 To 25
C13(i) = C12(i + Lsi(13))
D13(i) = D12(i + Lsi(13))
Next
C13(26) = C12(0)
D13(26) = D12(0)
C13(27) = C12(1)
D13(27) = D12(1)
''组合C13,D13成C_D
For i = 0 To 27
C_D(i) = C13(i)
C_D(i + 28) = D13(i)
Next
''PC_2转换,生成K13
For i = 0 To 47
K13(i) = C_D(PC_2(i))
Next
''***************************************************K14
''生成C14,D14
For i = 0 To 25
C14(i) = C13(i + Lsi(14))
D14(i) = D13(i + Lsi(14))
Next
C14(26) = C13(0)
D14(26) = D13(0)
C14(27) = C13(1)
D14(27) = D13(1)
''组合C14,D14成C_D
For i = 0 To 27
C_D(i) = C14(i)
C_D(i + 28) = D14(i)
Next
''PC_2转换,生成K14
For i = 0 To 47
K14(i) = C_D(PC_2(i))
Next
''***************************************************K15
''生成C15,D15
For i = 0 To 25
C15(i) = C14(i + Lsi(15))
D15(i) = D14(i + Lsi(15))
Next
C15(26) = C14(0)
D15(26) = D14(0)
C15(27) = C14(1)
D15(27) = D14(1)
''组合C15,D15成C_D
For i = 0 To 27
C_D(i) = C15(i)
C_D(i + 28) = D15(i)
Next
''PC_2转换,生成K15
For i = 0 To 47
K15(i) = C_D(PC_2(i))
Next
''***************************************************K16
''生成C16,D16
For i = 0 To 26
C16(i) = C15(i + Lsi(16))
D16(i) = D15(i + Lsi(16))
Next
C16(27) = C15(0)
D16(27) = D15(0)
''组合C16,D16成C_D
For i = 0 To 27
C_D(i) = C16(i)
C_D(i + 28) = D16(i)
Next
''PC_2转换,生成K16
For i = 0 To 47
K16(i) = C_D(PC_2(i))
Next
''**************************************************************************************
''将明文位数扩展为8的倍数
If (UBound(sCode) + 1) Mod 8 > 0 Then ReDim Preserve sCode(((UBound(sCode) + 1) \ 8 + 1) * 8 - 1)
''定义返回密文长度
ReDim bReturn(UBound(sCode))
For j = 0 To UBound(sCode) Step 8
''加密过程
''依次取8位加密
CopyMemory tempCode(0), sCode(j), 8
For i = 0 To 7
BinCode(i * 8 + 0) = (tempCode(i) And &H80) \ &H80
BinCode(i * 8 + 1) = (tempCode(i) And &H40) \ &H40
BinCode(i * 8 + 2) = (tempCode(i) And &H20) \ &H20
BinCode(i * 8 + 3) = (tempCode(i) And &H10) \ &H10
BinCode(i * 8 + 4) = (tempCode(i) And &H8) \ &H8
BinCode(i * 8 + 5) = (tempCode(i) And &H4) \ &H4
BinCode(i * 8 + 6) = (tempCode(i) And &H2) \ &H2
BinCode(i * 8 + 7) = (tempCode(i) And &H1) \ &H1
Next
''IP置换
For i = 0 To 63
CodeIP(i) = BinCode(IP(i))
Next
''分段
For i = 0 To 31
L0(i) = CodeIP(i)
R0(i) = CodeIP(i + 32)
Next
''进行第一次迭代
For i = 0 To 47
CodeE(i) = R0(E(i)) ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K1(i) ''与K1按位作不进位加法运算
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))
''产生L1,R1
R1(i) = L0(i) Xor CodeP(i)
L1(i) = R0(i)
Next
''进行第二次迭代
For i = 0 To 47
CodeE(i) = R1(E(i)) ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K2(i) ''与K2按位作不进位加法运算
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))
''产生L2,R2
R2(i) = L1(i) Xor CodeP(i)
L2(i) = R1(i)
Next
''进行第三次迭代
For i = 0 To 47
CodeE(i) = R2(E(i)) ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K3(i) ''与K3按位作不进位加法运算
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))
''产生L3,R3
R3(i) = L2(i) Xor CodeP(i)
L3(i) = R2(i)
Next
''进行第四次迭代
For i = 0 To 47
CodeE(i) = R3(E(i)) ''经过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)
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))
''产生L4,R4
R4(i) = L3(i) Xor CodeP(i)
L4(i) = R3(i)
Next
''进行第五次迭代
For i = 0 To 47
CodeE(i) = R4(E(i)) ''经过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))
''产生L5,R5
R5(i) = L4(i) Xor CodeP(i)
L5(i) = R4(i)
Next
''进行第六次迭代
For i = 0 To 47
CodeE(i) = R5(E(i)) ''经过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))
''产生L6,R6
R6(i) = L5(i) Xor CodeP(i)
L6(i) = R5(i)
Next
''进行第7次迭代
For i = 0 To 47
CodeE(i) = R6(E(i)) ''经过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))
''产生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)) ''经过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))
''产生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)) ''经过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))
''产生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)) ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K10(i) ''与K10按位作不进位加法运算
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)) ''经过E变换扩充,由32位变为48位
CodeE(i) = CodeE(i) Xor K11(i) ''与K11按位作不进位加法运算
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