This code is for encrypting and then decrypting any string. here we use teo strings NORMAL and ENCRYPT. in this method we pick characters from input and and get its index from NORMAL string and character at the same index in ENCRYPT string is replaces by original character. reverse process is used for decryption.
string NORMAL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string ENCRYPT = "YO17KPLVSU50C8WE64GAI3MB2DFNZQ9JXTRH";
string strInput = "NikhilGaur";
string strEncrypted = string.Empty;
string strDecrypted = string.Empty;
string strNewChar;
string strLtr;
int iIdx;
protected EncryptionDecryptionDemo()
{
Response.Write("Original String : " + strInput);
Response.Write("
");
for (int i = 0; i < strInput.Length; i++)
{
strLtr = strInput.Substring(i, 1).ToUpper();
iIdx = NORMAL.IndexOf(strLtr);
strNewChar = ENCRYPT.Substring(iIdx, 1).ToUpper();
strEncrypted += strNewChar;
}
Response.Write("Encrypted string : " + strEncrypted);
Response.Write("
");
for (int i = 0; i < strInput.Length; i++)
{
strLtr = strEncrypted.Substring(i, 1).ToUpper();
iIdx = ENCRYPT.IndexOf(strLtr);
strNewChar = NORMAL.Substring(iIdx, 1).ToUpper();
strDecrypted += strNewChar;
}
Response.Write("Decrypted string(Original again :" + strDecrypted);
}
If you want to use any special characters you can add in variable "NORMAL" and equivalent encrypted value in variable "ENCRYPT".
"ENCRYPT" variable has the reordered letters of "NORMAL" variable.
Showing posts with label Decryption. Show all posts
Showing posts with label Decryption. Show all posts
Tuesday, August 25, 2009
Thursday, July 2, 2009
Code Encryption and Decryption using a Key in C#
Many time we need to encrypt the user name and password for a high level of security..
here is the code for doing so.. We just need to include the name space System.Security in our application.. IN this code. we can use the Key to encrypt and Decrypt, thus it will provide a higher level of security.. That key we can also save in the Web config file or can provide from front end..
here is the code.
#region "Encryption & Decryption"
public static string Encrypt(stringtoEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =UTF8Encoding.UTF8.GetBytes(toEncrypt);
string key = "543z";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform =tdes.CreateEncryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
returnConvert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(stringcipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =Convert.FromBase64String(cipherString);
string key = "543z";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform =tdes.CreateDecryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
returnUTF8Encoding.UTF8.GetString(resultArray);
}
#endregion
here is the code for doing so.. We just need to include the name space System.Security in our application.. IN this code. we can use the Key to encrypt and Decrypt, thus it will provide a higher level of security.. That key we can also save in the Web config file or can provide from front end..
here is the code.
#region "Encryption & Decryption"
public static string Encrypt(stringtoEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =UTF8Encoding.UTF8.GetBytes(toEncrypt);
string key = "543z";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform =tdes.CreateEncryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
returnConvert.ToBase64String(resultArray, 0, resultArray.Length);
}
public static string Decrypt(stringcipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray =Convert.FromBase64String(cipherString);
string key = "543z";
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = newMD5CryptoServiceProvider();
keyArray =hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray =UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes =new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform =tdes.CreateDecryptor();
byte[] resultArray =cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
returnUTF8Encoding.UTF8.GetString(resultArray);
}
#endregion
Subscribe to:
Posts (Atom)