0

我正在尝试在 C# 中解码 AES 密码。如果我使用与编码时相同的 AES 对象,一切都很好。但是一旦我创建了一个不同的 AES 实例,它就不起作用了:

        string original = "username,password,companyID";
        byte[] encrypted;
        using (Aes myAes1 = Aes.Create()) {
            encrypted = EncryptStringToBytes_Aes(original, GetBytes("password"), myAes1.IV);

            //test1
            string test1 = DecryptStringFromBytes_Aes(encrypted, GetBytes("password"), myAes1.IV);
        }
        using (Aes myAes2 = Aes.Create()) {

            //test2
            string test2 = DecryptStringFromBytes_Aes(encrypted, GetBytes("password"), myAes2.IV);
        }

所以在这段代码中,test1 使用了 myAes1,它运行良好,因为加密也使用了 myAes1。然而 test2 使用 myAes2 并且它不起作用,这是输出:

test1 = username,password,companyID
test2 = t0�V�e]��Ԅ��yd,companyID

我究竟做错了什么?


以下是我从网上复制的支持功能:

   static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key,byte[] IV) {
        // Check arguments. 
        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;
        // Create an Aes object 
        // with the specified key and IV. 
        using (Aes aesAlg = Aes.Create()) {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

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

            // Create the streams used for encryption. 
            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 the encrypted bytes from the memory stream. 
        return encrypted;

    }

    static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) {
        // Check arguments. 
        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");

        // Declare the string used to hold 
        // the decrypted text. 
        string plaintext = null;

        // Create an Aes object 
        // with the specified key and IV. 
        using (Aes aesAlg = Aes.Create()) {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

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

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherText)) {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return plaintext;

    }

    static byte[] GetBytes(string str) {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
4

1 回答 1

3

如果您不指定 IV,则会随机生成一个。您在解密时使用了 2 个不同的 IV 值,这就是您看到不同结果的原因。

于 2013-09-13T18:44:11.270 回答