7,763
社区成员
发帖
与我相关
我的任务
分享
'AES加密模块(附带Base64编码模块)
'状态:编写
'作者:沈超
'最后修改日期:2006-6-4
Public Enum BlockLength '分组及密钥的长度
Bits128 = 16
Bits192 = 24
Bits256 = 32
End Enum
Public Enum OperationType '操作方式:加密或者解密
Encrypt = 0
Decrypt = 1
End Enum
Public Const MAXBC = 8
Public Const MAXKC = 8
Public Const MAXROUND = 14
Public Const MAXBOUND = 255
'Private Nb As Integer
'Private Nk As Integer
'Private Nr As Integer
Private intRound As Integer
Private Keys() As Byte
Private Ept() As Byte
Private Block(4, MAXBC) As Byte
Private KeySize As BlockLength
Private BlockSize As BlockLength
Private Sbox(256) As Byte
Private iSbox(256) As Byte
Private W(4, 120) As Byte '密钥轮表
Private Rc(30) As Integer
Private Logtab(256) As Byte '用于GF(28)域乘法
Private Alogtab(256) As Byte '同Logtab
Private Shifts(5, 4) As Byte '位移表
Private NumRounds(5, 5) As Byte '轮数表
Private intBC As Integer, intKC As Integer, intRounds As Integer
'以下申明属于base64
Option Explicit
Private Const BASE64CHR As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
Private psBase64Chr(0 To 63) As String
Sub AESinit(Keylen As BlockLength, Blocklen As BlockLength)
'初始化AES各个置换表的值
Dim i As Long, J As Integer, K As Integer
Dim intTemp As Integer
Dim a(), b(), C(), d(), e(), f()
'------------初始化Sbox和iSbox置换盒,及GF(28)域乘法表
a = Array( _
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, _
118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, _
192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, _
49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, _
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, _
0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, _
170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, _
146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, _
95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, _
34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, _
36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, _
78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, _
180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, _
14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, _
148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, _
104, 65, 153, 45, 15, 176, 84, 187, 22)
C = Array( _
0, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3, 100, 4, 224, _
14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193, 125, 194, 29, _
181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120, 101, 47, 138, _
5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142, 150, 143, 219, 189, _
54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56, 102, 221, 253, 48, _
191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16, 126, 110, 72, 195, 163, _
182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186, 43, 121, 10, 21, 155, 159, _
94, 202, 78, 212, 172, 229, 243, 115, 167, 87, 175, 88, 168, 80, 244, 234, _
214, 116, 79, 174, 233, 213, 231, 230, 173, 232, 44, 215, 117, 122, 235, _
22, 11, 245, 89, 203, 95, 176, 156, 169, 81, 160, 127, 12, 246, 111, 23, 196, _
73, 236, 216, 67, 31, 45, 164, 118, 123, 183, 204, 187, 62, 90, 251, 96, _
177, 134, 59, 82, 161, 108, 170, 85, 41, 157, 151, 178, 135, 144, 97, 190, _
220, 252, 188, 149, 207, 205, 55, 63, 91, 209, 83, 57, 132, 60, 65, 162, 109, _
71, 20, 42, 158, 93, 86, 242, 211, 171, 68, 17, 146, 217, 35, 32, 46, 137, _
180, 124, 184, 38, 119, 153, 227, 165, 103, 74, 237, 222, 197, 49, 254, _
24, 13, 99, 140, 128, 192, 247, 112, 7)
d = Array( _
1, 3, 5, 15, 17, 51, 85, 255, 26, 46, 114, 150, 161, 248, 19, 53, 95, 225, _
56, 72, 216, 115, 149, 164, 247, 2, 6, 10, 30, 34, 102, 170, 229, 52, 92, _
228, 55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49, 83, 245, 4, 12, _
20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178, 205, 76, 212, 103, 169, _
224, 59, 77, 215, 98, 166, 241, 8, 24, 40, 120, 136, 131, 158, 185, 208, 107, _
189, 220, 127, 129, 152, 179, 206, 73, 219, 118, 154, 181, 196, 87, 249, _
16, 48, 80, 240, 11, 29, 39, 105, 187, 214, 97, 163, 254, 25, 43, 125, 135, _
146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160, 251, 22, 58, 78, 210, _
109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65, 195, 94, 226, 61, 71, 201, _
64, 192, 91, 237, 44, 116, 156, 191, 218, 117, 159, 186, 213, 100, 172, 239, _
42, 126, 130, 157, 188, 223, 122, 142, 137, 128, 155, 182, 193, 88, 232, _
35, 101, 175, 234, 37, 111, 177, 200, 67, 197, 84, 252, 31, 33, 99, 165, _
244, 7, 9, 27, 45, 119, 153, 176, 203, 70, 202, 69, 207, 74, 222, 121, 139, _
134, 145, 168, 227, 62, 66, 198, 81, 243, 14, 18, 54, 90, 238, 41, 123, 141, _
140, 143, 138, 133, 148, 167, 242, 13, 23, 57, 75, 221, 124, 132, 151, 162, _
253, 28, 36, 108, 180, 199, 82, 246, 1)
For i = 0 To MAXBOUND Step 1
Sbox(i) = a(i)
iSbox(a(i)) = i
Logtab(i) = C(i)
Alogtab(i) = d(i)
Next i
ReDim a(1): ReDim b(1): ReDim C(1): ReDim d(1)
'-------------------------------------------------------------------------------------
'------------初始化密钥扩展表RC
b = Array( _
&H0, &H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80, &H1B, &H36, _
&H6C, &HD8, &HAB, &H4D, &H9A, &H2F, &H5E, &HBC, &H63, &HC6, &H97, &H35, _
&H6A, &HD4, &HB3, &H7D, &HFA, &HEF, &HC5)
For i = 0 To 29
Rc(i) = b(i)
Next i
'------------------------------------------------------------------------------------
'--------------初始化位移表Shifts
C = Array(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 4, 0, 1, 3, 4)
K = 0
For i = 0 To 4 Step 1
For J = 0 To 3 Step 1
Shifts(i, J) = C(K)
K = K + 1
Next J
Next i
'-----------------------------------------------------------------------------------
'--------------初始化轮数表NumRounds
d = Array(10, 11, 12, 13, 14, 11, 11, 12, 13, 14, 12, 12, 12, 13, 14, _
13, 13, 13, 13, 14, 14, 14, 14, 14, 14)
K = 0
For i = 0 To 4 Step 1
For J = 0 To 4 Step 1
NumRounds(i, J) = d(K)
K = K + 1
Next J
Next i
intKC = Keylen / 4
intBC = Blocklen / 4
BlockSize = Blocklen
KeySize = Keylen
ReDim Keys(KeySize)
'-----------------------------------------------------------------------------------
End Sub
Private Sub Command1_Click()
Dim Temp() As Byte
Dim strInfo As String
Dim i As Integer, J As Integer, intK As Integer
strInfo = ""
strInfo = AES_Str_Encyrpt(txtShow.Text, "123")
'For i = 0 To UBound(Temp)
' strInfo = strInfo + Hex(Temp(i)) + " "
' If i Mod 15 = 0 And i <> 0 Then strInfo = strInfo + Chr(13) + Chr(10)
'Next i
txtDisp.Text = strInfo
End Sub
Private Sub Command2_Click()
Dim Str1 As String, Str2 As String
Str1 = AES_Str_Decyrpt(txtDisp.Text, "1312123")
'Str1 = EncodeBase64String(txtShow.Text)
txtDisp.Text = Str1
End Sub