0

我是密码学的新手。

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

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

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

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

更新:

WinRT (Windows 8.1) 的 Cryptography and Certificate 示例解决了我的问题。

请检查: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;

    }
4

1 回答 1

0

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

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

请注意,使用 ECB 模式是不安全的。目前您可能正在混合使用 CBC 和 ECB 模式,这是行不通的。对于安全通信,您应该使用经过身份验证的加密MAC(使用第二个密钥)。

于 2013-07-31T22:11:11.087 回答