2day i'm showing 64 bit encryption and decryption using a Key in asp.net.for that u need to import these name space's,
using System.Security.Cryptography;
using System.Text;
using System.IO;
Encryption
private const string DEFAULT_KEY = "#kl?+@<z";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void txt_string_TextChanged(object sender, EventArgs e)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream MemoryStream = new MemoryStream();
CryptoStream Cryptostream;
string key="#kl?+@<z";
CheckKey(ref key);
des.Key = HashKey(key, des.KeySize / 8);
des.IV = HashKey(key, des.KeySize / 8);
string stringToEncrypt = txt_string.Text.Trim();
byte[] inputBytes = Encoding.UTF8.GetBytes(stringToEncrypt);
Cryptostream = new CryptoStream(MemoryStream, des.CreateEncryptor(), CryptoStreamMode.Write);
Cryptostream.Write(inputBytes, 0, inputBytes.Length);
Cryptostream.FlushFinalBlock();
string result = Convert.ToBase64String(MemoryStream.ToArray());
Label1.Text = result.ToString();
}
private static void CheckKey(ref string keyToCheck)
{
keyToCheck = keyToCheck.Length > 8 ? keyToCheck.Substring(0, 8) : keyToCheck;
if (keyToCheck.Length < 8)
{
for (int i = keyToCheck.Length; i < 8; i++)
{
keyToCheck += DEFAULT_KEY[i];
}
}
}
private static byte[] HashKey(string key, int length)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
// Hash the key
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] hash = sha1.ComputeHash(keyBytes);
// Truncate hash
byte[] truncatedHash = new byte[length];
Array.Copy(hash, 0, truncatedHash, 0, length);
return truncatedHash;
}
Decryption
private const string DEFAULT_KEY = "#kl?+@<z";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void txt_String_TextChanged(object sender, EventArgs e)
{
string key = "#kl?+@<z";
string txt = txt_String.Text.Trim();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream;
// Check whether the key is valid, otherwise make it valid
CheckKey(ref key);
des.Key = HashKey(key, des.KeySize / 8);
des.IV = HashKey(key, des.KeySize / 8);
byte[] inputBytes = Convert.FromBase64String(txt);
cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
lbl.Text= encoding.GetString(memoryStream.ToArray());
}
private static void CheckKey(ref string keyToCheck)
{
keyToCheck = keyToCheck.Length > 8 ? keyToCheck.Substring(0, 8) : keyToCheck;
if (keyToCheck.Length < 8)
{
for (int i = keyToCheck.Length; i < 8; i++)
{
keyToCheck += DEFAULT_KEY[i];
}
}
}
private static byte[] HashKey(string key, int length)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
// Hash the key
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] hash = sha1.ComputeHash(keyBytes);
// Truncate hash
byte[] truncatedHash = new byte[length];
Array.Copy(hash, 0, truncatedHash, 0, length);
return truncatedHash;
}
Have a nice day... 'N happy Coding :)
No comments:
Post a Comment