如何在WinRT中使用AES加密/解密?

时间:2013-07-31 21:35:04

标签: c# encryption windows-8 windows-phone-8 cryptography

我是密码学的新手。

已编辑:使用正确的加密/解密算法似乎错了,所以我将问题更改为:

如何将这些代码行转换为WinRT?

代码基于http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx

处的代码

我需要在WinRT中做同样的事情:

更新

WinRT(Windows 8.1)的密码学和证书样本解决了我的问题。

请检查:http://code.msdn.microsoft.com/windowsapps/Cryptography-and-3305467b

public static byte[] Encrypt(string plainText)
    {
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        using (AesManaged aesAlg = new AesManaged())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }
        return encrypted;
    }

public static string Decrypt(byte[] cipherText)
    {
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        string plaintext = null;

        using (AesManaged aesAlg = new AesManaged())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

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

            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                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();
                    }
                }
            }

        }

        return plaintext;

    }

1 个答案:

答案 0 :(得分:0)

是的,这是可能的,但你应该使用相同的:

当然还有正确的密码(AES代替Rijndael),对称密钥大小和值以及IV。确保分别检查这些功能的每个IO 。不要依赖默认值,明确设置每个值。

请注意,使用ECB模式是不安全的。目前你可能正在混合CBC和ECB模式,这是行不通的。要进行安全通信,您应使用authenticated encryptionMAC(使用第二个密钥)。