Friday, 28 October 2016

How to Encrypt and Decrypt Any Fields in in ASP.NET or MVC in C#

Hi This Battu ,in this tutorial i show how to Encryption and Decryption the given field in login form, Admin Form or any other forms .When we use the given code then the original field values encrypted in certain format and when you use the value then the values is decrypted , but the value stored in database as encryption format. so hackers cant hack our account as well as if you use sessions then the account gets full tied so hackers cant hack our password or any other data, in same way if you use stored procedure in database site then it is impossible to hack any data of you.

we have two methods in the below code

1) public string Encrypt(string plainText,string encryptionKey)
2) public string Decrypt(string encryptText, string decryptionKey)

in above methods we used two parameters ,the second paramere using for key

using System.IO;
using System.Text;
using System.Security.Cryptography;

the above three namespaces we have import.
now the code shown below if you want use in project just check if it works then use it


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace battu.common
{
  
    public class Utility
    {
         
        #region Protected Field

        protected string sqlCommandText;

        #endregion

        #region Encrypt & Decrypt Methods

        public string Encrypt(string plainText,string encryptionKey)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(plainText);
            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();
                    }

                    plainText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return plainText;
        }

        public string Decrypt(string encryptText, string decryptionKey)
        {
            byte[] encryptBytes = Convert.FromBase64String(encryptText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(decryptionKey, 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(encryptBytes, 0, encryptBytes.Length);
                        cs.Close();
                    }

                    encryptText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return encryptText;
        }

        #endregion
    }
}  

No comments:

Post a Comment