简体   繁体   English

WinRT中的AES ECB解密,可从.NET Desktop转换代码

[英]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. 我正在尝试在Windows Phone应用程序中使用“正常”(桌面).NET框架中的一些加密代码,但无法正确翻译该代码。

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 我已尽力将其翻译为WinRT .NET Framework可用的代码,但它给了我一个

Value does not fall within the expected range.

error (System.ArgumentException). 错误(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): 解决方案(来自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. 我只对解密来自另一服务的数据感兴趣,而不关心ECB的安全性。 If that is not the case for you, note what Cyprien Autexier said in his response: 如果您的情况并非如此,请注意Cyprien Autexier在回应中所说的话:

And one more thing you should really be careful about ECB which has known security flaws. 此外,您还应该对具有已知安全漏洞的ECB真正小心。

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. 这可能是因为您使用的ECB加密模式不使用IV,因此您应该传递null而不是当前的已归零IV。

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

In addition you could convert your base 64 string directly to CryptographicBuffer 另外,您可以将基本64位字符串直接转换为CryptographicBuffer

And one more thing you should really be careful about ECB which has known security flaws. 此外,您还应该对具有已知安全漏洞的ECB真正小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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