Encryption of string in C#
//This class is for encrypt the value
//call this method with two parameters text and key "01AB=$6Q"
class EncryptdecryptClass
{
public static string Encrypt(string text, string key)
{
byte[] encodedkey;
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] bytes;
encodedkey = Encoding.UTF8.GetBytes(key);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
bytes = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
try
{
CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(encodedkey, iv), CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
}
catch (Exception ex)
{
Logger.LogInfo(ex);
}
return Convert.ToBase64String(ms.ToArray());
}
}
Thanks for the short snippet for encryption through C#.
Although the overall code looks fine, but it would have been very comfortable on the part of any reader who has come to this site for knowledge gain, if some discussion about classes used, and the methods which are called have been done. Also the namespaces should have been mentioned. Atleast i would have gained much from this. Still, the code is short and helpful.