📄 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
'****************************************************************************
' :) 人人为我,我为人人 :)
'枕善居汉化收藏整理
'发布日期:2006/07/27
'描 述:AES加密类模块
'网 站:http://www.mndsoft.com/
'e-mail :mndsoft@163.com 最新的邮箱,如果您有新的好的代码别忘记给枕善居哦
'OICQ :88382850
'****************************************************************************
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
#Const SUPPORT_LEVEL = 0 'Default=0
'SUPPORT_LEVEL = 0 [AES Compliant] Blocksize = 128 Keysize = 128, 192, 256
'SUPPORT_LEVEL = 1 [Common Sizes ] Blocksize = 128, 192, 256 Keysize = 128, 192, 256
'SUPPORT_LEVEL = 2 [ All Sizes ] Blocksize = 128, 160, 192, 224, 256 Keysize = 128, 160, 192, 224, 256
#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
#If SUPPORT_LEVEL Then
Private rco(28) As Long
#Else
Private rco(9) As Long
#End If
'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]
#If SUPPORT_LEVEL Then
Private fkey(119) As Long 'Nb*(Nr + 1)
Private rkey(119) As Long 'Nb*(Nr + 1)
#Else
Private fkey(59) As Long 'Nb*(Nr + 1)
Private rkey(59) As Long 'Nb*(Nr + 1)
#End If
'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)
'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.
#If SUPPORT_LEVEL Then
Public Function SetCipherKey(pass() As Byte, KeyBits As Long, BlockBits As Long) As Long
#Else
Public Function SetCipherKey(pass() As Byte, KeyBits As Long) As Long
#End If
Dim i As Long
Dim j As Long
Dim s(3) As Byte
#If SUPPORT_LEVEL Then
Select Case BlockBits
Case 128
#End If
'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
'Debug.Assert i = (Nr + 1) * 4
#If SUPPORT_LEVEL = 2 Then
Case 160
i = 5
j = 0
CopyMemory fkey(0), pass(0), 4& * i
Do
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 5) 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 - 4) Xor fkey(i)
fkey(i + 2) = fkey(i - 3) Xor fkey(i + 1)
If j = 8 Then Exit Do
fkey(i + 3) = fkey(i - 2) Xor fkey(i + 2)
fkey(i + 4) = fkey(i - 1) Xor fkey(i + 3)
i = i + 5
j = j + 1
Loop
Nr = 11
'Debug.Assert i + 3 = (Nr + 1) * 4
#End If
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
'Debug.Assert i + 4 = (Nr + 1) * 4
#If SUPPORT_LEVEL = 2 Then
Case 224
i = 7
CopyMemory fkey(0), pass(0), 4& * i
For j = 0 To 6
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 7) 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 - 6) Xor fkey(i)
fkey(i + 2) = fkey(i - 5) Xor fkey(i + 1)
fkey(i + 3) = fkey(i - 4) Xor fkey(i + 2)
CopyMemory s(0), fkey(i + 3), 4&
fkey(i + 4) = fkey(i - 3) Xor (Te4(s(3)) And &HFF000000) _
Xor (Te4(s(2)) And &HFF0000) _
Xor (Te4(s(1)) And &HFF00&) _
Xor (Te4(s(0)) And &HFF&)
fkey(i + 5) = fkey(i - 2) Xor fkey(i + 4)
fkey(i + 6) = fkey(i - 1) Xor fkey(i + 5)
i = i + 7
Next j
Nr = 13
'Debug.Assert i = (Nr + 1) * 4
#End If
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)
fkey(i + 3) = fkey(i - 5) Xor fkey(i + 2)
If j = 6 Then Exit Do
CopyMemory s(0), fkey(i + 3), 4&
fkey(i + 4) = fkey(i - 4) Xor (Te4(s(3)) And &HFF000000) _
Xor (Te4(s(2)) And &HFF0000) _
Xor (Te4(s(1)) And &HFF00&) _
Xor (Te4(s(0)) And &HFF&)
fkey(i + 5) = fkey(i - 3) Xor fkey(i + 4)
fkey(i + 6) = fkey(i - 2) Xor fkey(i + 5)
fkey(i + 7) = fkey(i - 1) Xor fkey(i + 6)
i = i + 8
j = j + 1
Loop
Nr = 14
'Debug.Assert i + 4 = (Nr + 1) * 4
Case Else
Err.Raise 1, , "cRijndael.SetCipherKey - Illegal KeyBits Value"
SetCipherKey = 1
Exit Function
End Select
CreateDecryptionKeys 4
#If SUPPORT_LEVEL Then
#If SUPPORT_LEVEL = 2 Then
Case 160
'160 bit block size
Select Case KeyBits
Case 128
i = 4
CopyMemory fkey(0), pass(0), 4& * i
For j = 0 To 13
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 = 11
'Debug.Assert i = (Nr + 1) * 5
Case 160
i = 5
CopyMemory fkey(0), pass(0), 4& * i
For j = 0 To 10
CopyMemory s(0), fkey(i - 1), 4&
fkey(i) = fkey(i - 5) 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 - 4) Xor fkey(i)
fkey(i + 2) = fkey(i - 3) Xor fkey(i + 1)
fkey(i + 3) = fkey(i - 2) Xor fkey(i + 2)
fkey(i + 4) = fkey(i - 1) Xor fkey(i + 3)
i = i + 5
Next j
Nr = 11
'Debug.Assert i = (Nr + 1) * 5
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)
fkey(i + 4) = fkey(i - 2) Xor fkey(i + 3)
If j = 9 Then Exit Do
fkey(i + 5) = fkey(i - 1) Xor fkey(i + 4)
i = i + 6
j = j + 1
Loop
Nr = 12
'Debug.Assert i + 5 = (Nr + 1) * 5
Case 224
i = 7
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -