Tuesday, 11 October 2011

Encryption........Decryption functionality on Asp.net

Add one class file ....name as Encryption.cs

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using Paragon.Security.Cryptography;
using Microsoft.Win32;

/// <summary>
/// Summary description for Encryption
/// </summary>
public class Encryption
{
    protected string encryptionKey = "";
    public Encryption()
    {
        try
        {
            encryptionKey = System.Configuration.ConfigurationManager.AppSettings["EncryptionKey"];
            if (encryptionKey == null || encryptionKey == "")
                throw new Exception("Encryption key is missing or is blank");

        }
        catch (Exception ex)
        {
            //GMSLogFile.LogBLError(ex.Message);
            //throw ex;
        }
    }
    public string EncryptString(string dataToEncrypt)
    {
        string encryptedString = "";
        try
        {
            if (dataToEncrypt != "")
            {
                encryptedString = EncryptionHelper.EncryptData(EncryptionHelper.EncryptionMethod.Rijndael, EncryptionHelper.HashMethod.MD5, dataToEncrypt, encryptionKey);
            }
           
        }
        catch (Exception ex)
        {
        }
        return encryptedString;
    }
    public string DecryptString(string dataToDecrypt)
    {
        string decryptedString = "";
        try
        {
            if (dataToDecrypt != "")
            {
                decryptedString = EncryptionHelper.DecryptData(EncryptionHelper.EncryptionMethod.Rijndael, EncryptionHelper.HashMethod.MD5, dataToDecrypt, encryptionKey);
            }
         
        }
        catch (Exception ex)
        {

        }
        return decryptedString;
    }
}

add  dll file name as Paragon.Security.Cryptography.dll

add key to web.config file under <appsetting> section
<add key="EncryptionKey" value="AxrLnHejuKEmpKvFqLpbQedndvpgTsDF" />
create object of Encryption class globally
  Encryption encry;
1)for encrypt decrypted code we can use
  tempPass = FirstName + usid.ToString() + lastname;
   encry = new Encryption();
  string pass = encry.EncryptString(tempPass);
in this sentence encrypt the value of variable tempPass and stored it to pass variable
2)
for decrypt  encrypted code we can use
create object of Encryption class globally
  Encryption encry;
 encry = new Encryption();
string strpass = Convert.ToString(dr["Password"]);
string decpass = encry.DecryptString(strpass);
in this sentence decrypt the value of variable strpass and stored it to decpass variable

No comments:

Post a Comment