如何使用C#在ASP.NET中使用AES算法加密和解密字符串?

如何使用C#在ASP.NET中使用AES算法加密和解密字符串?

问题描述:

我想使用带有c#的asp.net中的AES算法加密和解密字符串。我想用一个可以随机生成和解密的密钥加密一个字符串,并在AES算法中加密字符串。我在AES中尝试加密和解密,但解密的值与我加密的字符串不匹配。我还需要随机生成加密密钥。目前我已经完成了静态获得的密钥。



我尝试过:



完成的代码如下:

I want to encrypt and decrypt a string using AES Algorithm in asp.net with c#. I want to encrypt a string with a key that can be randomly generated and decrypt and get the string encrypted in AES Algorithm. I tried encryption and decryption in AES but decrypted value is not matching with the string I have encrypted.Also I need to generate the encryption key randomly. Currently I have done with a key obtained statically.

What I have tried:

The code done is as below:

Encryption Method
public static string AES_Encrypt(string clearText, ref string keyStr)
        {
            string EncryptionKey = "MAKV2SPBNI99212";
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

                byte[] key=new byte[16];
                key= pdb.GetBytes(16);
                encryptor.Key =key;
                encryptor.IV = key;

                keyStr = System.Text.Encoding.UTF8.GetString(key);
      

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }

       Decryption Method
        public static string AES_Decrypt(string cipherText, Aes encryptor)
        {

            //string EncryptionKey = "MAKV2SPBNI99212";
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            //using (Aes encryptor = Aes.Create())
            //{
            //Rfc2898DeriveBytes pdb = new
            //    Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            //encryptor.Key = pdb.GetBytes(16);
            //encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
            // }
            return cipherText;
        }

这是一篇很好的CodeProject文章: C#中的Swanky加密/解密 [ ^ ]

引用:

因为良好的性能,因为AES是2001年11月26日起的新FIPS标准,你应该总是选择AES_256_CBC。这将确保良好的安全性和性能。
Here is a good CodeProject article about it: Swanky Encryption/Decryption in C#[^]
Quote:
Because of the good performance and because AES is the "new" FIPS standard from November 26, 2001 you should probably always go for AES_256_CBC. This will ensure good security and performance.