Encryption and Decryption of a Querystring
Encryption and Decryption of a Querystring.
We can send the querystring by Encryption mode and receiver it in Decryption mode.
Following is the sample code for encrypt
public static string KEY = "K6u8#m2a";
public static string EncryptQueryString(string stringToEncrypt)
{
byte[] key = { };
byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
try
{
key = Encoding.UTF8.GetBytes(KEY);
using (DESCryptoServiceProvider oDESCrypto = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream MyMemoryStream = new MemoryStream();
CryptoStream MyCryptoStream = new CryptoStream(MyMemoryStream,
MyDESCrypto.CreateEncryptor(key, IV), CryptoStreamMode.Write);
MyCryptoStream.Write(inputByteArray, 0, inputByteArray.Length);
MyCryptoStream.FlushFinalBlock();
return Convert.ToBase64String(MyMemoryStream.ToArray());
}
}
catch (Exception ex)
{
throw ex;
}
}
Following is the code for Decrypt of Querystring
public static string DecryptQueryString(string stringToDecrypt)
{
byte[] key = { };
byte[] IV = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78 };
stringToDecrypt = stringToDecrypt.Replace(" ", "+");
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(KEY);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
By
Nathan