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());

}

}


Comments

Author: Amit Srivastava15 Apr 2009 Member Level: Bronze   Points : 2

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.

Author: D.Jeya kumar(JK)20 Apr 2009 Member Level: Gold   Points : 1

Hi,


Please post with some description about the Code. so that it will be helpful to others who is new to .net.

Regards
JK



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: