⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb.txt

📁 aes加解密算法源代码
💻 TXT
字号:

Private Const BlockLengthMax = 32           ' maximum block length in bytes
Private Const KeyLengthMax = 32             ' maximum block length in bytes
Private Const KeyScheduleLengthMax = 64     ' maximum key schedule length in bytes

Private Type EncCtx                         ' type to hold the AES encryption context data
  Ekey(0 To KeyScheduleLengthMax - 1) As Long
End Type

Private Type DecCtx                         ' type to hold the AES decryption context data
  Ekey(0 To KeyScheduleLengthMax - 1) As Long
End Type

Private Type Key                            ' type to hold user key data
 K(0 To KeyLengthMax - 1) As Byte
End Type

Private Type InOut                          ' type to hold cipher input and output blocks
 IO(0 To BlockLengthMax - 1) As Byte
End Type

Rem Change "c:\temp\" in the following lines to the directory path where the AES DLL is located
Private Declare Function AesEncryptKey Lib "c:\temp\aes.dll" Alias "_aes_encrypt_key@12" (K As Key, ByVal N As Long, C As EncCtx) As Integer
Private Declare Function AesEncryptKey128 Lib "c:\temp\aes.dll" Alias "_aes_encrypt_key128@8" (K As Key, C As EncCtx) As Integer
Private Declare Function AesEncryptKey192 Lib "c:\temp\aes.dll" Alias "_aes_encrypt_key192@8" (K As Key, C As EncCtx) As Integer
Private Declare Function AesEncryptKey256 Lib "c:\temp\aes.dll" Alias "_aes_encrypt_key256@8" (K As Key, C As EncCtx) As Integer
Private Declare Function AesEncrypt Lib "c:\temp\aes.dll" Alias "_aes_encrypt@12" (Ib As InOut, Ob As InOut, C As EncCtx) As Integer
Private Declare Function AesDecryptKey Lib "c:\temp\aes.dll" Alias "_aes_decrypt_key@12" (K As Key, ByVal N As Long, C As DecCtx) As Integer
Private Declare Function AesDecryptKey128 Lib "c:\temp\aes.dll" Alias "_aes_decrypt_key128@8" (K As Key, C As DecCtx) As Integer
Private Declare Function AesDecryptKey192 Lib "c:\temp\aes.dll" Alias "_aes_decrypt_key192@8" (K As Key, C As DecCtx) As Integer
Private Declare Function AesDecryptKey256 Lib "c:\temp\aes.dll" Alias "_aes_decrypt_key256@8" (K As Key, C As DecCtx) As Integer
Private Declare Function AesDecrypt Lib "c:\temp\aes.dll" Alias "_aes_decrypt@12" (Ib As InOut, Ob As InOut, C As DecCtx) As Integer

Private Sub Hex(X As Byte)                  ' output a byte in hexadecimal format
Dim H As Byte
H = Int(X / 16)
If H < 10 Then
    Debug.Print Chr(48 + H);
Else
    Debug.Print Chr(87 + H);
End If
H = Int(X Mod 16)
If H < 10 Then
    Debug.Print Chr(48 + H);
Else
    Debug.Print Chr(87 + H);
End If
End Sub

Private Sub OutKey(S As String, B As Key, ByVal KeyL As Integer)   ' display a key value
Debug.Print: Debug.Print S;
For i = 0 To KeyL - 1
   Hex B.K(i)
Next i
End Sub

Private Sub OutBlock(S As String, B As InOut)   ' display an input/output block
Debug.Print: Debug.Print S;
For i = 0 To 15
   Hex B.IO(i)
Next i
End Sub

Rem The following Main routine should output the following in the immediate window:
Rem Variable Key Length ( 16 )
Rem Key =            00000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = 66e94bd4ef8a2c3b884cfa59ca342b2e
Rem Decrypted Text = 00000000000000000000000000000000
Rem Variable Key Length ( 24 )
Rem Key =            000000000000000000000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = aae06992acbf52a3e8f4a96ec9300bd7
Rem Decrypted Text = 00000000000000000000000000000000
Rem Variable Key Length ( 32 )
Rem Key =            0000000000000000000000000000000000000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = dc95c078a2408989ad48a21492842087
Rem Decrypted Text = 00000000000000000000000000000000
Rem Fixed Key Length ( 128 )
Rem Key =            00000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = 66e94bd4ef8a2c3b884cfa59ca342b2e
Rem Decrypted Text = 00000000000000000000000000000000
Rem Fixed Key Length ( 192 )
Rem Key =            000000000000000000000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = aae06992acbf52a3e8f4a96ec9300bd7
Rem Decrypted Text = 00000000000000000000000000000000
Rem Fixed Key Length ( 256 )
Rem Key =            0000000000000000000000000000000000000000000000000000000000000000
Rem Input =          00000000000000000000000000000000
Rem Encrypted Text = dc95c078a2408989ad48a21492842087
Rem Decrypted Text = 00000000000000000000000000000000

Sub Main()
Dim Key As Key                                  ' these variables are automatically
Dim Ib As InOut, Ob As InOut, Rb As InOut       ' initialised to zero values in VBA
Dim Ecx As EncCtx
Dim Dcx As DecCtx
Dim RetVal As Integer

For KeyL = 16 To 32 Step 8
Debug.Print "Variable Key Length ("; KeyL; ")";
OutKey "Key =            ", Key, KeyL
OutBlock "Input =          ", Ib
RetVal = AesEncryptKey(Key, KeyL, Ecx)          ' set an all zero encryption key
RetVal = AesEncrypt(Ib, Ob, Ecx)                ' encrypt Ib to Ob
OutBlock "Encrypted Text = ", Ob
RetVal = AesDecryptKey(Key, KeyL, Dcx)          ' set an all zero decryption key
RetVal = AesDecrypt(Ob, Rb, Dcx)                ' decrypt Ob to Rb
OutBlock "Decrypted Text = ", Rb
Debug.Print
Next KeyL

KeyL = 128: Debug.Print "Fixed Key Length ("; KeyL; ")";
OutKey "Key =            ", Key, 16
OutBlock "Input =          ", Ib
RetVal = AesEncryptKey128(Key, Ecx)             ' set an all zero encryption key
RetVal = AesEncrypt(Ib, Ob, Ecx)                ' encrypt Ib to Ob
OutBlock "Encrypted Text = ", Ob
RetVal = AesDecryptKey128(Key, Dcx)             ' set an all zero decryption key
RetVal = AesDecrypt(Ob, Rb, Dcx)                ' decrypt Ob to Rb
OutBlock "Decrypted Text = ", Rb
Debug.Print

KeyL = 192: Debug.Print "Fixed Key Length ("; KeyL; ")";
OutKey "Key =            ", Key, 24
OutBlock "Input =          ", Ib
RetVal = AesEncryptKey192(Key, Ecx)             ' set an all zero encryption key
RetVal = AesEncrypt(Ib, Ob, Ecx)                ' encrypt Ib to Ob
OutBlock "Encrypted Text = ", Ob
RetVal = AesDecryptKey192(Key, Dcx)             ' set an all zero decryption key
RetVal = AesDecrypt(Ob, Rb, Dcx)                ' decrypt Ob to Rb
OutBlock "Decrypted Text = ", Rb
Debug.Print

KeyL = 256: Debug.Print "Fixed Key Length ("; KeyL; ")";
OutKey "Key =            ", Key, 32
OutBlock "Input =          ", Ib
RetVal = AesEncryptKey256(Key, Ecx)             ' set an all zero encryption key
RetVal = AesEncrypt(Ib, Ob, Ecx)                ' encrypt Ib to Ob
OutBlock "Encrypted Text = ", Ob
RetVal = AesDecryptKey256(Key, Dcx)             ' set an all zero decryption key
RetVal = AesDecrypt(Ob, Rb, Dcx)                ' decrypt Ob to Rb
OutBlock "Decrypted Text = ", Rb
Debug.Print

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -