ASP.NET Rijndael 解密错误:要解密的数据长度无效

发布于 2024-08-13 16:49:13 字数 3513 浏览 3 评论 0原文

好的,所以有 刚刚获得密钥 并得到一个 44 个字符长的加密字符串,我现在无法解密 (aarrgghh):

要解密的数据长度无效。

环顾四周并阅读了各种帖子后,看起来好像转换为 Base64String 可能是问题所在,但我看不出问题出在哪里 - 我看到的许多解决方案似乎都是相同的到我所拥有的。再次,我真的很感激任何帮助 - 摘录如下:

加密/解密函数

Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")

Private Function Rijndael(ByVal sInput As String, ByVal bEncrypt As Boolean) As String
    ' Create an instance of encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using key and IV - already available in byte() as byteKey and byteIV
    Dim transform As ICryptoTransform
    If bEncrypt Then
        transform = _rijndael.CreateEncryptor(byteKey, byteIV)
    Else
        transform = _rijndael.CreateDecryptor(byteKey, byteIV)
    End If
    ' Create streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed data into the crypto stream. 
    msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
    ' Flush crypto stream. 
    msInput.FlushFinalBlock()
    Dim byteOutput As Byte() = msOutput.ToArray
    Return Convert.ToBase64String(byteOutput)
End Function

用法:

Dim sEncrypted As String = Rijndael("This is a test", True)
Dim sDecrypted As String = Rijndael(sEncrypted, False) **This is the line where it is crashing**

编辑-最终,看似工作的函数对(参见评论)参考:

Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")

Public Function Encrypt(ByVal sInput As String) As String
    ' Create an instance of our encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using our key and IV 
    Dim transform As ICryptoTransform
    transform = _rijndael.CreateEncryptor(byteKey, byteIV)
    ' Create the streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed our data into the crypto stream 
    msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
    msInput.FlushFinalBlock()
    Return Convert.ToBase64String(msOutput.ToArray)
End Function

Public Function Decrypt(ByVal sInput As String) As String
    ' Create an instance of our encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using our key and IV 
    Dim transform As ICryptoTransform
    transform = _rijndael.CreateDecryptor(byteKey, byteIV)
    ' Create the streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed our data into the crypto stream. 
    msInput.Write(Convert.FromBase64String(sInput), 0, Convert.FromBase64String(sInput).Length)
    msInput.FlushFinalBlock()
    Return Encoding.UTF8.GetString(msOutput.ToArray)
End Function

用法

Dim sEncrypted As String = Encrypt("This is a test")
Dim sDecrypted As String = Decrypt(sEncrypted) 

OK, so having just got the key to be accepted and have an encrypted string out which is 44 char long, I now am unable to decrypt (aarrgghh):

Length of the data to decrypt is invalid.

Having looked around and read various posts it looks as though the conversion to a Base64String may be the problem but I can't see where it is wrong - many of the solutions I have seen appear to be identical to what I have. Again, I'd really appreciate any help - extracts below:

Encryption/Decryption Function

Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")

Private Function Rijndael(ByVal sInput As String, ByVal bEncrypt As Boolean) As String
    ' Create an instance of encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using key and IV - already available in byte() as byteKey and byteIV
    Dim transform As ICryptoTransform
    If bEncrypt Then
        transform = _rijndael.CreateEncryptor(byteKey, byteIV)
    Else
        transform = _rijndael.CreateDecryptor(byteKey, byteIV)
    End If
    ' Create streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed data into the crypto stream. 
    msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
    ' Flush crypto stream. 
    msInput.FlushFinalBlock()
    Dim byteOutput As Byte() = msOutput.ToArray
    Return Convert.ToBase64String(byteOutput)
End Function

Usage:

Dim sEncrypted As String = Rijndael("This is a test", True)
Dim sDecrypted As String = Rijndael(sEncrypted, False) **This is the line where it is crashing**

EDIT - Final, seemingly working pair of functions (see comment) for ref:

Private byteKey As Byte() = Encoding.UTF8.GetBytes("B499F4BF48242E05548D1E4C8BB26A2E")
Private byteIV As Byte() = Encoding.UTF8.GetBytes(",%u'm&'CXSy/T7x;4")

Public Function Encrypt(ByVal sInput As String) As String
    ' Create an instance of our encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using our key and IV 
    Dim transform As ICryptoTransform
    transform = _rijndael.CreateEncryptor(byteKey, byteIV)
    ' Create the streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed our data into the crypto stream 
    msInput.Write(Encoding.UTF8.GetBytes(sInput), 0, Encoding.UTF8.GetBytes(sInput).Length)
    msInput.FlushFinalBlock()
    Return Convert.ToBase64String(msOutput.ToArray)
End Function

Public Function Decrypt(ByVal sInput As String) As String
    ' Create an instance of our encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using our key and IV 
    Dim transform As ICryptoTransform
    transform = _rijndael.CreateDecryptor(byteKey, byteIV)
    ' Create the streams for input and output 
    Dim msOutput As New System.IO.MemoryStream()
    Dim msInput As New CryptoStream(msOutput, transform, CryptoStreamMode.Write)
    ' Feed our data into the crypto stream. 
    msInput.Write(Convert.FromBase64String(sInput), 0, Convert.FromBase64String(sInput).Length)
    msInput.FlushFinalBlock()
    Return Encoding.UTF8.GetString(msOutput.ToArray)
End Function

Usage

Dim sEncrypted As String = Encrypt("This is a test")
Dim sDecrypted As String = Decrypt(sEncrypted) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-08-20 16:49:13

如果要将其转换为 Base64,则必须使用 Base64 对其进行解码。目前,您正在获取输出 Base64 字节数组,然后使用 UTF8 将其转换回字节,这会给您一个完全不同的值。

您应该使用 Convert.FromBase64String 而不是 UTF8解码时将字符串更改为字节,然后使用 UTF8 而不是 Base64 来解释数据。

EG:关于您想要的解码

msInput.Write(Convert.FromBase64String(sInput), 0, Convert.FromBase64String(sInput).Length)

以及解码时的返回:

Return Encoding.UTF8.GetString(byteOutput)

If you're going to convert it to base64 you're going to have to decode it using Base64. At the moment you're taking the output Base64 byte array then using UTF8 to convert it back to bytes, which gives you a completely different value.

You should be using Convert.FromBase64String instead of UTF8 for changing the string to bytes when decoding, then using UTF8 instead of Base64 for interpreting the data.

EG: On the decode you'd want

msInput.Write(Convert.FromBase64String(sInput), 0, Convert.FromBase64String(sInput).Length)

And for the return when decoding:

Return Encoding.UTF8.GetString(byteOutput)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文