2

I'm trying to decrypt a string, but I'm getting the

Specified initialization vector (IV) does not match the block size for this algorithm.

I've been searching SO and the web for a while and I understand my IV is 32 bytes and should be 16 bytes, but I can't figure out how to achieve it. The string to get has been encrypted using AES/CBC/PKCS5Padding and my code (well, actually I've found it somewhere in the web) is

var btKey = Encoding.ASCII.GetBytes("7c6e1257d0e81ff55bda80cc904365ae");
var btIV = Encoding.ASCII.GetBytes("cf5e4620455cd7190fcb53ede874f1a8");

aesAlg.Key = btKey;
aesAlg.IV = btIV;

aesAlg.Padding = PaddingMode.PKCS7;

// Create a decrytor to perform the stream transform.
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(encodedTicketAsBytes))
  {
    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)){
      using (StreamReader srDecrypt = new StreamReader(csDecrypt))
      {
        // Read the decrypted bytes from the decrypting stream
        // and place them in a string.
        plainText = srDecrypt.ReadToEnd();
      }
    }
  }

What I don't understand is the use of the aesAlg.Padding, to be honest I couldn't find yet an easy, to my understanding, example of this in C#.

Any help?,

Thanks!!

4

1 回答 1

5

您拥有的密钥几乎可以肯定是一堆十六进制值,而不是 ascii 字符。你正在做:

var btIV = Encoding.ASCII.GetBytes("cf5e4620455cd7190fcb53ede874f1a8");

它将它视为任何其他字符串并将其转换为其二进制 ascii 字节。对我来说,这些看起来像十六进制数字。每 2 个字符是一个单字节值。你可能想要类似的东西

var btIV = new byte[] {0xcf,0x5e,0x46,0x20,0x45,0x5c,0xd7,0x19,0x0f,0xcb,0x53,0xed,0xe8,0x74,0xf1,0xa8};
于 2012-05-29T14:13:15.137 回答