Tuesday, June 30, 2009

Encrypt and Decrypt the string using RSACryptoServiceProvider in c#.Net within Single Line of Code

//Call the functions

string EncryptedString= GetEncryptedText("Chandru");
string DecryptedString = GetDecryptedText(EncryptedString);


//Create an object for RSACryptoServiceProvider
RSACryptoServiceProvider RSAEncrypt = new RSACryptoServiceProvider();
//Encrypt the String Provided
public string GetEncryptedText(string PlainStringToEncrypt)
{
return Encoding.Default.GetString(RSAEncrypt.Encrypt(Encoding.Default.GetBytes(PlainStringToEncrypt), false));

}

//Decrypt The String Provided
public string GetDecryptedText(string EncryptedStringToDecrypt)
{
return Encoding.Default.GetString(RSAEncrypt.Decrypt(Encoding.Default.GetBytes(EncryptedStringToDecrypt), false));
}

No comments: