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

C# 如何在WinRT中使用AES加密/解密?,c#,encryption,windows-8,windows-phone-8,cryptography,C#,Encryption,Windows 8,Windows Phone 8,Cryptography,我是密码学的新手 编辑:似乎我使用了正确的加密/解密算法,因此我将问题改为: 如何将这些代码行转换为WinRT 该代码基于上的代码 我需要在WinRT中执行同样的操作: 更新: WinRT(Windows 8.1)的加密和证书示例解决了我的问题。 请检查: 公共静态字节[]加密(字符串明文) { 如果(plainText==null | | plainText.Length是,这是可能的,但您应该使用相同的: 纯文本的字符编码,如 (可选)密文的字符串编码,例如或更易于调试的 当然还有正

我是密码学的新手

编辑:似乎我使用了正确的加密/解密算法,因此我将问题改为:

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

该代码基于上的代码

我需要在WinRT中执行同样的操作:

更新:

WinRT(Windows 8.1)的加密和证书示例解决了我的问题。

请检查:

公共静态字节[]加密(字符串明文)
{

如果(plainText==null | | plainText.Length是,这是可能的,但您应该使用相同的:

  • 纯文本的字符编码,如
  • (可选)密文的字符串编码,例如或更易于调试的
当然还有正确的密码(AES而不是Rijndael)、对称密钥大小和值以及。请确保分别检查这些函数的每个IO。不要依赖默认值,请显式设置每个值


请注意,使用ECB模式是不安全的。目前您可能混合使用CBC和ECB模式,这将不起作用。对于安全通信,您应该使用或(使用第二个密钥).

首先,您的解密例程看起来可疑地像加密例程。其次,请同时发布密钥派生。请注意,ECB不安全,您的密钥派生方法也不安全(为什么首先需要一种?)@owlstead感谢您的回复。哦,解密粘贴错误。它现在被编辑,解密方法现在是正确的代码。我有一些资源用于Win8应用程序和WP8应用程序。我想通过加密来保护它们。显然,对密钥的第一部分和最后一部分使用相同的字节将使密钥大小减半。通常情况下f仅仅几轮AES算法是不值得的。@owlstead代码的基础是:不要只信任网络上的任何加密代码,这种代码很糟糕。最好的方法是学习如何应用加密技术。第二个最好的方法是使用经过严格审查的高级库。
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;

    }