📄 rijndael.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cRijndael"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'Title: cRijndael - Advanced Encryption Standard (AES) Class
'Author: John Korejwa <korejwa@tiac.net>
'Filename: Rijndael.cls
'Date: 09 / July / 2006
'Version: 1.0
'Usage:
'Create an instance of the class, set the cipher key, encrypt/decrypt to your heart's content
'These are arrays of constants. They are initialized with the Class and do not change.
Private Te0(255) As Long
Private Te1(255) As Long
Private Te2(255) As Long
Private Te3(255) As Long
Private Te4(255) As Long
Private Td0(255) As Long
Private Td1(255) As Long
Private Td2(255) As Long
Private Td3(255) As Long
Private Td4(255) As Long
Private rco(9) As Long
'Key schedule arrays
Private Nr As Long 'Number of rounds [For 128 bit block, Nr = {10, 12, 14} for 128, 192, 256 bit cipher key]
Private fkey(59) As Long 'Nb*(Nr + 1)
Private rkey(59) As Long 'Nb*(Nr + 1)
'For file encryption, this is the maximum amount of memory (in bytes) allowed for file data
Private Const MaxFileChunkSize As Long = 4& * 1024& * 1024&
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'Decryption Key Scheduler. Calculate rkey() decryption keys based on fkey() and Nr.
'Nb is the number of 32 bit units in the block size.
Private Sub CreateDecryptionKeys(Nb As Long)
Dim i As Long
Dim j As Long
Dim k As Long
Dim s(3) As Byte
'Invert the order of the round keys
i = 0
j = Nb * Nr
For k = 0 To Nr
CopyMemory rkey(i), fkey(j), Nb * 4&
i = i + Nb
j = j - Nb
Next k
'Apply the inverse MixColumn transform to all round keys except the first and the last
For i = Nb To Nb * Nr - 1
CopyMemory s(0), rkey(i), 4&
rkey(i) = Td0(Te4(s(0)) And &HFF&) Xor _
Td1(Te4(s(1)) And &HFF&) Xor _
Td2(Te4(s(2)) And &HFF&) Xor _
Td3(Te4(s(3)) And &HFF&)
Next i
End Sub
'Key Scheduler. Expand the cipher key into the encryption key schedule.
'pass(0 ... n-1) contains the cipher key, where n = {16, 20, 24, 28, 32} , depending on KeyBits.
'If SUPPORT_LEVEL is non-zero, you must specify BlockBits for the block size you will be using.
'Returns zero on success.
Public Function SetCipherKey(pass() As Byte, KeyBits As Long) As Long
Dim i As Long
Dim j As Long
Dim s(3) As Byte
'128 bit block size
Select Case KeyBits
Case 128
i = 4
CopyMemory fkey(0), pass(0), 4& * i
For j = 0 To 9
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 4) Xor (Te4(s(0)) And &HFF000000) _
Xor (Te4(s(3)) And &HFF0000) _
Xor (Te4(s(2)) And &HFF00&) _
Xor (Te4(s(1)) And &HFF&) _
Xor rco(j)
fkey(i + 1) = fkey(i - 3) Xor fkey(i)
fkey(i + 2) = fkey(i - 2) Xor fkey(i + 1)
fkey(i + 3) = fkey(i - 1) Xor fkey(i + 2)
i = i + 4
Next j
Nr = 10
Case 192
i = 6
j = 0
CopyMemory fkey(0), pass(0), 4& * i
Do
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 6) Xor (Te4(s(0)) And &HFF000000) _
Xor (Te4(s(3)) And &HFF0000) _
Xor (Te4(s(2)) And &HFF00&) _
Xor (Te4(s(1)) And &HFF&) _
Xor rco(j)
fkey(i + 1) = fkey(i - 5) Xor fkey(i)
fkey(i + 2) = fkey(i - 4) Xor fkey(i + 1)
fkey(i + 3) = fkey(i - 3) Xor fkey(i + 2)
If j = 7 Then Exit Do
fkey(i + 4) = fkey(i - 2) Xor fkey(i + 3)
fkey(i + 5) = fkey(i - 1) Xor fkey(i + 4)
i = i + 6
j = j + 1
Loop
Nr = 12
Case 256
i = 8
j = 0
CopyMemory fkey(0), pass(0), 4& * i
Do
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 8) Xor (Te4(s(0)) And &HFF000000) _
Xor (Te4(s(3)) And &HFF0000) _
Xor (Te4(s(2)) And &HFF00&) _
Xor (Te4(s(1)) And &HFF&) _
Xor rco(j)
fkey(i + 1) = fkey(i - 7) Xor fkey(i)
fkey(i + 2) = fkey(i - 6) Xor fkey(i + 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -