VB下的AES加密问题,请高手进来看看

tw9902 2010-12-02 05:08:35
现在有个用AES加密的任务,从网上找了些例子来看,发现跟网事用C#加密出来的并不一样,如:
需加密字符串为:12345678
C#串加密后为:K69oVPiPEuyxLICXl0f2og==
而VB中加密后为:xwVjY5ZjYxhjY8MHYyOaYw==或者为:F69F305ED8047F906DB98141763A470D
现在的问题是怎么才能在VB中加密成C#中的那样?
请高手指教!

注:密钥都一样。
...全文
1030 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2010-12-06
  • 打赏
  • 举报
回复

//
to_utf16_little_string ( base64_encode ( aes256_encode_ecb ( ansi_string ( "12345678" ) , ansi_string ( "123" ) ) ) )
//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00 34 00 35 00 36 00 37 00 38 00
//--------
output = 31 32 33 34 35 36 37 38
//

//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00
//--------
output = 31 32 33
//

//------
aes 256 ecb encrypt
//------
key = 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//------
plain = 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 cipher = 6C 7A 86 71 B5 AF 3C E3 C3 D8 1F 06 58 97 ED B5
//

//----
base64 encode make data to string
//----
input = 6C 7A 86 71 B5 AF 3C E3 C3 D8 1F 06 58 97 ED B5
//----
output = 62 00 48 00 71 00 47 00 63 00 62 00 57 00 76 00 50 00 4F 00 50 00 44 00 32 00 42 00 38 00 47 00 57 00 4A 00 66 00 74 00 74 00 51 00 3D 00 3D 00
//

//--
to_utf16_little_string convert utf16 little endian format input data to string
//--
input = 62 00 48 00 71 00 47 00 63 00 62 00 57 00 76 00 50 00 4F 00 50 00 44 00 32 00 42 00 38 00 47 00 57 00 4A 00 66 00 74 00 74 00 51 00 3D 00 3D 00
//--
output = "bHqGcbWvPOPD2B8GWJfttQ=="
//
用户 昵称 2010-12-06
  • 打赏
  • 举报
回复
俺用字符串 "123"右边填充全00,对字符串"1234578"加密,并进行base64运算,没碰上楼主所说的结果。


//
to_utf16_little_string ( base64_encode ( aes128_encode_ecb ( ansi_string ( "12345678" ) , ansi_string ( "123" ) ) ) )
//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00 34 00 35 00 36 00 37 00 38 00
//--------
output = 31 32 33 34 35 36 37 38
//

//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00
//--------
output = 31 32 33
//

//------
aes 128 ecb encrypt
//------
key = 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00
//------
plain = 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 cipher = F3 2E 19 9B B1 35 09 58 E2 7C 0B 80 F1 10 A6 CB
//

//----
base64 encode make data to string
//----
input = F3 2E 19 9B B1 35 09 58 E2 7C 0B 80 F1 10 A6 CB
//----
output = 38 00 79 00 34 00 5A 00 6D 00 37 00 45 00 31 00 43 00 56 00 6A 00 69 00 66 00 41 00 75 00 41 00 38 00 52 00 43 00 6D 00 79 00 77 00 3D 00 3D 00
//

//--
to_utf16_little_string convert utf16 little endian format input data to string
//--
input = 38 00 79 00 34 00 5A 00 6D 00 37 00 45 00 31 00 43 00 56 00 6A 00 69 00 66 00 41 00 75 00 41 00 38 00 52 00 43 00 6D 00 79 00 77 00 3D 00 3D 00
//--
output = "8y4Zm7E1CVjifAuA8RCmyw=="
//

//
to_utf16_little_string ( base64_encode ( aes192_encode_ecb ( ansi_string ( "12345678" ) , ansi_string ( "123" ) ) ) )
//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00 34 00 35 00 36 00 37 00 38 00
//--------
output = 31 32 33 34 35 36 37 38
//

//--------
ansi_string convert utf16-little-endian format input data to ansi format
//--------
input = 31 00 32 00 33 00
//--------
output = 31 32 33
//

//------
aes 192 ecb encrypt
//------
key = 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//------
plain = 31 32 33 34 35 36 37 38 00 00 00 00 00 00 00 00 cipher = 38 99 64 3C D0 DA E7 1C B7 87 82 18 66 3C 87 F2
//

//----
base64 encode make data to string
//----
input = 38 99 64 3C D0 DA E7 1C B7 87 82 18 66 3C 87 F2
//----
output = 4F 00 4A 00 6C 00 6B 00 50 00 4E 00 44 00 61 00 35 00 78 00 79 00 33 00 68 00 34 00 49 00 59 00 5A 00 6A 00 79 00 48 00 38 00 67 00 3D 00 3D 00
//

//--
to_utf16_little_string convert utf16 little endian format input data to string
//--
input = 4F 00 4A 00 6C 00 6B 00 50 00 4E 00 44 00 61 00 35 00 78 00 79 00 33 00 68 00 34 00 49 00 59 00 5A 00 6A 00 79 00 48 00 38 00 67 00 3D 00 3D 00
//--
output = "OJlkPNDa5xy3h4IYZjyH8g=="
//
tw9902 2010-12-03
  • 打赏
  • 举报
回复
'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
tw9902 2010-12-03
  • 打赏
  • 举报
回复
窗体:
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
tw9902 2010-12-03
  • 打赏
  • 举报
回复
我把VB的源码发过来
choovy123 2010-12-03
  • 打赏
  • 举报
回复
来围观一下 友情提示:没有细节高手也不知道你问题在哪
tw9902 2010-12-03
  • 打赏
  • 举报
回复
是AES加密的 至于左填充还是右填充这个就不清楚了,VB的例子是在网上找的,感觉VB进行AES加密好麻烦哦
of123 2010-12-03
  • 打赏
  • 举报
回复

你是如何将分组填充到 16 字节的?填充内容是什么?左填充还是右填充?
用户 昵称 2010-12-02
  • 打赏
  • 举报
回复
这是aes? aes是加解密至少有两个必须的参数,密钥 + 数据,你这太不像是aes了。

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧