我的问题实际上比仅在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


运行解密代码时,出现以下错误信息


填充无效,无法删除。

最佳答案

好的,所以我的原始代码有些错误。

首先,无需调试。在将其包装到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密码模式,这需要初始向量(IV)值。这给实现增加了一定程度的复杂性,直到其余部分100%都可以工作时我才准备添加。

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

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

10-16 06:27