好的,所以有just got the key被接受,並有一個加密的字符串輸出,這是44個字符長,我現在無法解密(aarrgghh):ASP.NET Rijndael解密錯誤:要解密的數據長度無效
要解密的數據的長度無效。
說完看了看四周,看不同的崗位,它看起來好像轉換爲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**
編輯 - 最後,看似工作對函數(見註釋)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
用法
Dim sEncrypted As String = Encrypt("This is a test")
Dim sDecrypted As String = Decrypt(sEncrypted)
這是其中一個布爾參數是告訴你一個案件,這是真的兩個不同的例程 - 在這種情況下,程序1需要向後運行以製作例程2. – 2009-12-02 22:26:19
回想一個非常公平的評論,考慮到兩者之間的差異 - 原來(錯誤地),並沒有太多爲什麼會這樣 - 我活着學習。 Upvote評論它的價值!謝謝。 – Chris 2009-12-02 22:34:18
感謝您發佈您的答案,我很感激。對我來說,和你一樣,問題在於我轉換了'Encoding.UTF8.GetString()'和'Convert.ToBase64String()'。 – DMan 2010-07-29 17:05:02