gpt4 book ai didi

java - 在 VB.NET 中使用 AesCryptoServiceProvider

转载 作者:行者123 更新时间:2023-12-01 17:21:27 27 4
gpt4 key购买 nike

我的问题实际上比如何在 VB.NET 中使用 AES 更复杂一些,因为我真正想做的是在跨 JACOB 的 Java 应用程序中在 VB.NET 中使用 AES。但现在,我需要关注的是 AES 实现本身。

这是我的加密代码

Public Function EncryptAES(ByVal toEncrypt As String, ByVal key As String) As Byte()
Dim keyArray = Convert.FromBase64String(key)
Dim toEncryptArray = Encoding.Unicode.GetBytes(toEncrypt)

Dim aes = New AesCryptoServiceProvider
aes.Key = keyArray
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim encryptor = aes.CreateEncryptor()

Dim encrypted = encryptor.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
aes.Clear()

Return encrypted
End Function

回到 Java 代码后,我将字节数组转换为十六进制字符串。

现在,为了反转该过程,这是我的解密代码

Public Function DecryptAES(ByVal toDecrypt As String, ByVal key As String) As Byte()
Dim keyArray = Convert.FromBase64String(key)
Dim toDecryptArray = Convert.FromBase64String(toDecrypt)

Dim aes = New AesCryptoServiceProvider
aes.Key = keyArray
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim decryptor = aes.CreateDecryptor()

Dim decrypted = decryptor.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length)
aes.Clear()
Return decrypted
End Function

当我运行解密代码时,收到以下错误消息

Padding is invalid and cannot be removed.

最佳答案

好的,所以我的原始代码有一些问题。

其一,无需调试。我必须添加一个内部测试来验证我的代码在用 Java 包装之前是否可以加密和解密。我真是太傻了。

二:加密器和解密器对象的 TransformFinalBlock 方法似乎没有像宣传的那样执行。

说得够多了。这是代码

Public Function EncryptAES(ByVal clearText As String, ByVal key As String) As String

' prepare input
Dim keyBytes = Convert.FromBase64String(key)
Dim sourceBytes = Encoding.Unicode.GetBytes(clearText)

' prepare encryption provider
Dim aes = New AesCryptoServiceProvider
aes.Key = keyBytes
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim encryptor = aes.CreateEncryptor()

Dim ms = New MemoryStream
Dim cs = New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
cs.Write(sourceBytes, 0, sourceBytes.Length)
cs.FlushFinalBlock()
Dim encrypted = Convert.ToBase64String(ms.ToArray())

'Dim decrypted = DecryptAES(encrypted, key)
'If Not EventLog.SourceExists("CryptoBridge") Then
' EventLog.CreateEventSource("CryptoBridge", "CryptoBridge")
'End If
'Dim log As New EventLog
'log.Source = "CryptoBridge"
'log.WriteEntry(clearText & ":" & decrypted)

aes.Clear()
cs.Dispose()
ms.Dispose()

Return encrypted
End Function

Public Function DecryptAES(ByVal base64Cipher As String, ByVal key As String) As String
Dim keyBytes = Convert.FromBase64String(key)
Dim cipherBytes = Convert.FromBase64String(base64Cipher)

' prepare decryption provider
Dim aes = New AesCryptoServiceProvider
aes.Key = keyBytes
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.ISO10126
Dim decryptor = aes.CreateDecryptor()

Dim ms = New MemoryStream(cipherBytes)
Dim cs = New CryptoStream(ms, decryptor, CryptoStreamMode.Read)

Dim decryptedBytes As Byte()
ReDim decryptedBytes(cipherBytes.Length)

Dim readByteCount = cs.Read(decryptedBytes, 0, decryptedBytes.Length)
Dim decrypted = Encoding.Unicode.GetString(decryptedBytes, 0, readByteCount)

aes.Clear()
cs.Dispose()
ms.Dispose()

Return decrypted
End Function

接下来,我将修改此代码,将 AES 算法提升到 256 位和 CBC 密码模式,这需要初始 vector (IV) 值。这增加了实现的复杂性,直到其余部分 100% 工作时我才准备好添加。

感谢所有对我的问题发表评论的人。

关于java - 在 VB.NET 中使用 AesCryptoServiceProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/723129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com