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. *** 
  Jul 15 2018 12:09PM     Michael
  1 Comments    3626 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


Adam Fletcher
Nov 11 2018 9:41AM
One can try ZetExcel.com to know more about how to generate Excel files on C#.

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Oct 29 2018 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Oct 28 2018 12:09PM Posted By Pranav
Google URL Shortener in C#
Oct 20 2018 12:09PM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Oct 5 2018 12:09PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Sep 26 2018 12:09PM Posted By John
Gridview custom CSS in ASP.Net
Sep 14 2018 12:09PM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Sep 10 2018 12:09PM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: