Click here to Skip to main content
16,018,653 members
Articles / Programming Languages / C# 5.0
Tip/Trick

How to Encrypt and Decrypt String using AES Algorithm

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
7 Nov 2014CPOL 25.6K   21   4
This tip presents an example of encrypt and decrypt password and store in data source.

Introduction

This tip presents an example of encrypt and decrypt password. In real scenarios, the user passwords are stored in database as in encrypted formatted for more security. This is a very good practice to store encrypted passwords in database.

Using the Code

In ASP.NET codebehind, first import the required namespace:

C#
System.Security.Cryptography;

In encrypted method, code is below:

C#
//
// Encrypt string method here


private string Encrypt(string Text)
{
    string EncryptionKey = "AMAR2SPBNRAP390";
    byte[] clearBytes = Encoding.Unicode.GetBytes(Text);
    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(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
   using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(),
 CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
           Text = Convert.ToBase64String(ms.ToArray());
        }
    }
    return Text;
}

In decrypted method, code is below:

C#
//
// Decrypt string method here

private string Decrypt(string cipherText)
{
    string EncryptionKey = "AMAR2SPBNRAP390";
    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(32);
        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;
}

Hope these two methods will be helpful to all.

Wherever you required to encrypt and decrypt string, just call these methods with parameter of the encrypted string just enough. This is very useful to all.

Points of Interest

This is most useful and will be required for all whenever users have secure passwords.

History

  • 2014-11-08

License

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



Comments and Discussions

 
Questionthe-input-data-is-not-a-complete-block-when-decrypting-using-aes Pin
Member 1398830525-Oct-18 23:08
Member 1398830525-Oct-18 23:08 
Bug[My vote of 1] This technique should NOT be used for secure password storage Pin
Brice Williams1-Jul-17 13:40
Brice Williams1-Jul-17 13:40 
The AES standard is designed for general purpose symmetric-key data encryption and not for secure password storage. Please see the OWASP article at Using Rfc2898DeriveBytes for PBKDF2 - OWASP[^] for more details on using the Rfc2898DeriveBytes function for storing passwords.
Questionwhy do you have a fixed salt ? Pin
Garth J Lancaster7-Nov-14 23:23
professionalGarth J Lancaster7-Nov-14 23:23 
AnswerRe: why do you have a fixed salt ? Pin
hans.hinnekint7-Jan-16 2:08
hans.hinnekint7-Jan-16 2:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.