简体   繁体   中英

Why does RijndaelManaged 256 bit is giving me “specified key is not a valid size for this algorithm” error?

I'm unable to make the C# RijndaelManaged encryption work with PHP mcrypt_encrypt as I kept getting "Specified key is not a valid size for this algorithm".

The PHP specification use 256 bit, with ECB cipher mode. My understanding is Initization Vector is not used when in ECB mode.

We're using temporary key to get this working in development project, to keep on building the developmental application and we'll issue a new secure key much later.

[PHP]
$plaintext = '1~ABCDEFG~1408740350~0~';

for($i = 1; $i <= 32; $i++) { $key .= chr($i); }

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $plaintext, 'ecb', '');


[C#]
string postFormData = "1~ABCDEFG~1408740350~0~";
StringBuilder sb = new StringBuilder();
foreach (var b in Encoding.ASCII.GetBytes(String.Concat(Enumerable.Range(1, 32)))) { sb.AppendFormat("{0}", b); }
postedFormData = RijndaelAES.Encrypt(postedFormData, sb.ToString());

public static string Encrypt(string plainText, string key)
{
    string cipherText;
    var rijndael = new RijndaelManaged()
    {
        Key = Encoding.UTF8.GetBytes(key),
        Mode = CipherMode.ECB,
        BlockSize = 256, //128,
        Padding = PaddingMode.Zeros//, 
        //IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    };
    ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, null);

    using (var memoryStream = new MemoryStream())
    {
        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
            using (var streamWriter = new StreamWriter(cryptoStream))
            {
                streamWriter.Write(plainText);
                streamWriter.Flush();
            }
            cipherText = Convert.ToBase64String(memoryStream.ToArray());
        }
    }
    return cipherText;
}

Rijndael/AES has a fixed block size of 128 (in all implementations). You probably meant to set the key size , not the block size.

Also, the way you convert the key to bytes is highly unusual (it is amazing, actually). I don't think you really understand encodings. This is an important topic to research. Probably, that PHP version is assuming the string to be ASCII. In C#, you can do it like this:

string keyStr = ...;
Debug.Assert(keyStr.Length == (256 / 8));

byte[] keyBytesASCII = Encoding.ASCII.GetBytes(keyStr);
Debug.Assert(keyBytesASCII.Length == (256 / 8));
Debug.Assert(keyBytesASCII.Length == keyStr.Length);

In the future you can debug problems like this by looking at important values using the debugger . I think you'd find that the key you set is not of the expected length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM