Thursday, 4 April 2013
Encryption methods
Hi Big,
Please check below the encryption method for SHA256 and TripleDES
using System.Security.Cryptography;
SHA256 Encryption:
public static string SHA256Encryption(string valueToEncrypt)
{
var uEncode = new UnicodeEncoding();
byte[] byteVal = uEncode.GetBytes(valueToEncrypt);
SHA256 sha = SHA256.Create();
byte[] hash = sha.ComputeHash(byteVal);
return Convert.ToBase64String(hash);
}
TripleDES Encryption:
public static string TripleDESEncryption(string valueToEncrypt)
{
byte[] key = Encoding.ASCII.GetBytes("<TripleDESCryptoService>"); //24characters
byte[] byteValue = Encoding.ASCII.GetBytes(valueToEncrypt);
TripleDES des = TripleDES.Create();
des.Key = key;
byte[] IV = new byte[des.IV.Length];
Array.Copy(key, IV, des.IV.Length);
des.IV = IV;
//des.Mode = CipherMode.ECB;
byte[] encryptedByte = des.CreateEncryptor(key, IV).TransformFinalBlock(byteValue, 0, byteValue.Length);
return Convert.ToBase64String(encryptedByte);
}
TripleDES Decryption:
public static string TripleDESDecryption(string value)
{
byte[] key = Encoding.ASCII.GetBytes("<TripleDESCryptoService>"); //24characters
byte[] plainText = Convert.FromBase64String(value);
TripleDES des = TripleDES.Create();
des.Key = key;
byte[] IV = new byte[des.IV.Length];
Array.Copy(key, IV, des.IV.Length);
des.IV = IV;
byte[] enc = des.CreateDecryptor(key, IV).TransformFinalBlock(plainText, 0, plainText.Length);
return Encoding.ASCII.GetString(enc);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment