You are on page 1of 2

24/04/2017 Printversion

http://www.csharpdeveloping.net/Snippet/how_to_encrypt_decrypt_using_asymmetric_algorithm_rsa

Howtoencrypt/decryptusingasymmetric
algorithmRSA
Classforencrypting,decryptingbytesandtextwithpublicandprivatekeysusingasymmetricalgorithmRSA.Also
enablesgeneratekeys.RSAisusedforencryptingsmalleramountofdata.UseGetMaxDataLengthmethodtocheck
maximumdatalengthforspecifiedkeysize.

Importnamespaces
usingSystem;
usingSystem.Security.Cryptography;
usingSystem.Text;

Class
publicstaticclassAsymmetricEncryption
{
privatestaticbool_optimalAsymmetricEncryptionPadding=false;

publicstaticvoidGenerateKeys(intkeySize,outstringpublicKey,outstringpublicAndPrivateKey)
{
using(varprovider=newRSACryptoServiceProvider(keySize))
{
publicKey=provider.ToXmlString(false);
publicAndPrivateKey=provider.ToXmlString(true);
}
}

publicstaticstringEncryptText(stringtext,intkeySize,stringpublicKeyXml)
{
varencrypted=Encrypt(Encoding.UTF8.GetBytes(text),keySize,publicKeyXml);
returnConvert.ToBase64String(encrypted);
}

publicstaticbyte[]Encrypt(byte[]data,intkeySize,stringpublicKeyXml)
{
if(data==null||data.Length==0)thrownewArgumentException("Dataareempty","data");
intmaxLength=GetMaxDataLength(keySize);
if(data.Length>maxLength)thrownewArgumentException(String.Format("Maximumdatalengthis{0}"
if(!IsKeySizeValid(keySize))thrownewArgumentException("Keysizeisnotvalid","keySize");

if(String.IsNullOrEmpty(publicKeyXml))thrownewArgumentException("Keyisnullorempty","publicKeyXml

using(varprovider=newRSACryptoServiceProvider(keySize))
{
provider.FromXmlString(publicKeyXml);
returnprovider.Encrypt(data,_optimalAsymmetricEncryptionPadding);
}
}

publicstaticstringDecryptText(stringtext,intkeySize,stringpublicAndPrivateKeyXml)
{
vardecrypted=Decrypt(Convert.FromBase64String(text),keySize,publicAndPrivateKeyXml);
returnEncoding.UTF8.GetString(decrypted);
}

publicstaticbyte[]Decrypt(byte[]data,intkeySize,stringpublicAndPrivateKeyXml)
{
if(data==null||data.Length==0)thrownewArgumentException("Dataareempty","data");
if(!IsKeySizeValid(keySize))thrownewArgumentException("Keysizeisnotvalid","keySize");
if(String.IsNullOrEmpty(publicAndPrivateKeyXml))thrownewArgumentException("Keyisnullorempty"

using(varprovider=newRSACryptoServiceProvider(keySize))
{
provider.FromXmlString(publicAndPrivateKeyXml);
http://www.csharpdeveloping.net/Print/2103dcd723a64ec6bee1b4bd097871d7 1/2
24/04/2017 Printversion
provider.FromXmlString(publicAndPrivateKeyXml);
returnprovider.Decrypt(data,_optimalAsymmetricEncryptionPadding);
}
}

publicstaticintGetMaxDataLength(intkeySize)
{
if(_optimalAsymmetricEncryptionPadding)
{
return((keySize384)/8)+7;
}
return((keySize384)/8)+37;
}

publicstaticboolIsKeySizeValid(intkeySize)
{
returnkeySize>=384&&
keySize<=16384&&
keySize%8==0;
}
}

Use
constintkeySize=1024;
stringpublicAndPrivateKey;
stringpublicKey;

AsymmetricEncryption.GenerateKeys(keySize,outpublicKey,outpublicAndPrivateKey);

stringtext="textforencryption";
stringencrypted=AsymmetricEncryption.EncryptText(text,keySize,publicKey);
stringdecrypted=AsymmetricEncryption.DecryptText(encrypted,keySize,publicAndPrivateKey);
Console.WriteLine("Encrypted:{0}",encrypted);
Console.WriteLine("Decrypted:{0}",decrypted);

http://www.csharpdeveloping.net/Print/2103dcd723a64ec6bee1b4bd097871d7 2/2