1

OK、受け入れられるキーを取得し、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**

編集 - 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) 
4

1 に答える 1

9

base64 に変換する場合は、Base64 を使用してデコードする必要があります。現時点では、出力 Base64 バイト配列を取得してから、UTF8 を使用してそれをバイトに戻すため、まったく異なる値が得られます。

デコード時に文字列をバイトに変更するには、UTF8 の代わりにConvert.FromBase64Stringを使用し、データの解釈には Base64 の代わりに UTF8を使用する必要があります。

EG: あなたが望むデコードについて

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

そして、デコード時の戻り値:

Return Encoding.UTF8.GetString(byteOutput)
于 2009-12-02T22:18:46.200 に答える