📄 rijndael.cls
字号:
Dim t0 As Long
Dim t1 As Long
Dim t2 As Long
Dim t3 As Long
Dim t4 As Long
Dim t5 As Long
Dim t6 As Long
Dim t7 As Long
Dim s(31) As Byte
CopyMemory t0, plaintext(p + 0), 4&
CopyMemory t1, plaintext(p + 4), 4&
CopyMemory t2, plaintext(p + 8), 4&
CopyMemory t3, plaintext(p + 12), 4&
CopyMemory t4, plaintext(p + 16), 4&
CopyMemory t5, plaintext(p + 20), 4&
CopyMemory t6, plaintext(p + 24), 4&
CopyMemory t7, plaintext(p + 28), 4&
t0 = t0 Xor fkey(0)
t1 = t1 Xor fkey(1)
t2 = t2 Xor fkey(2)
t3 = t3 Xor fkey(3)
t4 = t4 Xor fkey(4)
t5 = t5 Xor fkey(5)
t6 = t6 Xor fkey(6)
t7 = t7 Xor fkey(7)
k = 8
For i = 1 To Nr - 1 'Nr is number of rounds
CopyMemory s(0), t0, 4&
CopyMemory s(4), t1, 4&
CopyMemory s(8), t2, 4&
CopyMemory s(12), t3, 4&
CopyMemory s(16), t4, 4&
CopyMemory s(20), t5, 4&
CopyMemory s(24), t6, 4&
CopyMemory s(28), t7, 4&
t0 = Te0(s(0)) Xor Te1(s(5)) Xor Te2(s(14)) Xor Te3(s(19)) Xor fkey(k + 0)
t1 = Te0(s(4)) Xor Te1(s(9)) Xor Te2(s(18)) Xor Te3(s(23)) Xor fkey(k + 1)
t2 = Te0(s(8)) Xor Te1(s(13)) Xor Te2(s(22)) Xor Te3(s(27)) Xor fkey(k + 2)
t3 = Te0(s(12)) Xor Te1(s(17)) Xor Te2(s(26)) Xor Te3(s(31)) Xor fkey(k + 3)
t4 = Te0(s(16)) Xor Te1(s(21)) Xor Te2(s(30)) Xor Te3(s(3)) Xor fkey(k + 4)
t5 = Te0(s(20)) Xor Te1(s(25)) Xor Te2(s(2)) Xor Te3(s(7)) Xor fkey(k + 5)
t6 = Te0(s(24)) Xor Te1(s(29)) Xor Te2(s(6)) Xor Te3(s(11)) Xor fkey(k + 6)
t7 = Te0(s(28)) Xor Te1(s(1)) Xor Te2(s(10)) Xor Te3(s(15)) Xor fkey(k + 7)
k = k + 8
Next i
'Final round
CopyMemory s(0), t0, 4&
CopyMemory s(4), t1, 4&
CopyMemory s(8), t2, 4&
CopyMemory s(12), t3, 4&
CopyMemory s(16), t4, 4&
CopyMemory s(20), t5, 4&
CopyMemory s(24), t6, 4&
CopyMemory s(28), t7, 4&
t0 = (Te4(s(0)) And &HFF&) Xor (Te4(s(5)) And &HFF00&) Xor (Te4(s(14)) And &HFF0000) Xor (Te4(s(19)) And &HFF000000) Xor fkey(k + 0)
t1 = (Te4(s(4)) And &HFF&) Xor (Te4(s(9)) And &HFF00&) Xor (Te4(s(18)) And &HFF0000) Xor (Te4(s(23)) And &HFF000000) Xor fkey(k + 1)
t2 = (Te4(s(8)) And &HFF&) Xor (Te4(s(13)) And &HFF00&) Xor (Te4(s(22)) And &HFF0000) Xor (Te4(s(27)) And &HFF000000) Xor fkey(k + 2)
t3 = (Te4(s(12)) And &HFF&) Xor (Te4(s(17)) And &HFF00&) Xor (Te4(s(26)) And &HFF0000) Xor (Te4(s(31)) And &HFF000000) Xor fkey(k + 3)
t4 = (Te4(s(16)) And &HFF&) Xor (Te4(s(21)) And &HFF00&) Xor (Te4(s(30)) And &HFF0000) Xor (Te4(s(3)) And &HFF000000) Xor fkey(k + 4)
t5 = (Te4(s(20)) And &HFF&) Xor (Te4(s(25)) And &HFF00&) Xor (Te4(s(2)) And &HFF0000) Xor (Te4(s(7)) And &HFF000000) Xor fkey(k + 5)
t6 = (Te4(s(24)) And &HFF&) Xor (Te4(s(29)) And &HFF00&) Xor (Te4(s(6)) And &HFF0000) Xor (Te4(s(11)) And &HFF000000) Xor fkey(k + 6)
t7 = (Te4(s(28)) And &HFF&) Xor (Te4(s(1)) And &HFF00&) Xor (Te4(s(10)) And &HFF0000) Xor (Te4(s(15)) And &HFF000000) Xor fkey(k + 7)
CopyMemory ciphertext(q + 0), t0, 4&
CopyMemory ciphertext(q + 4), t1, 4&
CopyMemory ciphertext(q + 8), t2, 4&
CopyMemory ciphertext(q + 12), t3, 4&
CopyMemory ciphertext(q + 16), t4, 4&
CopyMemory ciphertext(q + 20), t5, 4&
CopyMemory ciphertext(q + 24), t6, 4&
CopyMemory ciphertext(q + 28), t7, 4&
End Sub
'Decrypt a 256 bit block. ciphertext(q ... q+31) is input, plaintext(p ... p+31) is output.
'plaintext and ciphertext can be the same array. Will crash if plaintext(p ... p+31) is not allocated.
Public Sub Block256Decrypt(plaintext() As Byte, ciphertext() As Byte, p As Long, q As Long)
Dim i As Long
Dim k As Long
Dim t0 As Long
Dim t1 As Long
Dim t2 As Long
Dim t3 As Long
Dim t4 As Long
Dim t5 As Long
Dim t6 As Long
Dim t7 As Long
Dim s(31) As Byte
CopyMemory t0, ciphertext(q + 0), 4&
CopyMemory t1, ciphertext(q + 4), 4&
CopyMemory t2, ciphertext(q + 8), 4&
CopyMemory t3, ciphertext(q + 12), 4&
CopyMemory t4, ciphertext(q + 16), 4&
CopyMemory t5, ciphertext(q + 20), 4&
CopyMemory t6, ciphertext(q + 24), 4&
CopyMemory t7, ciphertext(q + 28), 4&
t0 = t0 Xor rkey(0)
t1 = t1 Xor rkey(1)
t2 = t2 Xor rkey(2)
t3 = t3 Xor rkey(3)
t4 = t4 Xor rkey(4)
t5 = t5 Xor rkey(5)
t6 = t6 Xor rkey(6)
t7 = t7 Xor rkey(7)
k = 8
For i = 1 To Nr - 1 'Nr is number of rounds
CopyMemory s(0), t0, 4&
CopyMemory s(4), t1, 4&
CopyMemory s(8), t2, 4&
CopyMemory s(12), t3, 4&
CopyMemory s(16), t4, 4&
CopyMemory s(20), t5, 4&
CopyMemory s(24), t6, 4&
CopyMemory s(28), t7, 4&
t0 = Td0(s(0)) Xor Td1(s(29)) Xor Td2(s(22)) Xor Td3(s(19)) Xor rkey(k + 0)
t1 = Td0(s(4)) Xor Td1(s(1)) Xor Td2(s(26)) Xor Td3(s(23)) Xor rkey(k + 1)
t2 = Td0(s(8)) Xor Td1(s(5)) Xor Td2(s(30)) Xor Td3(s(27)) Xor rkey(k + 2)
t3 = Td0(s(12)) Xor Td1(s(9)) Xor Td2(s(2)) Xor Td3(s(31)) Xor rkey(k + 3)
t4 = Td0(s(16)) Xor Td1(s(13)) Xor Td2(s(6)) Xor Td3(s(3)) Xor rkey(k + 4)
t5 = Td0(s(20)) Xor Td1(s(17)) Xor Td2(s(10)) Xor Td3(s(7)) Xor rkey(k + 5)
t6 = Td0(s(24)) Xor Td1(s(21)) Xor Td2(s(14)) Xor Td3(s(11)) Xor rkey(k + 6)
t7 = Td0(s(28)) Xor Td1(s(25)) Xor Td2(s(18)) Xor Td3(s(15)) Xor rkey(k + 7)
k = k + 8
Next i
'Final round
CopyMemory s(0), t0, 4&
CopyMemory s(4), t1, 4&
CopyMemory s(8), t2, 4&
CopyMemory s(12), t3, 4&
CopyMemory s(16), t4, 4&
CopyMemory s(20), t5, 4&
CopyMemory s(24), t6, 4&
CopyMemory s(28), t7, 4&
t0 = (Td4(s(0)) And &HFF&) Xor (Td4(s(29)) And &HFF00&) Xor (Td4(s(22)) And &HFF0000) Xor (Td4(s(19)) And &HFF000000) Xor rkey(k + 0)
t1 = (Td4(s(4)) And &HFF&) Xor (Td4(s(1)) And &HFF00&) Xor (Td4(s(26)) And &HFF0000) Xor (Td4(s(23)) And &HFF000000) Xor rkey(k + 1)
t2 = (Td4(s(8)) And &HFF&) Xor (Td4(s(5)) And &HFF00&) Xor (Td4(s(30)) And &HFF0000) Xor (Td4(s(27)) And &HFF000000) Xor rkey(k + 2)
t3 = (Td4(s(12)) And &HFF&) Xor (Td4(s(9)) And &HFF00&) Xor (Td4(s(2)) And &HFF0000) Xor (Td4(s(31)) And &HFF000000) Xor rkey(k + 3)
t4 = (Td4(s(16)) And &HFF&) Xor (Td4(s(13)) And &HFF00&) Xor (Td4(s(6)) And &HFF0000) Xor (Td4(s(3)) And &HFF000000) Xor rkey(k + 4)
t5 = (Td4(s(20)) And &HFF&) Xor (Td4(s(17)) And &HFF00&) Xor (Td4(s(10)) And &HFF0000) Xor (Td4(s(7)) And &HFF000000) Xor rkey(k + 5)
t6 = (Td4(s(24)) And &HFF&) Xor (Td4(s(21)) And &HFF00&) Xor (Td4(s(14)) And &HFF0000) Xor (Td4(s(11)) And &HFF000000) Xor rkey(k + 6)
t7 = (Td4(s(28)) And &HFF&) Xor (Td4(s(25)) And &HFF00&) Xor (Td4(s(18)) And &HFF0000) Xor (Td4(s(15)) And &HFF000000) Xor rkey(k + 7)
CopyMemory plaintext(p + 0), t0, 4&
CopyMemory plaintext(p + 4), t1, 4&
CopyMemory plaintext(p + 8), t2, 4&
CopyMemory plaintext(p + 12), t3, 4&
CopyMemory plaintext(p + 16), t4, 4&
CopyMemory plaintext(p + 20), t5, 4&
CopyMemory plaintext(p + 24), t6, 4&
CopyMemory plaintext(p + 28), t7, 4&
End Sub
#End If
'Encrypt an arbritrary size array in blocks. plaintext(0 ... n-1) is input,
'ciphertext(0 ... m-1) is output, where m is n padded to a multiple of (BlockBits\8) bytes.
'plaintext and ciphertext must be distinct, and ciphertext must be a redimensionable array.
'If appendsize is non-zero, a one byte length field is appended to the plaintext
'before encrypting so the original length can be retrieved after decryption.
#If SUPPORT_LEVEL Then
Public Function ArrayEncrypt(plaintext() As Byte, ciphertext() As Byte, appendsize As Long, BlockBits As Long) As Long
#Else
Public Function ArrayEncrypt(plaintext() As Byte, ciphertext() As Byte, appendsize As Long) As Long
#End If
Dim i As Long
Dim m As Long
Dim n As Long
#If SUPPORT_LEVEL = 0 Then
Const BlockSize As Long = 16 'bytes
#Else
Dim BlockSize As Long
Select Case BlockBits
Case 128: BlockSize = 16
Case 192: BlockSize = 24
Case 256: BlockSize = 32
#If SUPPORT_LEVEL = 2 Then
Case 160: BlockSize = 20
Case 224: BlockSize = 28
#End If
Case Else: Err.Raise 1, , "cRijndael.ArrayEncrypt - Illegal BlockBits value"
End Select
#End If
If LBound(plaintext) <> 0 Then Err.Raise 1, , "cRijndael.ArrayEncrypt - plaintext must be zero based array"
n = UBound(plaintext) + 1
If appendsize = 0 Then
#If SUPPORT_LEVEL Then
m = ((n + BlockSize - 1) \ BlockSize) * BlockSize
#Else
m = (n + BlockSize - 1) And &HFFFFFFF0 'BlockSize=16 specific
#End If
ReDim ciphertext(m - 1)
Else
#If SUPPORT_LEVEL Then
m = ((n + BlockSize) \ BlockSize) * BlockSize
#Else
m = (n + BlockSize) And &HFFFFFFF0 'BlockSize=16 specific
#End If
ReDim ciphertext(m - 1)
ciphertext(m - 1) = n Mod BlockSize
End If
#If SUPPORT_LEVEL Then
Select Case BlockBits
Case 128
#End If
For i = 0 To n - BlockSize Step BlockSize
BlockEncrypt plaintext, ciphertext, i, i
Next i
#If SUPPORT_LEVEL Then
Case 192
For i = 0 To n - BlockSize Step BlockSize
Block192Encrypt plaintext, ciphertext, i, i
Next i
Case 256
For i = 0 To n - BlockSize Step BlockSize
Block256Encrypt plaintext, ciphertext, i, i
Next i
#If SUPPORT_LEVEL = 2 Then
Case 160
For i = 0 To n - BlockSize Step BlockSize
Block160Encrypt plaintext, ciphertext, i, i
Next i
Case 224
For i = 0 To n - BlockSize Step BlockSize
Block224Encrypt plaintext, ciphertext, i, i
Next i
#End If
End Select
#End If
If (n Mod BlockSize) <> 0 Then CopyMemory ciphertext(i), plaintext(i), n Mod BlockSize
#If SUPPORT_LEVEL Then
Select Case BlockBits
Case 128
#End If
If i <> m Then BlockEncrypt ciphertext, ciphertext, i, i
#If SUPPORT_LEVEL Then
Case 192
If i <> m Then Block192Encrypt ciphertext, ciphertext, i, i
Case 256
If i <> m Then Block256Encrypt ciphertext, ciphertext, i, i
#If SUPPORT_LEVEL = 2 Then
Case 160
If i <> m Then Block160Encrypt ciphertext, ciphertext, i, i
Case 224
If i <> m Then Block224Encrypt ciphertext, ciphertext, i, i
#End If
End Select
#End If
End Function
'Decrypts an array encrypted with the ArrayEncrypt function
#If SUPPORT_LEVEL Then
Public Function ArrayDecrypt(plaintext() As Byte, ciphertext() As Byte, appendsize As Long, BlockBits As Long) As Long
#Else
Public Function ArrayDecrypt(plaintext() As Byte, ciphertext() As Byte, appendsize As Long) As Long
#End If
Dim i As Long
Dim m As Long
Dim n As Long
#If SUPPORT_LEVEL = 0 Then
Const BlockSize As Long = 16 'bytes
#Else
Dim BlockSize As Long
Select Case BlockBits
Case 128: BlockSize = 16
Case 192: BlockSize = 24
Case 256: BlockSize = 32
#If SUPPORT_LEVEL = 2 Then
Case 160: BlockSize = 20
Case 224: BlockSize = 28
#End If
Case Else: Err.Raise 1, , "cRijndael.ArrayDecrypt - Illegal BlockBits value"
End Select
#End If
If LBound(ciphertext) <> 0 Then Err.Raise 1, , "cRijndael.ArrayDecrypt - ciphertext must be zero based array"
n = UBound(ciphertext) + 1
If ((n Mod BlockSize) = 0) Then
ReDim plaintext(n - 1)
#If SUPPORT_LEVEL Then
Select Case BlockBits
Case 128
#End If
For i = 0 To n - BlockSize Step BlockSize
BlockDecrypt plaintext, ciphertext, i, i
Next i
#If SUPPORT_LEVEL Then
Case 192
For i = 0 To n - BlockSize Step BlockSize
Block192Decrypt plaintext, ciphertext, i, i
Next i
Case 256
For i = 0 To n - BlockSize Step BlockSize
Block256Decrypt plaintext, ciphertext, i, i
Next i
#If SUPPORT_LEVEL = 2 Then
Case 160
For i = 0 To n - BlockSize Step BlockSize
Block160Decrypt plaintext, cipherte
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -