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

时间:2009-12-02 22:13:21

标签: asp.net encryption cryptography rijndaelmanaged

好的,所以让just got the key被接受并且有一个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) 

1 个答案:

答案 0 :(得分:9)

如果您要将其转换为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)