Saturday 20 July 2019

Encryption and Decryption for Password in C#





Encryption / Decryption:

Encryption is the process of translating plain text data into something that appears to be random and meaningless (ciphertext). 

Decryption is the process of converting ciphertext back to plaintext. To encrypt more than a small amount of data, symmetric encryption is used.

Here we have used Rfc2898DeriveBytes class derived from DeriveBytes.

Namespace:

using System.Security.Cryptography;

C# Code:


Generate Key:

       public static string GenerateEncryptionKey()
        {
            string EncryptionKey = string.Empty;
            Random Robj = new Random();
            int Rnumber = Robj.Next();
            EncryptionKey = "XYZ" + Convert.ToString(Rnumber);
            return EncryptionKey;
        }

The GenerateEncryptionKey() method will generate the key. Pass the generated EncryptionKey to the Encrypt() method to get the Encrypted string value.
or
you can use encryption key inside the method.
 .

For Encryption:
public static readonly string PwdToken = "AnT";

       public static string Password_Encrypt(string clearText, bool IsToken)
        {
            /////////// Generate Token
            string EncryptionKey = "";
            if (IsToken)
            {
                Random Robj = new Random();
                int Rnumber = Robj.Next();
                EncryptionKey = PwdToken + Convert.ToString(Rnumber);
            }

            /////////// Encrypt password
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
            {
                System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.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 (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }


The above method will accept the arguement cleartText and generated encryption key.


For Decryption:


While decrypt the encrypted string, have to pass the EncryptionKey which we have used for encryption.


public static string Password_Decrypt(string cipherText, bool IsToken)
        {
            /////////// Generate Token
            string EncryptionKey = "";
            if (IsToken)
            {
                Random Robj = new Random();
                int Rnumber = Robj.Next();
                EncryptionKey = PwdToken + Convert.ToString(Rnumber);
            }

            /////////// Decrypt password
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (System.Security.Cryptography.Aes encryptor = System.Security.Cryptography.Aes.Create())
            {
                System.Security.Cryptography.Rfc2898DeriveBytes pdb = new System.Security.Cryptography.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 (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, encryptor.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }
Usage:

      private void Form_Load(object sender, EventArgs e)
        {

            
string EncryptionKey = GenerateEncryptionKey();

               
/////////// or

            
string EncryptPassword_Encrypt ("YourPassword"true );
            
string DecryptPassword_Decrypt (("YourEncryptedPassword"true );
        }

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...