SHA-256 Encryption And Decryption

 

Introduction:

 

Encryption is the conversion of information from a readable state to apparent nonsense (in general).

 

What is SHA?

 

SHA stands for Secure Hash Algorithm and it’s a family of cryptographic hash functions.

 

We created a user login page to encrypt the password and also you can see how to decrypt.

 

 

I given the password as ABCDEFG

 

Passing the ABCDEFG to encrypt method as below screen shot.



After encryption is will looks as below.

 



By calling the Decrypt method the output is as below.

The code for Encryption,Decryption  and hashing is as below.

Note: Directly copy paste the code (Default.aspx)into your code behind

using System;

using System.Text;

using System.Security.Cryptography;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    #region Private Members

         private byte[] _keyByte = { };

         string keyBytes;

        //Default Key

        private static string _key = "";

        //Default initial vector

        private byte[] _ivByte = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };

    #endregion

    #region Events

        protected void Page_Load(object sender, EventArgs e)

          {

 

          }

        #endregion

    #region methods

 

        ///<summary>

        /// Encrypt text by key with initialization vector

        ///</summary>

        ///<param name="value">plain text</param>

        ///<param name="key"> string key</param>

        ///<param name="iv">initialization vector</param>

        ///<returns>encrypted text</returns>

        public string Encrypt(string value, string key, string iv)

        {

            string encryptValue = string.Empty;

            MemoryStream ms = null;

            CryptoStream cs = null;

            if (!string.IsNullOrEmpty(value))

            {

                try

                {

                    if (!string.IsNullOrEmpty(key))

                    {

                        _keyByte = Encoding.UTF8.GetBytes

                                (key.Substring(0, 8));

                        if (!string.IsNullOrEmpty(iv))

                        {

                            _ivByte = Encoding.UTF8.GetBytes

                                (iv.Substring(0, 8));

                        }

                    }

                    else

                    {

                        _keyByte = Encoding.UTF8.GetBytes(_key);

                    }

                    using (DESCryptoServiceProvider des =

                            new DESCryptoServiceProvider())

                    {

                       

                        byte[] inputByteArray =

                            Encoding.UTF8.GetBytes(value);

                        ms = new MemoryStream();

                        cs = new CryptoStream(ms, des.CreateEncryptor

                        (_keyByte, _ivByte), CryptoStreamMode.Write);

                        cs.Write(inputByteArray, 0, inputByteArray.Length);

                        cs.FlushFinalBlock();

                        encryptValue = Convert.ToBase64String(ms.ToArray());

                    }

                }

                catch(Exception ex)

                {

                    //TODO: write log

                    Response.Write(ex.Message.ToString());

                }

                finally

                {

                    cs.Dispose();

                    ms.Dispose();

                }

            }

           string x = Decrypt(encryptValue,keyBytes ,string.Empty);

           return encryptValue;

 

        }

        public string Decrypt(string value, string key, string iv)

        {

            string decrptValue = string.Empty;

            if (!string.IsNullOrEmpty(value))

 

            {

 

                MemoryStream ms = null;

 

                CryptoStream cs = null;

 

                value = value.Replace(" ", "+");

 

                byte[] inputByteArray = new byte[value.Length];

 

                try

 

                {

 

                    if (!string.IsNullOrEmpty(key))

 

                    {

 

                        _keyByte = Encoding.UTF8.GetBytes

 

                                (key.Substring(0, 8));

 

                        if (!string.IsNullOrEmpty(iv))

 

                        {

 

                            _ivByte = Encoding.UTF8.GetBytes

 

                                (iv.Substring(0, 8));

 

                        }

 

                    }

 

                    else

 

                    {

 

                        _keyByte = Encoding.UTF8.GetBytes(_key);

 

                    }

 

                    using (DESCryptoServiceProvider des =

 

                            new DESCryptoServiceProvider())

 

                    {

 

                        inputByteArray = Convert.FromBase64String(value);

 

                        ms = new MemoryStream();

 

                        cs = new CryptoStream(ms, des.CreateDecryptor

 

                        (_keyByte, _ivByte), CryptoStreamMode.Write);

 

                        cs.Write(inputByteArray, 0, inputByteArray.Length);

 

                        cs.FlushFinalBlock();

 

                        Encoding encoding = Encoding.UTF8;

 

                        decrptValue = encoding.GetString(ms.ToArray());

 

                    }

 

                }

 

                catch

 

                {

 

                    //TODO: write log

 

                }

 

                finally

 

                {

 

                    cs.Dispose();

 

                    ms.Dispose();

 

                }

 

            }

 

            return decrptValue;

 

        }

 

 

Conclusion:  like the above functionality we can also can do SHA1,MD5,SHA384 = 8 and          SHA512 = 16,….etc

                                                                                                              SHA256Exp.zip (27.42 kb)