Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
        }
Posted
Updated 25-May-17 3:49am
Comments
F-ES Sitecore 25-May-17 8:41am    
I'm sure if you google for "c# aes encrypt decrypt" you'll find lots of working examples. If you want the key to be random you're going to need some way to ensure that whatever decrypts the text knows what the random key is. The mechanism you choose could easily make your code non-secure.
CPallini 25-May-17 8:49am    
My virtual 5.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900