[go: up one dir, main page]

0% found this document useful (0 votes)
109 views2 pages

RSA Encryption for Developers

This document describes how to encrypt and decrypt data using asymmetric encryption with RSA keys in C#. It includes a class with methods to generate public and private keys, encrypt and decrypt bytes and text, and get the maximum data length for a given key size. The class handles encryption with the public key and decryption with the private key. An example at the end generates keys, encrypts a sample text, then decrypts it to demonstrate the encryption/decryption process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views2 pages

RSA Encryption for Developers

This document describes how to encrypt and decrypt data using asymmetric encryption with RSA keys in C#. It includes a class with methods to generate public and private keys, encrypt and decrypt bytes and text, and get the maximum data length for a given key size. The class handles encryption with the public key and decryption with the private key. An example at the end generates keys, encrypts a sample text, then decrypts it to demonstrate the encryption/decryption process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

You might also like