Hi,
I wanna do encryption and decryption of a string in C# Visual Studio 2005. I did some search on google and got a code in VB.Net, and I converted it to C#. But its returning this string while encrypting... "Specified key is not a valid size for this algorithm.". Can anybody help me please. is this the right way in Visual Studio 2005 ?
Here is the code...
public static string Decrypt(string encryptedText)
{
byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };
try
{
byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Convert.FromBase64String(encryptedText.Replace(' ', '+'));
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, rgbIV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception e)
{
return e.Message;
}
}
public static string Encrypt(string stringToEncrypt)
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
byte[] rgbIV = { 0x21, 0x43, 0x56, 0x87, 0x10, 0xfd, 0xea, 0x1c };
try
{
byte[] key = Encoding.UTF8.GetBytes("A0D1X0Q");
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception e)
{
return e.Message;
}
}