用vb實現DES加解密演算法(三)--解密 (轉)

amyz發表於2007-11-25
用vb實現DES加解密演算法(三)--解密 (轉)[@more@]

  'de  待字串
  'sKey  金鑰字串
  Public Sub DES_Decode(ByRef sCode() As Byte, ByVal sKey As String, ByRef bReturn() As Byte)
Attribute DES_Decode.VB_Description = "解密  sCode:密文16進位制串  sKey:金鑰文字,前8位有效  bReturn:明文16進位制串"
 
  Dim LenTimes As Integer  '明文
 
  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
  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
  R0(i) = CodeIP(i)
  L0(i) = CodeIP(i + 32)
  Next
 
  '進行第一次迭代
  For i = 0 To 47
  CodeE(i) = R0(E(i))  '經過E變換擴充,由32位變為48位
  CodeE(i) = CodeE(i) Xor K16(i)  '與K16按位作不進位加法運算
  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 K15(i)  '與K15按位作不進位加法運算
  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 K14(i)  '與K14按位作不進位加法運算
  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 K13(i)  '與K13按位作不進位加法運算
  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 K12(i)  '與K12按位作不進位加法運算
  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 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
 
 
  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 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))
 
  '產生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 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))  '經過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))  '經過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))  '經過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))  '經過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))  '經過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))
 
  '產生L13,R13
  R13(i) = L12(i) Xor CodeP(i)
  L13(i) = R12(i)
  Next
 
 
  '進行第14次迭代
  For i = 0 To 47
  CodeE(i) = R13(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))
 
  '產生L14,R14
  R14(i) = L13(i) Xor CodeP(i)
  L14(i) = R13(i)
  Next
 
 
  '進行第15次迭代
  For i = 0 To 47
  CodeE(i) = R14(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))
 
  '產生L15,R15
  R15(i) = L14(i) Xor CodeP(i)
  L15(i) = R14(i)
  Next
 
  '進行第16次迭代
  For i = 0 To 47
  CodeE(i) = R15(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))
 
  '產生L11,R11
  R16(i) = L15(i) Xor CodeP(i)
  L16(i) = R15(i)
  Next
 
  For i = 0 To 31
  BinCode(i) = R16(i)
  BinCode(i + 32) = L16(i)
  Next
 
  For i = 0 To 63
  CodeIP(i) = BinCode(IP_1(i))
  Next
 
  For i = 0 To 7
  tempReturn(i) = CodeIP(i * 8 + 0) * &H80
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 1) * &H40
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 2) * &H20
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 3) * &H10
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 4) * &H8
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 5) * &H4
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 6) * &H2
  tempReturn(i) = tempReturn(i) + CodeIP(i * 8 + 7) * &H1
  Next

  CopyMemory bReturn(j), tempReturn(0), 8
Next


End Sub

謹已這篇文章送給和我一樣在VB中苦苦掙扎的兄弟們!!


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-984527/,如需轉載,請註明出處,否則將追究法律責任。

相關文章