如何使用AES获取相同纯文本的不同密文

Say*_*Pal 3 c# encryption aes

我目前在C#中使用AesManaged类来加密纯文本.它工作正常.

但是,每次加密同一条数据时,它都会生成相同的密文.反正我是否可以调整此行为并为同一条数据生成不同的密文?

我使用AES_256算法和证书在SQL服务器中实现了加密.这个过程非常类似于这里的帖子:http://www.codeproject.com/Articles/662187/FIPS-Encryption-Algorithms-and-Implementation-of-A.在此过程中,每次加密纯文本时,都会生成不同的密文.

我想要与C#代码相同的效果.如何实现?

编辑: 以下是我实施Yolanda Ruiz建议的方法:

加密

public static string Encrypt(string plainText)
    {
        //Check for valid arguments.
        if (String.IsNullOrEmpty(plainText)) throw new ArgumentNullException("plainText");

        List<byte> encryptedList;

        //Create Aes object
        using (AesManaged aes = new AesManaged())
        {
            aes.Key = Key;
            aes.GenerateIV();
            encryptedList = aes.IV.ToList();
            aes.BlockSize = BlockSize;

            /*Here goes the standard code to encrypt the plain text - refer msdn for that*/
            /*Append the encrypted stream to encryptedList*/
        }

        return encryptedList.ToArray().ToBase64();
    }
Run Code Online (Sandbox Code Playgroud)

解码

        public static string Decrypt(string cipherText)
    {
        //Check for valid arguments.
        if (string.IsNullOrEmpty(cipherText)) throw new ArgumentNullException("cipherText");

        string plainText;
        byte[] cipherTextArray = cipherText.FromBase64();

        //Create Aes object
        using (AesManaged aes = new AesManaged())
        {
            aes.Key = Key;
            aes.BlockSize = BlockSize;
            aes.IV = cipherTextArray.Take(NoOfBytes).ToArray();//Extract the IV
            cipherTextArray = cipherTextArray.Skip(NoOfBytes).ToArray();//Extract the actual plain text.

            /*Here goes the standard code to Decrypt the cipher text - refer msdn for that*/
            /*Assign the decrypted stream output to plainText*/
        }
        return plainText;
    }
Run Code Online (Sandbox Code Playgroud)

单元测试

        //Arrange
        string plainText = "Sayan";

        //Act
        string cipherText1 = MyCrypto.Encrypt(plainText);
        string cipherText2 = Crypto.Encrypt(plainText);
        string plainText1 = Crypto.Decrypt(cipherText1);
        string plainText2 = Crypto.Decrypt(cipherText2);

        //Assert
        //Check the cipher text is different everytime
        Assert.AreNotEqual(cipherText1, cipherText2);

        //Check that every plaintext output should match with the original
        Assert.AreEqual(plainText, plainText1);
        Assert.AreEqual(plainText, plainText2);
Run Code Online (Sandbox Code Playgroud)

Yol*_*uiz 6

这样做的方法是为每个加密使用不同的初始化向量.

AesManaged中的默认操作模式是CBC.在该模式中,当加密明文块时,首先将其与前一个块的加密结果混合.只要前一个密文块总是不同,这就可以防止两个相似的明文块输出相同的密文.但是,我们在第一个街区使用了什么呢?初始化向量.

IV基本上是一个随机区块,其行为就像加密在实际的第一个明文块之前的假设明文块一样.

必须保留IV,以便我们可以将其提供给解密方法.因为它在语义上是密文块,所以通常将它添加到实际的密文中.在解密时,您将首先提取第一个密文块(按原样,不进行解密),并将其用作IV来解密后续块.

IV不是秘密.攻击者将无法从中获取密钥或第一个明文块.但是,您必须永远不要使用相同的密钥重复使用相同的IV两次,否则您将失去随机化属性.

你会想在看的方法是AesManaged.GenerateIV(),AesManaged.BlockSize(这是比特,记住它,如果你使用属性提取从密文的四字节).