April 12, 2003

Encryption/Decryption/hash implementation in C#

Following is code snippet for Encryption/Decryption/hash implementation

/// <summary>
/// Encryption/Decryption/hash implementation
/// </summary>
internal class Security {

private static int KEY_SIZE = 128;
private static int KEY_SKIP_SIZE = 64;

/// <summary>
/// Encrypts the Message
/// </summary>
/// <param name="strMessageToEncrypt">Message to Encrypt</param>
/// <param name="btSalt">Salt</param>
/// <param name="strKey">Key</param>
/// <param name="strHashAlgorithm">Hash Algorithm</param>
/// <returns>Encrypted Message</returns>
internal static string encryptMessage(string strMessageToEncrypt, byte[] btSalt, string strKey) {

// Get input message in bytes
byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(strMessageToEncrypt);

// create a MemoryStream so that the process can be done without I/O files
System.IO.MemoryStream ms = new System.IO.MemoryStream();

// Get proper size key require for encryption
byte[] bytKey = GetProperKey(strKey);

RijndaelManaged objCryptoService = new RijndaelManaged();

// set the private key and init vector
objCryptoService.Key = bytKey;
objCryptoService.IV = btSalt;

// create an Encryptor from the Provider Service instance
ICryptoTransform encrypto = objCryptoService.CreateEncryptor();

// create Crypto Stream that transforms a stream using the encryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

// write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();

// convert into Base64 so that the result can be used in xml
return System.Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Length);
}


/// <summary>
/// Decrypt Input Message
/// </summary>
/// <param name="strEncryptedMessage">Encrypted Message</param>
/// <param name="btSalt">Salt</param>
/// <param name="strKey">Key</param>
/// <param name="strHashAlgorithm">Hash Algorithm</param>
/// <returns>Decrypted Message</returns>
internal static string decryptMessage(string strEncryptedMessage, byte[] btSalt,string strKey) {
// convert from Base64 to binary
byte[] bytIn = System.Convert.FromBase64String(strEncryptedMessage);
// create a MemoryStream with the input
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

byte[] bytKey = GetProperKey(strKey);
RijndaelManaged objCryptoService = new RijndaelManaged();
// set the private key
objCryptoService.Key = bytKey;
objCryptoService.IV = btSalt;

// create a Decryptor from the Provider Service instance
ICryptoTransform encrypto = objCryptoService.CreateDecryptor();

// create Crypto Stream that transforms a stream using the decryption
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

// read out the result from the Crypto Stream
System.IO.StreamReader sr = new System.IO.StreamReader( cs );
return sr.ReadToEnd();
}

/// <summary>
/// Manipulates key size required for Encryption/Decryption algorithm
/// </summary>
/// <param name="Key">Sec Key</param>
/// <returns>Fix size bytes require as a key</returns>
private static byte[] GetProperKey(string Key) {
string sTemp;
int i = 0, reqSize = KEY_SIZE;
// key sizes are in bits
while (Key.Length * 8 > reqSize) {
i = reqSize;
reqSize += KEY_SKIP_SIZE;
}
sTemp = Key.PadRight(reqSize / 8, '*');

// convert the secret key to byte array
return ASCIIEncoding.ASCII.GetBytes(sTemp);
}

}