Sunday, May 24, 2009

Encryption and Decryption of query Strings - Part 2

C# v1.1.
The base64 strings are padded to a divisible of 4, spaces are deleted, and there is a rogue plus sign that some expert out there will be able to explain but this class works:


using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;


//for dispose
using System.ComponentModel;


namespace Enviroquip
{
public class clsBase64 : IDisposable
{


#region Constructors
public clsBase64()
{
}


#endregion


#region Declarations
private byte[] key = {};
private byte[] IV = {18, 52, 86, 120, 144, 171, 205, 239};
private string sEncryptionKey = "1!a^5$x#";


private string sLogSource = "clsBase64.cs";
private string sPageSource = "";


private clsLogWriter oLog = new clsLogWriter();


#endregion


#region Methods
public string encryptQueryString(string strQueryString)
{
return Encrypt(strQueryString);
}


public string decryptQueryString(string strQueryString)
{
return Decrypt(strQueryString);
}


private string Decrypt(string stringToDecrypt)
{
byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
try
{
string sOutcome="";
// special characters come back home
stringToDecrypt=stringToDecrypt.Replace("~plus~","+");


key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
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();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
sOutcome=encoding.GetString(ms.ToArray());
return sOutcome;
}
catch (Exception ex)
{

oLog.AddLogMessage(sLogSource + "." + sPageSource,clsLogWriter.MessageType.Error, 0, ex.Message);
return "error";


}
}

private string Encrypt(string stringToEncrypt)
{
try
{
clsFunctions oFuncs = new clsFunctions();
string sOutcome="";
// Convert the binary input into Base64 UUEncoded output.
// Each 3 byte sequence in the source data becomes a 4 byte
// sequence in the character array.
// If string to encrypt is not divisible by 4, make it so.
long arrayLength;
int index=stringToEncrypt.LastIndexOf(@"~");


arrayLength = (4 / 3) * stringToEncrypt.Length;
if (arrayLength % 4 != 0)
{
// appending to querystring var previous to last one
StringBuilder sb = new StringBuilder(stringToEncrypt);
sb.Remove(index, 1);
sb.Insert(index, "&~");
stringToEncrypt = sb.ToString();
sb=null;
}


arrayLength = (4 / 3) * stringToEncrypt.Length;
if (arrayLength % 4 != 0)
{
StringBuilder sb = new StringBuilder(stringToEncrypt);
sb.Remove(index, 1);
sb.Insert(index, "&~");
stringToEncrypt = sb.ToString();
sb=null;
}


arrayLength = (4 / 3) * stringToEncrypt.Length;
if (arrayLength % 4 != 0)
{
StringBuilder sb = new StringBuilder(stringToEncrypt);
sb.Remove(index, 1);
sb.Insert(index, "&~");
stringToEncrypt = sb.ToString();
sb=null;
}
arrayLength = (4 / 3) * stringToEncrypt.Length;
if (arrayLength % 4 != 0)
{
StringBuilder sb = new StringBuilder(stringToEncrypt);
sb.Remove(index, 1);
sb.Insert(index, "&~");
stringToEncrypt = sb.ToString();
sb=null;
}


key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
sOutcome=Convert.ToBase64String(ms.ToArray());
// space killer
sOutcome=sOutcome.Replace(" ","");


// special character replacements
sOutcome=sOutcome.Replace("+","~plus~");


return sOutcome;
}
catch (Exception ex)
{
oLog.AddLogMessage(sLogSource + "." + sPageSource,clsLogWriter.MessageType.Error, 0, ex.Message);
return "error";
}
}


#endregion


#region Destructors
void System.IDisposable.Dispose()
{
oLog = null;

}

#endregion

}
}

No comments: