2

我似乎无法解决这个问题,我已经使用GRC的随机生成器创建了我认为是 256 位的密钥,并将其与我的 IV 结合起来。我不断收到以下错误:

指定的密钥不是此算法的有效大小。

感谢您提供任何帮助,这是我用于加密/解密的代码:

Private Function Rijndael(ByVal sInput As String, ByVal bEncrypt As Boolean) As String
    ' Setup the Key and Initialization Vector.
    Dim byteKey As Byte() = Encoding.UTF8.GetBytes("C3CA193570B26E5C3CBB50FD805A0E23BAFFABA135E82C41517EEDCB9B7C90AC")
    Dim byteIV As Byte() = Encoding.UTF8.GetBytes("c+O2r)J~?L:$]u[2")
    ' Create an instance of the encyrption algorithm. 
    Dim _rijndael As New RijndaelManaged()
    ' Create an encryptor using our key and IV 
    Dim transform As ICryptoTransform
    If bEncrypt Then
        transform = _rijndael.CreateEncryptor(byteKey, byteIV)
    Else
        transform = _rijndael.CreateDecryptor(byteKey, byteIV)
    End If
    ' Create the 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()
    Return Convert.ToBase64String(msOutput.ToArray)
End Function
4

1 回答 1

4

C3CA193570B26E5C3CBB50FD805A0E23BAFFABA135E82C41517EEDCB9B7C90AC是 256 位长比特流的十六进制代码。

Encoding.UTF8.GetBytes不会按照您的想法将十六进制代码转换为相应的字节。

因为您在该字符串中有 64 个字符,所以您将获得 64 个字节的 UTF-8 字节。那是一个 512 位长的比特流。

于 2009-12-02T21:21:57.350 回答