好的,所以刚刚获得了要接受的密钥并输出了一个 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)