08-31-2010, 12:14 PM
Wrote this in about 20 minutes. Give credit if you use it.
Imports:
Code:
Usage:
Imports:
Code:
using System.Security.Cryptography;
using System.IO;
Code:
Code:
static string strRC2Encryption(string strInput, string strKey, string strIV)
{
try
{
byte[] byteInput = Encoding.UTF8.GetBytes(strInput);
byte[] byteKey = Encoding.ASCII.GetBytes(strKey);
byte[] byteIV = Encoding.ASCII.GetBytes(strIV);
MemoryStream MS = new MemoryStream();
RC2CryptoServiceProvider CryptoMethod = new RC2CryptoServiceProvider();
CryptoStream CS = new CryptoStream(MS, CryptoMethod.CreateEncryptor(byteKey, byteIV), CryptoStreamMode.Write);
CS.Write(byteInput, 0, byteInput.Length);
CS.FlushFinalBlock();
return Convert.ToBase64String(MS.ToArray());
}
catch (Exception up)
{
throw up; //YUCK! ;D
}
}
static string strRC2Decryption(string strInput, string strKey, string strIV)
{
try
{
byte[] byteInput = Convert.FromBase64String(strInput);
byte[] byteKey = Encoding.ASCII.GetBytes(strKey);
byte[] byteIV = Encoding.ASCII.GetBytes(strIV);
MemoryStream MS = new MemoryStream();
RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
CryptoStream CS = new CryptoStream(MS, RC2.CreateDecryptor(byteKey, byteIV), CryptoStreamMode.Write);
CS.Write(byteInput, 0, byteInput.Length);
CS.FlushFinalBlock();
return Encoding.UTF8.GetString(MS.ToArray());
}
catch (Exception up)
{
throw up; //YUCK! ;D
}
}
Usage:
Code:
string s = strRC2Encryption("Mike", "AAAAAAAA", "AAAAAAAA");
Console.WriteLine(s);
Console.WriteLine(strRC2Decryption(s, "AAAAAAAA", "AAAAAAAA"));