News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. ***
  Feb 17 2017 3:52AM     Michael
  0 Comments    1183 Views  
Here Michael provided a short description for Encryption and Decryption for Password in C#

Encryption / Decryption:

Encryption is the process of translating plain text data (plaintext) 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 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.

For Encryption:

       public string Encrypt(string clearText, string EncryptionKey)
        {
            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 });
                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();
                    }
                    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 string Decrypt(string cipherText, string EncryptionKey)
        {            
            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;
        }

Usage:

      private void Form1_Load(object sender, EventArgs e)
        {

            string EncryptionKey = GenerateEncryptionKey();

            string Encrypted = Encrypt("PasswordHere", EncryptionKey);
            string Decrypted = Decrypt(Encrypted, EncryptionKey);
        }


In this page I have described about simple Encryption and Decryption in C#. Thanks
BackToTop
Comments



 
Search
Recent Posts
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Read excel file and convert to List in C#
Feb 25 2017 6:26AM Posted By Michael
Encryption and Decryption for Password in C#
Feb 17 2017 3:52AM Posted By Michael
How to create Chart in ASP.Net C#
Feb 16 2016 8:03AM Posted By Michael Ryan
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: