Latest Post

Friday, 9 September 2016

Encryption and Decryption in ASP.Net using C#

Encryption and Decryption
Simple program for encryption and decryption (Cryptography) in ASP.Net using C# to Encrypt a string and Decrypt an Encrypted String

Namespaces
you  have to use below namespaces for encryption and decryption (Cryptography)
using System.IO;
using System.Text;
using System.Security.Cryptography;  

C# Sample Code


below is the code using Advanced Encryption Standard (AES) method

  
public int CONST_KEYSIZE = 256;     // 192,256
public int CONST_BLOCKSIZE = 256;   // 64,128,256
private string CONST_DEFAULT_SALTKEY = "oy6TAG4OdnyB4fPV4mX2h8ZF44tOINFYUv+KwKsgmRQ="; // Blank to generate random key

Encryption Method
public string[] AESEncryption(string plainString)

{
  string[] aPassword = new string[2];
  RijndaelManaged AesEncryption = new RijndaelManaged();         
  AesEncryption.KeySize = CONST_KEYSIZE;
  AesEncryption.BlockSize = CONST_BLOCKSIZE;
  AesEncryption.Mode = CipherMode.CBC;
  AesEncryption.Padding = PaddingMode.PKCS7;          
  string saltkey = GenerateStrongSALT();  // This common method to generate SALT
  string keyStr = saltkey;          
  byte[] ivArr = Convert.FromBase64String(keyStr);           
  AesEncryption.IV = ivArr;
  AesEncryption.Key = ivArr;

  // This array will contain the plain text in bytes
  byte[] plainText = ASCIIEncoding.UTF8.GetBytes(plainString);            
  ICryptoTransform crypto = AesEncryption.CreateEncryptor();           
           
  // The result of the encrypion and decryption
  byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);           

  aPassword[0] = saltkey;
  aPassword[1] = Convert.ToBase64String(cipherText);
  return aPassword;
}

Decryption Method

public string AESDecryption(string encryptedString, string saltkey)
{
   RijndaelManaged AesEncryption = new RijndaelManaged();
   AesEncryption.KeySize = CONST_KEYSIZE;
   AesEncryption.BlockSize = CONST_BLOCKSIZE;
   AesEncryption.Mode = CipherMode.CBC;
   AesEncryption.Padding = PaddingMode.PKCS7;
          
   string keyStr = saltkey;
   byte[] ivArr = Convert.FromBase64String(keyStr);

   AesEncryption.IV = ivArr;
   AesEncryption.Key = ivArr;

   byte[] plainText = Convert.FromBase64String(encryptedString);

   ICryptoTransform decrypto = AesEncryption.CreateDecryptor();
   byte[] decryptedText = decrypto.TransformFinalBlock(plainText, 0, plainText.Length);
   return ASCIIEncoding.UTF8.GetString(decryptedText);
   
}

Comman Method to Genereate SALT

public string GenerateStrongSALT()
{
  if (string.IsNullOrEmpty(CONST_DEFAULT_SALTKEY))
    {
      int arraysize = (CONST_BLOCKSIZE / 16);
      RNGCryptoServiceProvider objSalt = new RNGCryptoServiceProvider();
      byte[] ar = new byte[arraysize];
      objSalt.GetBytes(ar);

     string sHashWithSalt = Encoding.UTF8.GetString(ar, 0, ar.Length);
     byte[] saltedHashBytes = Encoding.UTF8.GetBytes(sHashWithSalt);
     HashAlgorithm algorithm = new SHA256Managed();
     byte[] hash = algorithm.ComputeHash(saltedHashBytes);
     return Convert.ToBase64String(hash);

   }
  else
   {
     return CONST_DEFAULT_SALTKEY;

    }
}

Government Jobs