Skip to main content

Encryption Decryption Helper



Encryption Decryption Helper

public class clsEncryptionDecryption
{
#region Variable Declaration

private static string keyString = "keyString";

#endregion

#region Methods/Functions

/// <summary>
/// Get Encrpted Value of Passed value
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks></remarks>
public static string GetEncrypt(string value)
{
return Encrypt(keyString, value);
}

/// <summary>
/// Get Decrypted value of passed encrypted string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <remarks></remarks>
public static string GetDecrypt(string value)
{
return Decrypt(keyString, value);
}

/// <summary>
/// Encrypt value
/// </summary>
/// <param name="Passphrase"></param>
/// <param name="Message"></param>
/// <returns></returns>
/// <remarks></remarks>
private static string Encrypt(string Passphrase, string Message)
{
using (MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider())
{
byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below

byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));
// Step 2. Create a new TripleDESCryptoServiceProvider object
using (TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider())
{

// Step 3. Setup the encoder
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;

// Step 4. Convert the input string to a byte[]
byte[] DataToEncrypt = UTF8.GetBytes(Message);

// Step 5. Attempt to encrypt the string
try
{
ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
TDESAlgorithm.Clear();
HashProvider.Clear();
}

// Step 6. Return the encrypted string as a base64 encoded string
return Convert.ToBase64String(Results);
//}
//catch (Exception)
//{
// throw;
//}
}
}

}

/// <summary>
/// decrypt value
/// </summary>
/// <param name="Passphrase"></param>
/// <param name="Message"></param>
/// <returns></returns>
/// <remarks></remarks>
private static string Decrypt(string Passphrase, string Message)
{

if (string.IsNullOrEmpty(Message))
return string.Empty;

using (MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider())
{

byte[] Results;
System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

// Step 1. We hash the passphrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below

byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

// Step 2. Create a new TripleDESCryptoServiceProvider object
using (TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider())
{

// Step 3. Setup the decoder
TDESAlgorithm.Key = TDESKey;
TDESAlgorithm.Mode = CipherMode.ECB;
TDESAlgorithm.Padding = PaddingMode.PKCS7;

Message = Message.Replace(" ", "+"); // Replace space with plus sign in encrypted value if any.- 

// Step 4. Convert the input string to a byte[]
byte[] DataToDecrypt = Convert.FromBase64String(Message);

// Step 5. Attempt to decrypt the string
try
{
ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
}
catch
{
return "";
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
TDESAlgorithm.Clear();
HashProvider.Clear();
}

// Step 6. Return the decrypted string in UTF8 format
return UTF8.GetString(Results);
//}
//catch (Exception)
//{
// throw;
//}
}
}

}


#endregion

}


Comments

Popular posts from this blog

Tata Punch: A Compact SUV That Packs a Punch

Postback Page using Javascript in ASP.net

Postback Page using Javascript in ASP.net For Calling Function: __doPostBack('__Page', ''); Function: function __doPostBack(eventTarget, eventArgument) {         if (!theForm.onsubmit || (theForm.onsubmit() != false)) {             theForm.__EVENTTARGET.value = eventTarget;             theForm.__EVENTARGUMENT.value = eventArgument;             theForm.submit();         }     }

Highest salary query in sql server

DECLARE @dtDepartment TABLE ( Id int, Name varchar(100) ) DECLARE @dtEmployee TABLE ( Id int, Name varchar(100), DeptId int ) DECLARE @dtEmpSalary TABLE ( Id int, EmpId int, Salary int ) INSERT INTO @dtDepartment VALUES(1,'.NET') INSERT INTO @dtDepartment VALUES(2,'Java') INSERT INTO @dtEmployee VALUES(1,'Brijesh',1) INSERT INTO @dtEmployee VALUES(2,'Parth',1) INSERT INTO @dtEmployee VALUES(3,'Ruchir 1',1) INSERT INTO @dtEmployee VALUES(4,'Jainik 1',1) INSERT INTO @dtEmployee VALUES(5,'Ruchir',2) INSERT INTO @dtEmployee VALUES(6,'Jainik',2) INSERT INTO @dtEmployee VALUES(7,'Jainik',2) INSERT INTO @dtEmployee VALUES(8,'Jainik',2) INSERT INTO @dtEmpSalary VALUES(1,1,10000) INSERT INTO @dtEmpSalary VALUES(2,2,15000) INSERT INTO @dtEmpSalary VALUES(3,3,12000) INSERT INTO @dtEmpSalary VALUES(4,4,12000) INSERT INTO @dtEmpSalary VALUES(5,5,12000) INSERT INTO @dtEmpSa...