📄 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
#Const COMPILE_CONSTANTS = 0 'Default=0
'COMPILE_CONSTANTS = 0 [Fast compile, small exe size] Calculate tables during Class initialization
'COMPILE_CONSTANTS = 1 [Fast run time initialization] Compile tables of constants
'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 = 4000000
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
'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.
Public Function SetCipherKey(pass() As Byte, KeyBits As Long) As Long
Dim i As Long
Dim j As Long
Dim s(3) As Byte
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
End Function
Public Function SetCipherKeyString(PassPhrase As String, KeyBits As Long) As Long
Dim pass() As Byte
pass = StrConv(PassPhrase, vbFromUnicode)
ReDim Preserve pass(31)
SetCipherKeyString = SetCipherKey(pass, KeyBits)
End Function
'Encrypt a 128 bit block. plaintext(p ... p+15) is input, ciphertext(q ... q+15) is output.
'plaintext and ciphertext can be the same array. Will crash if ciphertext(q ... q+15) is not allocated.
Public Sub BlockEncrypt(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 s(15) 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&
t0 = t0 Xor fkey(0)
t1 = t1 Xor fkey(1)
t2 = t2 Xor fkey(2)
t3 = t3 Xor fkey(3)
k = 4
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&
t0 = Te0(s(0)) Xor Te1(s(5)) Xor Te2(s(10)) Xor Te3(s(15)) Xor fkey(k + 0)
t1 = Te0(s(4)) Xor Te1(s(9)) Xor Te2(s(14)) Xor Te3(s(3)) Xor fkey(k + 1)
t2 = Te0(s(8)) Xor Te1(s(13)) Xor Te2(s(2)) Xor Te3(s(7)) Xor fkey(k + 2)
t3 = Te0(s(12)) Xor Te1(s(1)) Xor Te2(s(6)) Xor Te3(s(11)) Xor fkey(k + 3)
k = k + 4
Next i
'Final round
CopyMemory s(0), t0, 4&
CopyMemory s(4), t1, 4&
CopyMemory s(8), t2, 4&
CopyMemory s(12), t3, 4&
t0 = (Te4(s(0)) And &HFF&) Xor (Te4(s(5)) And &HFF00&) Xor (Te4(s(10)) And &HFF0000) Xor (Te4(s(15)) And &HFF000000) Xor fkey(k + 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -