简体   繁体   中英

AES ECB decryption in WinRT, translating code from .NET Desktop

I am trying to use some encryption code from the "normal" (Desktop) .NET framework in my Windows Phone application, but I am unable to translate the code correctly.

This is the working desktop code:

internal static byte[] decryptECB_Desktop(byte[] image)
{
    using (RijndaelManaged rm = new RijndaelManaged())
    { 
        rm.Mode = CipherMode.ECB;
        rm.Key = Convert.FromBase64String("key");
        rm.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        rm.Padding = PaddingMode.Zeros;
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, rm.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(image, 0, image.Length);
                return ms.ToArray();
            }
        }
    }
}

I have tried my best to translate it to code useable by the WinRT .NET Framework, but it gives me a

Value does not fall within the expected range.

error (System.ArgumentException).

Here is the code I am using:

internal static byte[] decryptECB_WinRT(byte[] image)
{
    IBuffer toDecryptBuffer = CryptographicBuffer.CreateFromByteArray(image);
    SymmetricKeyAlgorithmProvider aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);

    CryptographicKey symetricKey = aes.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(Convert.FromBase64String("key")));
    IBuffer ivBuffer = CryptographicBuffer.CreateFromByteArray(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

    IBuffer buffDecrypted = CryptographicEngine.Decrypt(symetricKey, toDecryptBuffer, ivBuffer); // <--- Exception here

    byte[] decrypted;

    CryptographicBuffer.CopyToByteArray(buffDecrypted, out decrypted);

    return decrypted;
}

The error appears when executing

CryptographicEngine.Decrypt(...)

Any help would be greatly appreciated. Thanks in advance.


Solution (from Cyprien Autexier):

public static byte[] decryptECB_WinRT(byte[] image)
{
    IBuffer toDecryptBuffer = CryptographicBuffer.CreateFromByteArray(image);
    SymmetricKeyAlgorithmProvider aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);

    CryptographicKey symetricKey = aes.CreateSymmetricKey(CryptographicBuffer.DecodeFromBase64String("base64key"));

    IBuffer buffDecrypted = CryptographicEngine.Decrypt(symetricKey, toDecryptBuffer, null);

    byte[] decrypted;

    CryptographicBuffer.CopyToByteArray(buffDecrypted, out decrypted);

    return decrypted;
}

I was only interested in decrypting data from another service, and do not care about the security of ECB. If that is not the case for you, note what Cyprien Autexier said in his response:

And one more thing you should really be careful about ECB which has known security flaws.

This is likely to be because you are using ECB encryption mode which does not uses an IV so you should pass null instead of your current zeroed IV.

IBuffer buffDecrypted = CryptographicEngine.Decrypt(symetricKey, toDecryptBuffer, null); 

In addition you could convert your base 64 string directly to CryptographicBuffer

And one more thing you should really be careful about ECB which has known security flaws.

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