C# 使用其他AES实例时AES解码错误

C# 使用其他AES实例时AES解码错误,c#,aes,C#,Aes,我正在尝试用C#解码AES密码。如果使用与编码时相同的AES对象,则一切正常。但一旦我创建了另一个AES实例,它就不起作用了: string original = "username,password,companyID"; byte[] encrypted; using (Aes myAes1 = Aes.Create()) { encrypted = EncryptStringToBytes_Aes(original,

我正在尝试用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;
    }
静态字节[]加密StringToBytes_Aes(字符串明文,字节[]键,字节[]IV){
//检查参数。

如果(plainText==null | | plainText.Length如果不指定IV,则随机生成一个IV。解密时使用两个不同的IV值,这就是为什么会看到不同的结果。

如果不指定IV,则随机生成一个IV。解密时使用两个不同的IV值,这就是为什么会看到不同的结果。

g不同的结果。

也许你在字符编码上搞砸了。如果没有看到
EncryptStringToBytes\u Aes
DecryptStringFromBytes\u Aes
,很难判断发生了什么。补充道。我不是在编码,代码是直接从这个问题上的另一个stackoverflow问题中获取的,特别提到不编码。这是可能值得在处理之前刷新StreamWriter。3小时前,有人问了一个问题(但没有发布任何代码,因此无法提供帮助)。我想每个人都有这个问题。也许你把字符编码搞砸了。如果没有看到
EncryptStringToBytes\u Aes
DecryptStringFromBytes\u Aes
,很难判断发生了什么。补充道。我不是在编码,代码是直接从这个问题上的另一个stackoverflow问题中获取的,特别是不编码的帮助。在处理之前刷新StreamWriter可能是值得的。3小时前,有人问了一个问题(但没有发布任何代码,因此无法提供帮助)我想每个人都会遇到这样的问题。我应该如何使用IV,考虑编码/解码发生在不同的计算机上,我是否应该使用一个客户端无法访问的密码?对于AES,IV应该是16个随机字节。IV不需要保持秘密,通常的做法是将IV准备到加密的字节。加密完成后的数组,然后在解密之前将其剥离。@YongkeBillYu:使用从密码派生密钥和IV。@dtb:Rfc2898DeriveBytes对于密钥很好,但对于IV则不行。每次加密时IV需要是随机的,以便重复加密同一文本不会产生相同的密文。什么我应该使用IV,考虑编码/解码发生在不同的计算机上,我是否应该使用客户端无法访问的密码?对于AES,IV应该是16个随机字节。IV不需要保持秘密,通常的做法是在加密完成后将IV准备到加密的字节数组,然后将其剥离。解密前关闭。@YongkeBillYu:使用从密码派生密钥和IV。@dtb:Rfc2898DeriveBytes适用于密钥,但不适用于IV。对于每次加密,IV需要是随机的,以便重复加密同一文本不会产生相同的密文。