VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "AES"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'**********************************************
' 文件名: aes.cls
' 版本: Advanced Encryption StAndard Class Version 1.0 Build20051108
' 作者: yyj
' 电子邮件: fly_yyj@hotmail.com
' 日期: 2005年11月08日
' 声明: AES数据加密/解密类
'**********************************************
'**********************************************
' 类的实现
Private FSBox(15, 15)
Private FIsBox(15, 15)
Private FRcon(10, 3)
Private FNb, FNk, FNr
Private FKey(31)
Private FW(59, 3)
Private FState(3, 3)
'**********************************************
' 输入:keySize密钥长度(128、192、256),Src明文字符串,Key密钥字符串
' 明文字符串不能超过 &HFFFF长度
' 输出:密文十六进制字符串
Public Function CipherStrToHexStr(ByVal keysize As Integer, ByVal Src As String, ByVal key As String)
SetNbNkNr keysize
InitKey key
Dim i, result, str32, input0(15), output(15)
Dim HexString, iLen, sLen
iLen = Len(Src)
sLen = CStr(Hex(iLen))
sLen = String(4 - Len(sLen), "0") & sLen
HexString = sLen & HexStr(Src)
result = ""
i = 0
str32 = Mid(HexString, 1, 32)
Do While Len(str32) > 0
HexStrToArray str32, input0
FCipher input0, output
result = result + ArrayToHexStr(output)
i = i + Len(str32)
str32 = Mid(HexString, i + 1, 32)
Loop
CipherStrToHexStr = result
End Function
'**********************************************
' 输入:keySize密钥长度(128、192、256),Src明文十六进制符串,Key密钥字符串
' 明文十六进制字符串不能超过 2 * &HFFFF长度
' 输出:密文十六进制字符串
Public Function CipherHexStrToHexStr(ByVal keysize As Integer, ByVal Src As String, ByVal key As String)
SetNbNkNr keysize
InitKey key
Dim i, result, str32, input0(15), output(15)
Dim HexString, iLen, sLen
iLen = Len(Src) \ 2
If iLen > 2 * &HFFFF Then Src = Left(Src, 2 * &HFFFF)
sLen = CStr(Hex(iLen))
sLen = String(4 - Len(sLen), "0") & sLen
HexString = sLen & Src
result = ""
i = 0
str32 = Mid(HexString, 1, 32)
Do While Len(str32) > 0
HexStrToArray str32, input0
FCipher input0, output
result = result + ArrayToHexStr(output)
i = i + Len(str32)
str32 = Mid(HexString, i + 1, 32)
Loop
CipherHexStrToHexStr = result
End Function
'**********************************************
' 输入:keySize密钥长度(128、192、256),Src密文十六进制符串,Key密钥字符串
' 输出:解密后的字符串
Public Function InvCipherHexStrToStr(keysize, Src, key)
SetNbNkNr keysize
InitKey key
Dim i, result, str32, input0(15), output(15)
Dim HexString, iLen, sLen, str
HexString = Src
result = ""
i = 0
str32 = Mid(HexString, 1, 32)
i = i + Len(str32)
HexStrToArray str32, input0
FInvCipher input0, output
str = ArrayToHexStr(output)
sLen = Left(str, 4)
iLen = HexToLng(sLen)
str = ArrayToStr(output)
result = Right(str, 7)
str32 = Mid(HexString, i + 1, 32)
Do While Len(str32) > 0
HexStrToArray str32, input0
FInvCipher input0, output
result = result + ArrayToStr(output)
i = i + Len(str32)
str32 = Mid(HexString, i + 1, 32)
Loop
InvCipherHexStrToStr = Left(result, iLen)
End Function
'**********************************************
' 输入:keySize密钥长度(128、192、256),Src密文十六进制符串,Key密钥字符串
' 输出:解密后的十六进制字符串
Public Function InvCipherHexStrToHexStr(keysize, Src, key)
SetNbNkNr keysize
InitKey key
Dim i, result, str32, input0(15), output(15)
Dim HexString, iLen, sLen, str
HexString = Src
result = ""
i = 0
str32 = Mid(HexString, 1, 32)
i = i + Len(str32)
HexStrToArray str32, input0 //这句何解
FInvCipher input0, output
str = ArrayToHexStr(output)
sLen = Left(str, 4)
iLen = HexToLng(sLen)
result = Right(str, 28)
str32 = Mid(HexString, i + 1, 32)
Do While Len(str32) > 0
HexStrToArray str32, input0
FInvCipher input0, output
result = result + ArrayToHexStr(output)
i = i + Len(str32)
str32 = Mid(HexString, i + 1, 32)
Loop
InvCipherHexStrToHexStr = Left(result, iLen * 4)
End Function
Private Function ArrayToHexStr(Src)
Dim i, result
result = ""
For i = LBound(Src) To UBound(Src)
result = result & CStr(MyHex(Src(i)))
Next
ArrayToHexStr = result
End Function
Private Function ArrayToStr(Src)
Dim i, result
result = ""
For i = LBound(Src) To UBound(Src) \ 2
result = result & ChrW(Src(2 * i) + Src(2 * i + 1) * &H100)
Next
ArrayToStr = result
End Function
Private Function HexStr(Src)
Dim i, HexString
For i = 0 To LenB(Src) - 1
HexString = HexString & CStr(MyHex(AscB(MidB(Src, i + 1, 1))))
Next
HexStr = HexString
End Function
Private Function HexToLng(H)
HexToLng = CLng(CStr("&H" & H))
End Function
Private Sub HexStrToArray(Src, Out)
If IsNull(Src) Then Src = ""
Dim W, i, j
i = 0
j = 0
For i = 0 To Len(Src) \ 2 - 1
Out(i) = HexToLng(Mid(Src, 2 * i + 1, 2))
Next
For i = Len(Src) \ 2 To 15
Out(i) = 0
Next
End Sub
Private Function C2Byte(B)
C2Byte = B And &HFF
End Function
Private Function MyHex(B)
If B < &H10 Then
MyHex = "0" & CStr(Hex(B))
Else
MyHex = CStr(Hex(B))
End If
End Function
'**********************************************
' 初始化工作Key,如果Key中包含Unicode 字符,则仅取Unicode字符的低字节
Private Sub InitKey(key)
Dim i
Dim j
Dim K
For i = 0 To 31
FKey(i) = 0
Next
If Len(key) > FNk * 4 Then
For i = 0 To FNk * 4 - 1
K = AscW(Mid(key, i + 1, 1))
If K > &HFF Then K = C2Byte(K)
FKey(i) = K
Next
Else
For i = 0 To Len(key) - 1
K = AscW(Mid(key, i + 1, 1))
If K > &HFF Then K = C2Byte(K)
FKey(i) = K
Next
End If
KeyExpansion
End Sub
Private Sub Class_Initialize()
Call BuildSBox
Call BuildIsBox
Call BuildRcon
End Sub
Private Sub SetNbNkNr(keysize)
FNb = 4
Select Case keysize
Case 192
FNk = 6
FNr = 12
Case 256
FNk = 8
FNr = 14
Case Else '别的都按128 处理
FNk = 4
FNr = 10
End Select
End Sub
Private Sub AddRoundKey(around)
Dim r, c
For r = 0 To 3
For c = 0 To 3
FState(r, c) = C2Byte((CLng(FState(r, c)) Xor (FW((around * 4) + c, r))))
Next
Next
End Sub
13 个解决方案
#1
Private Sub KeyExpansion()
Dim row
Dim temp(3)
Dim i
For row = 0 To FNk - 1
FW(row, 0) = FKey(4 * row)
FW(row, 1) = FKey(4 * row + 1)
FW(row, 2) = FKey(4 * row + 2)
FW(row, 3) = FKey(4 * row + 3)
Next
For row = FNk To FNb * (FNr + 1) - 1
temp(0) = FW(row - 1, 0)
temp(1) = FW(row - 1, 1)
temp(2) = FW(row - 1, 2)
temp(3) = FW(row - 1, 3)
If row Mod FNk = 0 Then
RotWord temp(0), temp(1), temp(2), temp(3)
SubWord temp(0), temp(1), temp(2), temp(3)
temp(0) = C2Byte((CLng(temp(0))) Xor (CLng(FRcon(row \ FNk, 0))))
temp(1) = C2Byte((CLng(temp(1))) Xor (CLng(FRcon(row \ FNk, 1))))
temp(2) = C2Byte((CLng(temp(2))) Xor (CLng(FRcon(row \ FNk, 2))))
temp(3) = C2Byte((CLng(temp(3))) Xor (CLng(FRcon(row \ FNk, 3))))
Else
If (FNk > 6) And ((row Mod FNk) = 4) Then
SubWord temp(0), temp(1), temp(2), temp(3)
End If
End If
FW(row, 0) = C2Byte((CLng(FW(row - FNk, 0))) Xor (CLng(temp(0))))
FW(row, 1) = C2Byte((CLng(FW(row - FNk, 1))) Xor (CLng(temp(1))))
FW(row, 2) = C2Byte((CLng(FW(row - FNk, 2))) Xor (CLng(temp(2))))
FW(row, 3) = C2Byte((CLng(FW(row - FNk, 3))) Xor (CLng(temp(3))))
Next
End Sub
Private Sub SubBytes()
Dim r, c
For r = 0 To 3
For c = 0 To 3
FState(r, c) = FSBox(FState(r, c) \ 16, FState(r, c) And &HF)
Next
Next
End Sub
Private Sub InvSubBytes()
Dim r, c
For r = 0 To 3
For c = 0 To 3
FState(r, c) = FIsBox(FState(r, c) \ 16, FState(r, c) And &HF)
Next
Next
End Sub
Private Sub ShIftRows()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For r = 1 To 3
For c = 0 To 3
FState(r, c) = temp(r, (c + r) Mod FNb)
Next
Next
End Sub
Private Sub InvShIftRows()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For r = 1 To 3
For c = 0 To 3
FState(r, (c + r) Mod FNb) = temp(r, c)
Next
Next
End Sub
Private Sub MixColumns()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For c = 0 To 3
FState(0, c) = C2Byte(CInt(gfmultby02(temp(0, c))) Xor CInt(gfmultby03(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
FState(1, c) = C2Byte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby02(temp(1, c))) Xor CInt(gfmultby03(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
FState(2, c) = C2Byte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby02(temp(2, c))) Xor CInt(gfmultby03(temp(3, c))))
FState(3, c) = C2Byte(CInt(gfmultby03(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby02(temp(3, c))))
Next
End Sub
Private Sub InvMixColumns()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For c = 0 To 3
FState(0, c) = C2Byte(CInt(gfmultby0e(temp(0, c))) Xor CInt(gfmultby0b(temp(1, c))) Xor CInt(gfmultby0d(temp(2, c))) Xor CInt(gfmultby09(temp(3, c))))
FState(1, c) = C2Byte(CInt(gfmultby09(temp(0, c))) Xor CInt(gfmultby0e(temp(1, c))) Xor CInt(gfmultby0b(temp(2, c))) Xor CInt(gfmultby0d(temp(3, c))))
FState(2, c) = C2Byte(CInt(gfmultby0d(temp(0, c))) Xor CInt(gfmultby09(temp(1, c))) Xor CInt(gfmultby0e(temp(2, c))) Xor CInt(gfmultby0b(temp(3, c))))
FState(3, c) = C2Byte(CInt(gfmultby0b(temp(0, c))) Xor CInt(gfmultby0d(temp(1, c))) Xor CInt(gfmultby09(temp(2, c))) Xor CInt(gfmultby0e(temp(3, c))))
Next
End Sub
Private Function gfmultby01(B)
gfmultby01 = B
End Function
Private Function gfmultby02(B)
If (B < &H80) Then
gfmultby02 = C2Byte(CInt(B * 2))
Else
gfmultby02 = C2Byte((CInt(B * 2)) Xor (CInt(&H1B)))
End If
End Function
Private Function gfmultby03(B)
gfmultby03 = C2Byte((CInt(gfmultby02(B))) Xor (CInt(B)))
End Function
Private Function gfmultby09(B)
gfmultby09 = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(B)))
End Function
Private Function gfmultby0b(B)
gfmultby0b = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(B))) Xor (CInt(B)))
End Function
Private Function gfmultby0d(B)
gfmultby0d = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(gfmultby02(B)))) Xor (CInt(B)))
End Function
Private Function gfmultby0e(B)
gfmultby0e = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(gfmultby02(B)))) Xor (CInt(gfmultby02(B))))
End Function
Private Sub SubWord(B1, B2, B3, B4)
B4 = FSBox(B4 \ 16, B4 And &HF)
B3 = FSBox(B3 \ 16, B3 And &HF)
B2 = FSBox(B2 \ 16, B2 And &HF)
B1 = FSBox(B1 \ 16, B1 And &HF)
End Sub
Private Sub RotWord(B1, B2, B3, B4)
Dim B
B = B1
B1 = B2
B2 = B3
B3 = B4
B4 = B
End Sub
Private Sub FCipher(input0, output)
Dim i, around
For i = 0 To 4 * FNb - 1
FState(i Mod 4, i \ 4) = input0(i)
Next
AddRoundKey 0
For around = 1 To FNr - 1
Call SubBytes
Call ShIftRows
Call MixColumns
AddRoundKey around
Next
Call SubBytes
Call ShIftRows
AddRoundKey FNr
For i = 0 To FNb * 4 - 1
output(i) = FState(i Mod 4, i \ 4)
Next
End Sub
Private Sub FInvCipher(input0, output)
Dim i, around
For i = 0 To 4 * FNb - 1
FState(i Mod 4, i \ 4) = input0(i)
Next
AddRoundKey FNr
around = FNr - 1
Do While around >= 1
Call InvShIftRows
Call InvSubBytes
AddRoundKey around
Call InvMixColumns
around = around - 1
Loop
Call InvShIftRows
Call InvSubBytes
AddRoundKey 0
For i = 0 To FNb * 4 - 1
output(i) = FState(i Mod 4, i \ 4)
Next
End Sub
#2
Private Function BuildSBox()
FSBox(0, 0) = &H63
FSBox(0, 1) = &H7C
FSBox(0, 2) = &H77
FSBox(0, 3) = &H7B
FSBox(0, 4) = &HF2
FSBox(0, 5) = &H6B
FSBox(0, 6) = &H6F
FSBox(0, 7) = &HC5
FSBox(0, 8) = &H30
FSBox(0, 9) = &H1
FSBox(0, 10) = &H67
FSBox(0, 11) = &H2B
FSBox(0, 12) = &HFE
FSBox(0, 13) = &HD7
FSBox(0, 14) = &HAB
FSBox(0, 15) = &H76
FSBox(1, 0) = &HCA
FSBox(1, 1) = &H82
FSBox(1, 2) = &HC9
FSBox(1, 3) = &H7D
FSBox(1, 4) = &HFA
FSBox(1, 5) = &H59
FSBox(1, 6) = &H47
FSBox(1, 7) = &HF0
FSBox(1, 8) = &HAD
FSBox(1, 9) = &HD4
FSBox(1, 10) = &HA2
FSBox(1, 11) = &HAF
FSBox(1, 12) = &H9C
FSBox(1, 13) = &HA4
FSBox(1, 14) = &H72
FSBox(1, 15) = &HC0
FSBox(2, 0) = &HB7
FSBox(2, 1) = &HFD
FSBox(2, 2) = &H93
FSBox(2, 3) = &H26
FSBox(2, 4) = &H36
FSBox(2, 5) = &H3F
FSBox(2, 6) = &HF7
FSBox(2, 7) = &HCC
FSBox(2, 8) = &H34
FSBox(2, 9) = &HA5
FSBox(2, 10) = &HE5
FSBox(2, 11) = &HF1
FSBox(2, 12) = &H71
FSBox(2, 13) = &HD8
FSBox(2, 14) = &H31
FSBox(2, 15) = &H15
FSBox(3, 0) = &H4
FSBox(3, 1) = &HC7
FSBox(3, 2) = &H23
FSBox(3, 3) = &HC3
FSBox(3, 4) = &H18
FSBox(3, 5) = &H96
FSBox(3, 6) = &H5
FSBox(3, 7) = &H9A
FSBox(3, 8) = &H7
FSBox(3, 9) = &H12
FSBox(3, 10) = &H80
FSBox(3, 11) = &HE2
FSBox(3, 12) = &HEB
FSBox(3, 13) = &H27
FSBox(3, 14) = &HB2
FSBox(3, 15) = &H75
FSBox(4, 0) = &H9
FSBox(4, 1) = &H83
FSBox(4, 2) = &H2C
FSBox(4, 3) = &H1A
FSBox(4, 4) = &H1B
FSBox(4, 5) = &H6E
FSBox(4, 6) = &H5A
FSBox(4, 7) = &HA0
FSBox(4, 8) = &H52
FSBox(4, 9) = &H3B
FSBox(4, 10) = &HD6
FSBox(4, 11) = &HB3
FSBox(4, 12) = &H29
FSBox(4, 13) = &HE3
FSBox(4, 14) = &H2F
FSBox(4, 15) = &H84
FSBox(5, 0) = &H53
FSBox(5, 1) = &HD1
FSBox(5, 2) = &H0
FSBox(5, 3) = &HED
FSBox(5, 4) = &H20
FSBox(5, 5) = &HFC
FSBox(5, 6) = &HB1
FSBox(5, 7) = &H5B
FSBox(5, 8) = &H6A
FSBox(5, 9) = &HCB
FSBox(5, 10) = &HBE
FSBox(5, 11) = &H39
FSBox(5, 12) = &H4A
FSBox(5, 13) = &H4C
FSBox(5, 14) = &H58
FSBox(5, 15) = &HCF
FSBox(6, 0) = &HD0
FSBox(6, 1) = &HEF
FSBox(6, 2) = &HAA
FSBox(6, 3) = &HFB
FSBox(6, 4) = &H43
FSBox(6, 5) = &H4D
FSBox(6, 6) = &H33
FSBox(6, 7) = &H85
FSBox(6, 8) = &H45
FSBox(6, 9) = &HF9
FSBox(6, 10) = &H2
FSBox(6, 11) = &H7F
FSBox(6, 12) = &H50
FSBox(6, 13) = &H3C
FSBox(6, 14) = &H9F
FSBox(6, 15) = &HA8
FSBox(7, 0) = &H51
FSBox(7, 1) = &HA3
FSBox(7, 2) = &H40
FSBox(7, 3) = &H8F
FSBox(7, 4) = &H92
FSBox(7, 5) = &H9D
FSBox(7, 6) = &H38
FSBox(7, 7) = &HF5
FSBox(7, 8) = &HBC
FSBox(7, 9) = &HB6
FSBox(7, 10) = &HDA
FSBox(7, 11) = &H21
FSBox(7, 12) = &H10
FSBox(7, 13) = &HFF
FSBox(7, 14) = &HF3
FSBox(7, 15) = &HD2
FSBox(8, 0) = &HCD
FSBox(8, 1) = &HC
FSBox(8, 2) = &H13
FSBox(8, 3) = &HEC
FSBox(8, 4) = &H5F
FSBox(8, 5) = &H97
FSBox(8, 6) = &H44
FSBox(8, 7) = &H17
FSBox(8, 8) = &HC4
FSBox(8, 9) = &HA7
FSBox(8, 10) = &H7E
FSBox(8, 11) = &H3D
FSBox(8, 12) = &H64
FSBox(8, 13) = &H5D
FSBox(8, 14) = &H19
FSBox(8, 15) = &H73
FSBox(9, 0) = &H60
FSBox(9, 1) = &H81
FSBox(9, 2) = &H4F
FSBox(9, 3) = &HDC
FSBox(9, 4) = &H22
FSBox(9, 5) = &H2A
FSBox(9, 6) = &H90
FSBox(9, 7) = &H88
FSBox(9, 8) = &H46
FSBox(9, 9) = &HEE
FSBox(9, 10) = &HB8
FSBox(9, 11) = &H14
FSBox(9, 12) = &HDE
FSBox(9, 13) = &H5E
FSBox(9, 14) = &HB
FSBox(9, 15) = &HDB
FSBox(10, 0) = &HE0
FSBox(10, 1) = &H32
FSBox(10, 2) = &H3A
FSBox(10, 3) = &HA
FSBox(10, 4) = &H49
FSBox(10, 5) = &H6
FSBox(10, 6) = &H24
FSBox(10, 7) = &H5C
FSBox(10, 8) = &HC2
FSBox(10, 9) = &HD3
FSBox(10, 10) = &HAC
FSBox(10, 11) = &H62
FSBox(10, 12) = &H91
FSBox(10, 13) = &H95
FSBox(10, 14) = &HE4
FSBox(10, 15) = &H79
FSBox(11, 0) = &HE7
FSBox(11, 1) = &HC8
FSBox(11, 2) = &H37
FSBox(11, 3) = &H6D
FSBox(11, 4) = &H8D
FSBox(11, 5) = &HD5
FSBox(11, 6) = &H4E
FSBox(11, 7) = &HA9
FSBox(11, 8) = &H6C
FSBox(11, 9) = &H56
FSBox(11, 10) = &HF4
FSBox(11, 11) = &HEA
FSBox(11, 12) = &H65
FSBox(11, 13) = &H7A
FSBox(11, 14) = &HAE
FSBox(11, 15) = &H8
FSBox(12, 0) = &HBA
FSBox(12, 1) = &H78
FSBox(12, 2) = &H25
FSBox(12, 3) = &H2E
FSBox(12, 4) = &H1C
FSBox(12, 5) = &HA6
FSBox(12, 6) = &HB4
FSBox(12, 7) = &HC6
FSBox(12, 8) = &HE8
FSBox(12, 9) = &HDD
FSBox(12, 10) = &H74
FSBox(12, 11) = &H1F
FSBox(12, 12) = &H4B
FSBox(12, 13) = &HBD
FSBox(12, 14) = &H8B
FSBox(12, 15) = &H8A
FSBox(13, 0) = &H70
FSBox(13, 1) = &H3E
FSBox(13, 2) = &HB5
FSBox(13, 3) = &H66
FSBox(13, 4) = &H48
FSBox(13, 5) = &H3
FSBox(13, 6) = &HF6
FSBox(13, 7) = &HE
FSBox(13, 8) = &H61
FSBox(13, 9) = &H35
FSBox(13, 10) = &H57
FSBox(13, 11) = &HB9
FSBox(13, 12) = &H86
FSBox(13, 13) = &HC1
FSBox(13, 14) = &H1D
FSBox(13, 15) = &H9E
FSBox(14, 0) = &HE1
FSBox(14, 1) = &HF8
FSBox(14, 2) = &H98
FSBox(14, 3) = &H11
FSBox(14, 4) = &H69
FSBox(14, 5) = &HD9
FSBox(14, 6) = &H8E
FSBox(14, 7) = &H94
FSBox(14, 8) = &H9B
FSBox(14, 9) = &H1E
FSBox(14, 10) = &H87
FSBox(14, 11) = &HE9
FSBox(14, 12) = &HCE
FSBox(14, 13) = &H55
FSBox(14, 14) = &H28
FSBox(14, 15) = &HDF
FSBox(15, 0) = &H8C
FSBox(15, 1) = &HA1
FSBox(15, 2) = &H89
FSBox(15, 3) = &HD
FSBox(15, 4) = &HBF
FSBox(15, 5) = &HE6
FSBox(15, 6) = &H42
FSBox(15, 7) = &H68
FSBox(15, 8) = &H41
FSBox(15, 9) = &H99
FSBox(15, 10) = &H2D
FSBox(15, 11) = &HF
FSBox(15, 12) = &HB0
FSBox(15, 13) = &H54
FSBox(15, 14) = &HBB
FSBox(15, 15) = &H16
End Function
#3
Private Function BuildIsBox()
FIsBox(0, 0) = &H52
FIsBox(0, 1) = &H9
FIsBox(0, 2) = &H6A
FIsBox(0, 3) = &HD5
FIsBox(0, 4) = &H30
FIsBox(0, 5) = &H36
FIsBox(0, 6) = &HA5
FIsBox(0, 7) = &H38
FIsBox(0, 8) = &HBF
FIsBox(0, 9) = &H40
FIsBox(0, 10) = &HA3
FIsBox(0, 11) = &H9E
FIsBox(0, 12) = &H81
FIsBox(0, 13) = &HF3
FIsBox(0, 14) = &HD7
FIsBox(0, 15) = &HFB
FIsBox(1, 0) = &H7C
FIsBox(1, 1) = &HE3
FIsBox(1, 2) = &H39
FIsBox(1, 3) = &H82
FIsBox(1, 4) = &H9B
FIsBox(1, 5) = &H2F
FIsBox(1, 6) = &HFF
FIsBox(1, 7) = &H87
FIsBox(1, 8) = &H34
FIsBox(1, 9) = &H8E
FIsBox(1, 10) = &H43
FIsBox(1, 11) = &H44
FIsBox(1, 12) = &HC4
FIsBox(1, 13) = &HDE
FIsBox(1, 14) = &HE9
FIsBox(1, 15) = &HCB
FIsBox(2, 0) = &H54
FIsBox(2, 1) = &H7B
FIsBox(2, 2) = &H94
FIsBox(2, 3) = &H32
FIsBox(2, 4) = &HA6
FIsBox(2, 5) = &HC2
FIsBox(2, 6) = &H23
FIsBox(2, 7) = &H3D
FIsBox(2, 8) = &HEE
FIsBox(2, 9) = &H4C
FIsBox(2, 10) = &H95
FIsBox(2, 11) = &HB
FIsBox(2, 12) = &H42
FIsBox(2, 13) = &HFA
FIsBox(2, 14) = &HC3
FIsBox(2, 15) = &H4E
FIsBox(3, 0) = &H8
FIsBox(3, 1) = &H2E
FIsBox(3, 2) = &HA1
FIsBox(3, 3) = &H66
FIsBox(3, 4) = &H28
FIsBox(3, 5) = &HD9
FIsBox(3, 6) = &H24
FIsBox(3, 7) = &HB2
FIsBox(3, 8) = &H76
FIsBox(3, 9) = &H5B
FIsBox(3, 10) = &HA2
FIsBox(3, 11) = &H49
FIsBox(3, 12) = &H6D
FIsBox(3, 13) = &H8B
FIsBox(3, 14) = &HD1
FIsBox(3, 15) = &H25
FIsBox(4, 0) = &H72
FIsBox(4, 1) = &HF8
FIsBox(4, 2) = &HF6
FIsBox(4, 3) = &H64
FIsBox(4, 4) = &H86
FIsBox(4, 5) = &H68
FIsBox(4, 6) = &H98
FIsBox(4, 7) = &H16
FIsBox(4, 8) = &HD4
FIsBox(4, 9) = &HA4
FIsBox(4, 10) = &H5C
FIsBox(4, 11) = &HCC
FIsBox(4, 12) = &H5D
FIsBox(4, 13) = &H65
FIsBox(4, 14) = &HB6
FIsBox(4, 15) = &H92
FIsBox(5, 0) = &H6C
FIsBox(5, 1) = &H70
FIsBox(5, 2) = &H48
FIsBox(5, 3) = &H50
FIsBox(5, 4) = &HFD
FIsBox(5, 5) = &HED
FIsBox(5, 6) = &HB9
FIsBox(5, 7) = &HDA
FIsBox(5, 8) = &H5E
FIsBox(5, 9) = &H15
FIsBox(5, 10) = &H46
FIsBox(5, 11) = &H57
FIsBox(5, 12) = &HA7
FIsBox(5, 13) = &H8D
FIsBox(5, 14) = &H9D
FIsBox(5, 15) = &H84
FIsBox(6, 0) = &H90
FIsBox(6, 1) = &HD8
FIsBox(6, 2) = &HAB
FIsBox(6, 3) = &H0
FIsBox(6, 4) = &H8C
FIsBox(6, 5) = &HBC
FIsBox(6, 6) = &HD3
FIsBox(6, 7) = &HA
FIsBox(6, 8) = &HF7
FIsBox(6, 9) = &HE4
FIsBox(6, 10) = &H58
FIsBox(6, 11) = &H5
FIsBox(6, 12) = &HB8
FIsBox(6, 13) = &HB3
FIsBox(6, 14) = &H45
FIsBox(6, 15) = &H6
FIsBox(7, 0) = &HD0
FIsBox(7, 1) = &H2C
FIsBox(7, 2) = &H1E
FIsBox(7, 3) = &H8F
FIsBox(7, 4) = &HCA
FIsBox(7, 5) = &H3F
FIsBox(7, 6) = &HF
FIsBox(7, 7) = &H2
FIsBox(7, 8) = &HC1
FIsBox(7, 9) = &HAF
FIsBox(7, 10) = &HBD
FIsBox(7, 11) = &H3
FIsBox(7, 12) = &H1
FIsBox(7, 13) = &H13
FIsBox(7, 14) = &H8A
FIsBox(7, 15) = &H6B
FIsBox(8, 0) = &H3A
FIsBox(8, 1) = &H91
FIsBox(8, 2) = &H11
FIsBox(8, 3) = &H41
FIsBox(8, 4) = &H4F
FIsBox(8, 5) = &H67
FIsBox(8, 6) = &HDC
FIsBox(8, 7) = &HEA
FIsBox(8, 8) = &H97
FIsBox(8, 9) = &HF2
FIsBox(8, 10) = &HCF
FIsBox(8, 11) = &HCE
FIsBox(8, 12) = &HF0
FIsBox(8, 13) = &HB4
FIsBox(8, 14) = &HE6
FIsBox(8, 15) = &H73
FIsBox(9, 0) = &H96
FIsBox(9, 1) = &HAC
FIsBox(9, 2) = &H74
FIsBox(9, 3) = &H22
FIsBox(9, 4) = &HE7
FIsBox(9, 5) = &HAD
FIsBox(9, 6) = &H35
FIsBox(9, 7) = &H85
FIsBox(9, 8) = &HE2
FIsBox(9, 9) = &HF9
FIsBox(9, 10) = &H37
FIsBox(9, 11) = &HE8
FIsBox(9, 12) = &H1C
FIsBox(9, 13) = &H75
FIsBox(9, 14) = &HDF
FIsBox(9, 15) = &H6E
FIsBox(10, 0) = &H47
FIsBox(10, 1) = &HF1
FIsBox(10, 2) = &H1A
FIsBox(10, 3) = &H71
FIsBox(10, 4) = &H1D
FIsBox(10, 5) = &H29
FIsBox(10, 6) = &HC5
FIsBox(10, 7) = &H89
FIsBox(10, 8) = &H6F
FIsBox(10, 9) = &HB7
FIsBox(10, 10) = &H62
FIsBox(10, 11) = &HE
FIsBox(10, 12) = &HAA
FIsBox(10, 13) = &H18
FIsBox(10, 14) = &HBE
FIsBox(10, 15) = &H1B
FIsBox(11, 0) = &HFC
FIsBox(11, 1) = &H56
FIsBox(11, 2) = &H3E
FIsBox(11, 3) = &H4B
FIsBox(11, 4) = &HC6
FIsBox(11, 5) = &HD2
FIsBox(11, 6) = &H79
FIsBox(11, 7) = &H20
FIsBox(11, 8) = &H9A
FIsBox(11, 9) = &HDB
FIsBox(11, 10) = &HC0
FIsBox(11, 11) = &HFE
FIsBox(11, 12) = &H78
FIsBox(11, 13) = &HCD
FIsBox(11, 14) = &H5A
FIsBox(11, 15) = &HF4
FIsBox(12, 0) = &H1F
FIsBox(12, 1) = &HDD
FIsBox(12, 2) = &HA8
FIsBox(12, 3) = &H33
FIsBox(12, 4) = &H88
FIsBox(12, 5) = &H7
FIsBox(12, 6) = &HC7
FIsBox(12, 7) = &H31
FIsBox(12, 8) = &HB1
FIsBox(12, 9) = &H12
FIsBox(12, 10) = &H10
FIsBox(12, 11) = &H59
FIsBox(12, 12) = &H27
FIsBox(12, 13) = &H80
FIsBox(12, 14) = &HEC
FIsBox(12, 15) = &H5F
FIsBox(13, 0) = &H60
FIsBox(13, 1) = &H51
FIsBox(13, 2) = &H7F
FIsBox(13, 3) = &HA9
FIsBox(13, 4) = &H19
FIsBox(13, 5) = &HB5
FIsBox(13, 6) = &H4A
FIsBox(13, 7) = &HD
FIsBox(13, 8) = &H2D
FIsBox(13, 9) = &HE5
FIsBox(13, 10) = &H7A
FIsBox(13, 11) = &H9F
FIsBox(13, 12) = &H93
FIsBox(13, 13) = &HC9
FIsBox(13, 14) = &H9C
FIsBox(13, 15) = &HEF
FIsBox(14, 0) = &HA0
FIsBox(14, 1) = &HE0
FIsBox(14, 2) = &H3B
FIsBox(14, 3) = &H4D
FIsBox(14, 4) = &HAE
FIsBox(14, 5) = &H2A
FIsBox(14, 6) = &HF5
FIsBox(14, 7) = &HB0
FIsBox(14, 8) = &HC8
FIsBox(14, 9) = &HEB
FIsBox(14, 10) = &HBB
FIsBox(14, 11) = &H3C
FIsBox(14, 12) = &H83
FIsBox(14, 13) = &H53
FIsBox(14, 14) = &H99
FIsBox(14, 15) = &H61
FIsBox(15, 0) = &H17
FIsBox(15, 1) = &H2B
FIsBox(15, 2) = &H4
FIsBox(15, 3) = &H7E
FIsBox(15, 4) = &HBA
FIsBox(15, 5) = &H77
FIsBox(15, 6) = &HD6
FIsBox(15, 7) = &H26
FIsBox(15, 8) = &HE1
FIsBox(15, 9) = &H69
FIsBox(15, 10) = &H14
FIsBox(15, 11) = &H63
FIsBox(15, 12) = &H55
FIsBox(15, 13) = &H21
FIsBox(15, 14) = &HC
FIsBox(15, 15) = &H7D
End Function
Private Function BuildRcon()
FRcon(0, 0) = &H0
FRcon(0, 1) = &H0
FRcon(0, 2) = &H0
FRcon(0, 3) = &H0
FRcon(1, 0) = &H1
FRcon(1, 1) = &H0
FRcon(1, 2) = &H0
FRcon(1, 3) = &H0
FRcon(2, 0) = &H2
FRcon(2, 1) = &H0
FRcon(2, 2) = &H0
FRcon(2, 3) = &H0
FRcon(3, 0) = &H4
FRcon(3, 1) = &H0
FRcon(3, 2) = &H0
FRcon(3, 3) = &H0
FRcon(4, 0) = &H8
FRcon(4, 1) = &H0
FRcon(4, 2) = &H0
FRcon(4, 3) = &H0
FRcon(5, 0) = &H10
FRcon(5, 1) = &H0
FRcon(5, 2) = &H0
FRcon(5, 3) = &H0
FRcon(6, 0) = &H20
FRcon(6, 1) = &H0
FRcon(6, 2) = &H0
FRcon(6, 3) = &H0
FRcon(7, 0) = &H40
FRcon(7, 1) = &H0
FRcon(7, 2) = &H0
FRcon(7, 3) = &H0
FRcon(8, 0) = &H80
FRcon(8, 1) = &H0
FRcon(8, 2) = &H0
FRcon(8, 3) = &H0
FRcon(9, 0) = &H1B
FRcon(9, 1) = &H0
FRcon(9, 2) = &H0
FRcon(9, 3) = &H0
FRcon(10, 0) = &H36
FRcon(10, 1) = &H0
FRcon(10, 2) = &H0
FRcon(10, 3) = &H0
End Function
#4
#5
高手们帮帮忙啊
#6
/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">被加密的明文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] Cryptograph = null; // 加密后的密文
Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流
using (MemoryStream Memory = new MemoryStream())
{
// 把内存流对象包装成加密流对象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文数据写入加密流
Encryptor.Write(Data, 0, Data.Length);
Encryptor.FlushFinalBlock();
Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}
return Cryptograph;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">被解密的密文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] original = null; // 解密后的明文
Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流,存储密文
using (MemoryStream Memory = new MemoryStream(Data))
{
// 把内存流对象包装成加密流对象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存储区
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}
return original;
}
#7
楼上的大哥,我在vb中没有找到向量,能帮我看下吗?没有弄清楚向量解密不出来,谢谢了
#8
你要VB代码还是C#代码?
#9
楼上大哥,我要C#的
#10
#11
那位VB高手能帮忙看看代码中用于AES加密的向量(IV)是什么吗?因为不知道向量值解不了密,我要用C#解密,谢谢
#12
顶都没人顶一下啊
#13
不太会vb只会一点点c#和java
#1
Private Sub KeyExpansion()
Dim row
Dim temp(3)
Dim i
For row = 0 To FNk - 1
FW(row, 0) = FKey(4 * row)
FW(row, 1) = FKey(4 * row + 1)
FW(row, 2) = FKey(4 * row + 2)
FW(row, 3) = FKey(4 * row + 3)
Next
For row = FNk To FNb * (FNr + 1) - 1
temp(0) = FW(row - 1, 0)
temp(1) = FW(row - 1, 1)
temp(2) = FW(row - 1, 2)
temp(3) = FW(row - 1, 3)
If row Mod FNk = 0 Then
RotWord temp(0), temp(1), temp(2), temp(3)
SubWord temp(0), temp(1), temp(2), temp(3)
temp(0) = C2Byte((CLng(temp(0))) Xor (CLng(FRcon(row \ FNk, 0))))
temp(1) = C2Byte((CLng(temp(1))) Xor (CLng(FRcon(row \ FNk, 1))))
temp(2) = C2Byte((CLng(temp(2))) Xor (CLng(FRcon(row \ FNk, 2))))
temp(3) = C2Byte((CLng(temp(3))) Xor (CLng(FRcon(row \ FNk, 3))))
Else
If (FNk > 6) And ((row Mod FNk) = 4) Then
SubWord temp(0), temp(1), temp(2), temp(3)
End If
End If
FW(row, 0) = C2Byte((CLng(FW(row - FNk, 0))) Xor (CLng(temp(0))))
FW(row, 1) = C2Byte((CLng(FW(row - FNk, 1))) Xor (CLng(temp(1))))
FW(row, 2) = C2Byte((CLng(FW(row - FNk, 2))) Xor (CLng(temp(2))))
FW(row, 3) = C2Byte((CLng(FW(row - FNk, 3))) Xor (CLng(temp(3))))
Next
End Sub
Private Sub SubBytes()
Dim r, c
For r = 0 To 3
For c = 0 To 3
FState(r, c) = FSBox(FState(r, c) \ 16, FState(r, c) And &HF)
Next
Next
End Sub
Private Sub InvSubBytes()
Dim r, c
For r = 0 To 3
For c = 0 To 3
FState(r, c) = FIsBox(FState(r, c) \ 16, FState(r, c) And &HF)
Next
Next
End Sub
Private Sub ShIftRows()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For r = 1 To 3
For c = 0 To 3
FState(r, c) = temp(r, (c + r) Mod FNb)
Next
Next
End Sub
Private Sub InvShIftRows()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For r = 1 To 3
For c = 0 To 3
FState(r, (c + r) Mod FNb) = temp(r, c)
Next
Next
End Sub
Private Sub MixColumns()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For c = 0 To 3
FState(0, c) = C2Byte(CInt(gfmultby02(temp(0, c))) Xor CInt(gfmultby03(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
FState(1, c) = C2Byte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby02(temp(1, c))) Xor CInt(gfmultby03(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
FState(2, c) = C2Byte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby02(temp(2, c))) Xor CInt(gfmultby03(temp(3, c))))
FState(3, c) = C2Byte(CInt(gfmultby03(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby02(temp(3, c))))
Next
End Sub
Private Sub InvMixColumns()
Dim temp(3, 3)
Dim r, c
For r = 0 To 3
For c = 0 To 3
temp(r, c) = FState(r, c)
Next
Next
For c = 0 To 3
FState(0, c) = C2Byte(CInt(gfmultby0e(temp(0, c))) Xor CInt(gfmultby0b(temp(1, c))) Xor CInt(gfmultby0d(temp(2, c))) Xor CInt(gfmultby09(temp(3, c))))
FState(1, c) = C2Byte(CInt(gfmultby09(temp(0, c))) Xor CInt(gfmultby0e(temp(1, c))) Xor CInt(gfmultby0b(temp(2, c))) Xor CInt(gfmultby0d(temp(3, c))))
FState(2, c) = C2Byte(CInt(gfmultby0d(temp(0, c))) Xor CInt(gfmultby09(temp(1, c))) Xor CInt(gfmultby0e(temp(2, c))) Xor CInt(gfmultby0b(temp(3, c))))
FState(3, c) = C2Byte(CInt(gfmultby0b(temp(0, c))) Xor CInt(gfmultby0d(temp(1, c))) Xor CInt(gfmultby09(temp(2, c))) Xor CInt(gfmultby0e(temp(3, c))))
Next
End Sub
Private Function gfmultby01(B)
gfmultby01 = B
End Function
Private Function gfmultby02(B)
If (B < &H80) Then
gfmultby02 = C2Byte(CInt(B * 2))
Else
gfmultby02 = C2Byte((CInt(B * 2)) Xor (CInt(&H1B)))
End If
End Function
Private Function gfmultby03(B)
gfmultby03 = C2Byte((CInt(gfmultby02(B))) Xor (CInt(B)))
End Function
Private Function gfmultby09(B)
gfmultby09 = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(B)))
End Function
Private Function gfmultby0b(B)
gfmultby0b = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(B))) Xor (CInt(B)))
End Function
Private Function gfmultby0d(B)
gfmultby0d = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(gfmultby02(B)))) Xor (CInt(B)))
End Function
Private Function gfmultby0e(B)
gfmultby0e = C2Byte((CInt(gfmultby02(gfmultby02(gfmultby02(B))))) Xor (CInt(gfmultby02(gfmultby02(B)))) Xor (CInt(gfmultby02(B))))
End Function
Private Sub SubWord(B1, B2, B3, B4)
B4 = FSBox(B4 \ 16, B4 And &HF)
B3 = FSBox(B3 \ 16, B3 And &HF)
B2 = FSBox(B2 \ 16, B2 And &HF)
B1 = FSBox(B1 \ 16, B1 And &HF)
End Sub
Private Sub RotWord(B1, B2, B3, B4)
Dim B
B = B1
B1 = B2
B2 = B3
B3 = B4
B4 = B
End Sub
Private Sub FCipher(input0, output)
Dim i, around
For i = 0 To 4 * FNb - 1
FState(i Mod 4, i \ 4) = input0(i)
Next
AddRoundKey 0
For around = 1 To FNr - 1
Call SubBytes
Call ShIftRows
Call MixColumns
AddRoundKey around
Next
Call SubBytes
Call ShIftRows
AddRoundKey FNr
For i = 0 To FNb * 4 - 1
output(i) = FState(i Mod 4, i \ 4)
Next
End Sub
Private Sub FInvCipher(input0, output)
Dim i, around
For i = 0 To 4 * FNb - 1
FState(i Mod 4, i \ 4) = input0(i)
Next
AddRoundKey FNr
around = FNr - 1
Do While around >= 1
Call InvShIftRows
Call InvSubBytes
AddRoundKey around
Call InvMixColumns
around = around - 1
Loop
Call InvShIftRows
Call InvSubBytes
AddRoundKey 0
For i = 0 To FNb * 4 - 1
output(i) = FState(i Mod 4, i \ 4)
Next
End Sub
#2
Private Function BuildSBox()
FSBox(0, 0) = &H63
FSBox(0, 1) = &H7C
FSBox(0, 2) = &H77
FSBox(0, 3) = &H7B
FSBox(0, 4) = &HF2
FSBox(0, 5) = &H6B
FSBox(0, 6) = &H6F
FSBox(0, 7) = &HC5
FSBox(0, 8) = &H30
FSBox(0, 9) = &H1
FSBox(0, 10) = &H67
FSBox(0, 11) = &H2B
FSBox(0, 12) = &HFE
FSBox(0, 13) = &HD7
FSBox(0, 14) = &HAB
FSBox(0, 15) = &H76
FSBox(1, 0) = &HCA
FSBox(1, 1) = &H82
FSBox(1, 2) = &HC9
FSBox(1, 3) = &H7D
FSBox(1, 4) = &HFA
FSBox(1, 5) = &H59
FSBox(1, 6) = &H47
FSBox(1, 7) = &HF0
FSBox(1, 8) = &HAD
FSBox(1, 9) = &HD4
FSBox(1, 10) = &HA2
FSBox(1, 11) = &HAF
FSBox(1, 12) = &H9C
FSBox(1, 13) = &HA4
FSBox(1, 14) = &H72
FSBox(1, 15) = &HC0
FSBox(2, 0) = &HB7
FSBox(2, 1) = &HFD
FSBox(2, 2) = &H93
FSBox(2, 3) = &H26
FSBox(2, 4) = &H36
FSBox(2, 5) = &H3F
FSBox(2, 6) = &HF7
FSBox(2, 7) = &HCC
FSBox(2, 8) = &H34
FSBox(2, 9) = &HA5
FSBox(2, 10) = &HE5
FSBox(2, 11) = &HF1
FSBox(2, 12) = &H71
FSBox(2, 13) = &HD8
FSBox(2, 14) = &H31
FSBox(2, 15) = &H15
FSBox(3, 0) = &H4
FSBox(3, 1) = &HC7
FSBox(3, 2) = &H23
FSBox(3, 3) = &HC3
FSBox(3, 4) = &H18
FSBox(3, 5) = &H96
FSBox(3, 6) = &H5
FSBox(3, 7) = &H9A
FSBox(3, 8) = &H7
FSBox(3, 9) = &H12
FSBox(3, 10) = &H80
FSBox(3, 11) = &HE2
FSBox(3, 12) = &HEB
FSBox(3, 13) = &H27
FSBox(3, 14) = &HB2
FSBox(3, 15) = &H75
FSBox(4, 0) = &H9
FSBox(4, 1) = &H83
FSBox(4, 2) = &H2C
FSBox(4, 3) = &H1A
FSBox(4, 4) = &H1B
FSBox(4, 5) = &H6E
FSBox(4, 6) = &H5A
FSBox(4, 7) = &HA0
FSBox(4, 8) = &H52
FSBox(4, 9) = &H3B
FSBox(4, 10) = &HD6
FSBox(4, 11) = &HB3
FSBox(4, 12) = &H29
FSBox(4, 13) = &HE3
FSBox(4, 14) = &H2F
FSBox(4, 15) = &H84
FSBox(5, 0) = &H53
FSBox(5, 1) = &HD1
FSBox(5, 2) = &H0
FSBox(5, 3) = &HED
FSBox(5, 4) = &H20
FSBox(5, 5) = &HFC
FSBox(5, 6) = &HB1
FSBox(5, 7) = &H5B
FSBox(5, 8) = &H6A
FSBox(5, 9) = &HCB
FSBox(5, 10) = &HBE
FSBox(5, 11) = &H39
FSBox(5, 12) = &H4A
FSBox(5, 13) = &H4C
FSBox(5, 14) = &H58
FSBox(5, 15) = &HCF
FSBox(6, 0) = &HD0
FSBox(6, 1) = &HEF
FSBox(6, 2) = &HAA
FSBox(6, 3) = &HFB
FSBox(6, 4) = &H43
FSBox(6, 5) = &H4D
FSBox(6, 6) = &H33
FSBox(6, 7) = &H85
FSBox(6, 8) = &H45
FSBox(6, 9) = &HF9
FSBox(6, 10) = &H2
FSBox(6, 11) = &H7F
FSBox(6, 12) = &H50
FSBox(6, 13) = &H3C
FSBox(6, 14) = &H9F
FSBox(6, 15) = &HA8
FSBox(7, 0) = &H51
FSBox(7, 1) = &HA3
FSBox(7, 2) = &H40
FSBox(7, 3) = &H8F
FSBox(7, 4) = &H92
FSBox(7, 5) = &H9D
FSBox(7, 6) = &H38
FSBox(7, 7) = &HF5
FSBox(7, 8) = &HBC
FSBox(7, 9) = &HB6
FSBox(7, 10) = &HDA
FSBox(7, 11) = &H21
FSBox(7, 12) = &H10
FSBox(7, 13) = &HFF
FSBox(7, 14) = &HF3
FSBox(7, 15) = &HD2
FSBox(8, 0) = &HCD
FSBox(8, 1) = &HC
FSBox(8, 2) = &H13
FSBox(8, 3) = &HEC
FSBox(8, 4) = &H5F
FSBox(8, 5) = &H97
FSBox(8, 6) = &H44
FSBox(8, 7) = &H17
FSBox(8, 8) = &HC4
FSBox(8, 9) = &HA7
FSBox(8, 10) = &H7E
FSBox(8, 11) = &H3D
FSBox(8, 12) = &H64
FSBox(8, 13) = &H5D
FSBox(8, 14) = &H19
FSBox(8, 15) = &H73
FSBox(9, 0) = &H60
FSBox(9, 1) = &H81
FSBox(9, 2) = &H4F
FSBox(9, 3) = &HDC
FSBox(9, 4) = &H22
FSBox(9, 5) = &H2A
FSBox(9, 6) = &H90
FSBox(9, 7) = &H88
FSBox(9, 8) = &H46
FSBox(9, 9) = &HEE
FSBox(9, 10) = &HB8
FSBox(9, 11) = &H14
FSBox(9, 12) = &HDE
FSBox(9, 13) = &H5E
FSBox(9, 14) = &HB
FSBox(9, 15) = &HDB
FSBox(10, 0) = &HE0
FSBox(10, 1) = &H32
FSBox(10, 2) = &H3A
FSBox(10, 3) = &HA
FSBox(10, 4) = &H49
FSBox(10, 5) = &H6
FSBox(10, 6) = &H24
FSBox(10, 7) = &H5C
FSBox(10, 8) = &HC2
FSBox(10, 9) = &HD3
FSBox(10, 10) = &HAC
FSBox(10, 11) = &H62
FSBox(10, 12) = &H91
FSBox(10, 13) = &H95
FSBox(10, 14) = &HE4
FSBox(10, 15) = &H79
FSBox(11, 0) = &HE7
FSBox(11, 1) = &HC8
FSBox(11, 2) = &H37
FSBox(11, 3) = &H6D
FSBox(11, 4) = &H8D
FSBox(11, 5) = &HD5
FSBox(11, 6) = &H4E
FSBox(11, 7) = &HA9
FSBox(11, 8) = &H6C
FSBox(11, 9) = &H56
FSBox(11, 10) = &HF4
FSBox(11, 11) = &HEA
FSBox(11, 12) = &H65
FSBox(11, 13) = &H7A
FSBox(11, 14) = &HAE
FSBox(11, 15) = &H8
FSBox(12, 0) = &HBA
FSBox(12, 1) = &H78
FSBox(12, 2) = &H25
FSBox(12, 3) = &H2E
FSBox(12, 4) = &H1C
FSBox(12, 5) = &HA6
FSBox(12, 6) = &HB4
FSBox(12, 7) = &HC6
FSBox(12, 8) = &HE8
FSBox(12, 9) = &HDD
FSBox(12, 10) = &H74
FSBox(12, 11) = &H1F
FSBox(12, 12) = &H4B
FSBox(12, 13) = &HBD
FSBox(12, 14) = &H8B
FSBox(12, 15) = &H8A
FSBox(13, 0) = &H70
FSBox(13, 1) = &H3E
FSBox(13, 2) = &HB5
FSBox(13, 3) = &H66
FSBox(13, 4) = &H48
FSBox(13, 5) = &H3
FSBox(13, 6) = &HF6
FSBox(13, 7) = &HE
FSBox(13, 8) = &H61
FSBox(13, 9) = &H35
FSBox(13, 10) = &H57
FSBox(13, 11) = &HB9
FSBox(13, 12) = &H86
FSBox(13, 13) = &HC1
FSBox(13, 14) = &H1D
FSBox(13, 15) = &H9E
FSBox(14, 0) = &HE1
FSBox(14, 1) = &HF8
FSBox(14, 2) = &H98
FSBox(14, 3) = &H11
FSBox(14, 4) = &H69
FSBox(14, 5) = &HD9
FSBox(14, 6) = &H8E
FSBox(14, 7) = &H94
FSBox(14, 8) = &H9B
FSBox(14, 9) = &H1E
FSBox(14, 10) = &H87
FSBox(14, 11) = &HE9
FSBox(14, 12) = &HCE
FSBox(14, 13) = &H55
FSBox(14, 14) = &H28
FSBox(14, 15) = &HDF
FSBox(15, 0) = &H8C
FSBox(15, 1) = &HA1
FSBox(15, 2) = &H89
FSBox(15, 3) = &HD
FSBox(15, 4) = &HBF
FSBox(15, 5) = &HE6
FSBox(15, 6) = &H42
FSBox(15, 7) = &H68
FSBox(15, 8) = &H41
FSBox(15, 9) = &H99
FSBox(15, 10) = &H2D
FSBox(15, 11) = &HF
FSBox(15, 12) = &HB0
FSBox(15, 13) = &H54
FSBox(15, 14) = &HBB
FSBox(15, 15) = &H16
End Function
#3
Private Function BuildIsBox()
FIsBox(0, 0) = &H52
FIsBox(0, 1) = &H9
FIsBox(0, 2) = &H6A
FIsBox(0, 3) = &HD5
FIsBox(0, 4) = &H30
FIsBox(0, 5) = &H36
FIsBox(0, 6) = &HA5
FIsBox(0, 7) = &H38
FIsBox(0, 8) = &HBF
FIsBox(0, 9) = &H40
FIsBox(0, 10) = &HA3
FIsBox(0, 11) = &H9E
FIsBox(0, 12) = &H81
FIsBox(0, 13) = &HF3
FIsBox(0, 14) = &HD7
FIsBox(0, 15) = &HFB
FIsBox(1, 0) = &H7C
FIsBox(1, 1) = &HE3
FIsBox(1, 2) = &H39
FIsBox(1, 3) = &H82
FIsBox(1, 4) = &H9B
FIsBox(1, 5) = &H2F
FIsBox(1, 6) = &HFF
FIsBox(1, 7) = &H87
FIsBox(1, 8) = &H34
FIsBox(1, 9) = &H8E
FIsBox(1, 10) = &H43
FIsBox(1, 11) = &H44
FIsBox(1, 12) = &HC4
FIsBox(1, 13) = &HDE
FIsBox(1, 14) = &HE9
FIsBox(1, 15) = &HCB
FIsBox(2, 0) = &H54
FIsBox(2, 1) = &H7B
FIsBox(2, 2) = &H94
FIsBox(2, 3) = &H32
FIsBox(2, 4) = &HA6
FIsBox(2, 5) = &HC2
FIsBox(2, 6) = &H23
FIsBox(2, 7) = &H3D
FIsBox(2, 8) = &HEE
FIsBox(2, 9) = &H4C
FIsBox(2, 10) = &H95
FIsBox(2, 11) = &HB
FIsBox(2, 12) = &H42
FIsBox(2, 13) = &HFA
FIsBox(2, 14) = &HC3
FIsBox(2, 15) = &H4E
FIsBox(3, 0) = &H8
FIsBox(3, 1) = &H2E
FIsBox(3, 2) = &HA1
FIsBox(3, 3) = &H66
FIsBox(3, 4) = &H28
FIsBox(3, 5) = &HD9
FIsBox(3, 6) = &H24
FIsBox(3, 7) = &HB2
FIsBox(3, 8) = &H76
FIsBox(3, 9) = &H5B
FIsBox(3, 10) = &HA2
FIsBox(3, 11) = &H49
FIsBox(3, 12) = &H6D
FIsBox(3, 13) = &H8B
FIsBox(3, 14) = &HD1
FIsBox(3, 15) = &H25
FIsBox(4, 0) = &H72
FIsBox(4, 1) = &HF8
FIsBox(4, 2) = &HF6
FIsBox(4, 3) = &H64
FIsBox(4, 4) = &H86
FIsBox(4, 5) = &H68
FIsBox(4, 6) = &H98
FIsBox(4, 7) = &H16
FIsBox(4, 8) = &HD4
FIsBox(4, 9) = &HA4
FIsBox(4, 10) = &H5C
FIsBox(4, 11) = &HCC
FIsBox(4, 12) = &H5D
FIsBox(4, 13) = &H65
FIsBox(4, 14) = &HB6
FIsBox(4, 15) = &H92
FIsBox(5, 0) = &H6C
FIsBox(5, 1) = &H70
FIsBox(5, 2) = &H48
FIsBox(5, 3) = &H50
FIsBox(5, 4) = &HFD
FIsBox(5, 5) = &HED
FIsBox(5, 6) = &HB9
FIsBox(5, 7) = &HDA
FIsBox(5, 8) = &H5E
FIsBox(5, 9) = &H15
FIsBox(5, 10) = &H46
FIsBox(5, 11) = &H57
FIsBox(5, 12) = &HA7
FIsBox(5, 13) = &H8D
FIsBox(5, 14) = &H9D
FIsBox(5, 15) = &H84
FIsBox(6, 0) = &H90
FIsBox(6, 1) = &HD8
FIsBox(6, 2) = &HAB
FIsBox(6, 3) = &H0
FIsBox(6, 4) = &H8C
FIsBox(6, 5) = &HBC
FIsBox(6, 6) = &HD3
FIsBox(6, 7) = &HA
FIsBox(6, 8) = &HF7
FIsBox(6, 9) = &HE4
FIsBox(6, 10) = &H58
FIsBox(6, 11) = &H5
FIsBox(6, 12) = &HB8
FIsBox(6, 13) = &HB3
FIsBox(6, 14) = &H45
FIsBox(6, 15) = &H6
FIsBox(7, 0) = &HD0
FIsBox(7, 1) = &H2C
FIsBox(7, 2) = &H1E
FIsBox(7, 3) = &H8F
FIsBox(7, 4) = &HCA
FIsBox(7, 5) = &H3F
FIsBox(7, 6) = &HF
FIsBox(7, 7) = &H2
FIsBox(7, 8) = &HC1
FIsBox(7, 9) = &HAF
FIsBox(7, 10) = &HBD
FIsBox(7, 11) = &H3
FIsBox(7, 12) = &H1
FIsBox(7, 13) = &H13
FIsBox(7, 14) = &H8A
FIsBox(7, 15) = &H6B
FIsBox(8, 0) = &H3A
FIsBox(8, 1) = &H91
FIsBox(8, 2) = &H11
FIsBox(8, 3) = &H41
FIsBox(8, 4) = &H4F
FIsBox(8, 5) = &H67
FIsBox(8, 6) = &HDC
FIsBox(8, 7) = &HEA
FIsBox(8, 8) = &H97
FIsBox(8, 9) = &HF2
FIsBox(8, 10) = &HCF
FIsBox(8, 11) = &HCE
FIsBox(8, 12) = &HF0
FIsBox(8, 13) = &HB4
FIsBox(8, 14) = &HE6
FIsBox(8, 15) = &H73
FIsBox(9, 0) = &H96
FIsBox(9, 1) = &HAC
FIsBox(9, 2) = &H74
FIsBox(9, 3) = &H22
FIsBox(9, 4) = &HE7
FIsBox(9, 5) = &HAD
FIsBox(9, 6) = &H35
FIsBox(9, 7) = &H85
FIsBox(9, 8) = &HE2
FIsBox(9, 9) = &HF9
FIsBox(9, 10) = &H37
FIsBox(9, 11) = &HE8
FIsBox(9, 12) = &H1C
FIsBox(9, 13) = &H75
FIsBox(9, 14) = &HDF
FIsBox(9, 15) = &H6E
FIsBox(10, 0) = &H47
FIsBox(10, 1) = &HF1
FIsBox(10, 2) = &H1A
FIsBox(10, 3) = &H71
FIsBox(10, 4) = &H1D
FIsBox(10, 5) = &H29
FIsBox(10, 6) = &HC5
FIsBox(10, 7) = &H89
FIsBox(10, 8) = &H6F
FIsBox(10, 9) = &HB7
FIsBox(10, 10) = &H62
FIsBox(10, 11) = &HE
FIsBox(10, 12) = &HAA
FIsBox(10, 13) = &H18
FIsBox(10, 14) = &HBE
FIsBox(10, 15) = &H1B
FIsBox(11, 0) = &HFC
FIsBox(11, 1) = &H56
FIsBox(11, 2) = &H3E
FIsBox(11, 3) = &H4B
FIsBox(11, 4) = &HC6
FIsBox(11, 5) = &HD2
FIsBox(11, 6) = &H79
FIsBox(11, 7) = &H20
FIsBox(11, 8) = &H9A
FIsBox(11, 9) = &HDB
FIsBox(11, 10) = &HC0
FIsBox(11, 11) = &HFE
FIsBox(11, 12) = &H78
FIsBox(11, 13) = &HCD
FIsBox(11, 14) = &H5A
FIsBox(11, 15) = &HF4
FIsBox(12, 0) = &H1F
FIsBox(12, 1) = &HDD
FIsBox(12, 2) = &HA8
FIsBox(12, 3) = &H33
FIsBox(12, 4) = &H88
FIsBox(12, 5) = &H7
FIsBox(12, 6) = &HC7
FIsBox(12, 7) = &H31
FIsBox(12, 8) = &HB1
FIsBox(12, 9) = &H12
FIsBox(12, 10) = &H10
FIsBox(12, 11) = &H59
FIsBox(12, 12) = &H27
FIsBox(12, 13) = &H80
FIsBox(12, 14) = &HEC
FIsBox(12, 15) = &H5F
FIsBox(13, 0) = &H60
FIsBox(13, 1) = &H51
FIsBox(13, 2) = &H7F
FIsBox(13, 3) = &HA9
FIsBox(13, 4) = &H19
FIsBox(13, 5) = &HB5
FIsBox(13, 6) = &H4A
FIsBox(13, 7) = &HD
FIsBox(13, 8) = &H2D
FIsBox(13, 9) = &HE5
FIsBox(13, 10) = &H7A
FIsBox(13, 11) = &H9F
FIsBox(13, 12) = &H93
FIsBox(13, 13) = &HC9
FIsBox(13, 14) = &H9C
FIsBox(13, 15) = &HEF
FIsBox(14, 0) = &HA0
FIsBox(14, 1) = &HE0
FIsBox(14, 2) = &H3B
FIsBox(14, 3) = &H4D
FIsBox(14, 4) = &HAE
FIsBox(14, 5) = &H2A
FIsBox(14, 6) = &HF5
FIsBox(14, 7) = &HB0
FIsBox(14, 8) = &HC8
FIsBox(14, 9) = &HEB
FIsBox(14, 10) = &HBB
FIsBox(14, 11) = &H3C
FIsBox(14, 12) = &H83
FIsBox(14, 13) = &H53
FIsBox(14, 14) = &H99
FIsBox(14, 15) = &H61
FIsBox(15, 0) = &H17
FIsBox(15, 1) = &H2B
FIsBox(15, 2) = &H4
FIsBox(15, 3) = &H7E
FIsBox(15, 4) = &HBA
FIsBox(15, 5) = &H77
FIsBox(15, 6) = &HD6
FIsBox(15, 7) = &H26
FIsBox(15, 8) = &HE1
FIsBox(15, 9) = &H69
FIsBox(15, 10) = &H14
FIsBox(15, 11) = &H63
FIsBox(15, 12) = &H55
FIsBox(15, 13) = &H21
FIsBox(15, 14) = &HC
FIsBox(15, 15) = &H7D
End Function
Private Function BuildRcon()
FRcon(0, 0) = &H0
FRcon(0, 1) = &H0
FRcon(0, 2) = &H0
FRcon(0, 3) = &H0
FRcon(1, 0) = &H1
FRcon(1, 1) = &H0
FRcon(1, 2) = &H0
FRcon(1, 3) = &H0
FRcon(2, 0) = &H2
FRcon(2, 1) = &H0
FRcon(2, 2) = &H0
FRcon(2, 3) = &H0
FRcon(3, 0) = &H4
FRcon(3, 1) = &H0
FRcon(3, 2) = &H0
FRcon(3, 3) = &H0
FRcon(4, 0) = &H8
FRcon(4, 1) = &H0
FRcon(4, 2) = &H0
FRcon(4, 3) = &H0
FRcon(5, 0) = &H10
FRcon(5, 1) = &H0
FRcon(5, 2) = &H0
FRcon(5, 3) = &H0
FRcon(6, 0) = &H20
FRcon(6, 1) = &H0
FRcon(6, 2) = &H0
FRcon(6, 3) = &H0
FRcon(7, 0) = &H40
FRcon(7, 1) = &H0
FRcon(7, 2) = &H0
FRcon(7, 3) = &H0
FRcon(8, 0) = &H80
FRcon(8, 1) = &H0
FRcon(8, 2) = &H0
FRcon(8, 3) = &H0
FRcon(9, 0) = &H1B
FRcon(9, 1) = &H0
FRcon(9, 2) = &H0
FRcon(9, 3) = &H0
FRcon(10, 0) = &H36
FRcon(10, 1) = &H0
FRcon(10, 2) = &H0
FRcon(10, 3) = &H0
End Function
#4
#5
高手们帮帮忙啊
#6
/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">被加密的明文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] Cryptograph = null; // 加密后的密文
Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流
using (MemoryStream Memory = new MemoryStream())
{
// 把内存流对象包装成加密流对象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文数据写入加密流
Encryptor.Write(Data, 0, Data.Length);
Encryptor.FlushFinalBlock();
Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}
return Cryptograph;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">被解密的密文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] original = null; // 解密后的明文
Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流,存储密文
using (MemoryStream Memory = new MemoryStream(Data))
{
// 把内存流对象包装成加密流对象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存储区
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}
return original;
}
#7
楼上的大哥,我在vb中没有找到向量,能帮我看下吗?没有弄清楚向量解密不出来,谢谢了
#8
你要VB代码还是C#代码?
#9
楼上大哥,我要C#的
#10
#11
那位VB高手能帮忙看看代码中用于AES加密的向量(IV)是什么吗?因为不知道向量值解不了密,我要用C#解密,谢谢
#12
顶都没人顶一下啊
#13
不太会vb只会一点点c#和java