Friday, July 29, 2005

RSA Public/Private key encryption/decryption in .NET

RSA Public/Private key encryption/decryption in .NET Here is some example code in C# that shows how to perform encryption/decryption using an assymetric scheme. Here are some points to remember: 1. The idea behind assymetric encryption/decryption is that its a scheme that allows others to provide you with data in a secure way. So you will generate the public-private key pair. You will then provide the public key to all those people who wish to send you secure data. Those people will use the public key to encrypt the data that they wish to send to you. When you receive the data, you will decrypt it using the private key. 2. Thus: use the public key for encryption and the private key for decryption 3. When using the RSACryptoServiceProvider, you will encrypt and decrypt bytes. Normally the data that you wish to encrypt will be a string. Its best to use Unicode encoding to do the GetBytes and GetString. Using any of the other encoding schemes can lead to loss of bytes. So while decryption if you get a bad data or bad key exception, then check if your loosing bytes between the encryption-decryption. Here is the example code:------------------------------------------------ using System.Security.Cryptography; using System.Text; UnicodeEncoding ByteConverter = new System.Text.UnicodeEncoding(); string privateKey = "l1iFuLPXWfkISoqSyyl1GAPIMK2+a7f9FuwH0P0BGMy+78k9CBulH0S1ETuGwOzbadMdXrujbI9IqDMCBGqX+Z9BbimhP/8Hy3ZNIRdcj6mfAMILBUSaa2N4xfyxdR01KZ4k+x8gXWwF+ZOWfp5TimEEGZ2hTLwZSGfh2GjyYRE=AQAB

xNs7N/AXNbA48U+jw0M8BLq4uOBT+gJczW/qNG5ZQtg9LahPpz6qUCYauPGLvznZ/IP+ZDSjkw6iT8oKK4qq1w==

xNDyN0rDwvaBB9vsgrjQPmWzQvlBLet24degd9qVl0Ijn15PnDUrjN2SPHq1olkUFcVzj2m8/cCqtpEjAhf+Vw==osp5KkUNrIRSUpv9XRhYpcCTya8ZQwJ5JBqMn1cx0alozj0FwdR8m1MnfhGh8z0wGzY8RTmSm6XabAk0Z9WAeQ==KraqCoPoTlSPFjSNdKVFVHVf3SYZFi0m5MaWJczDcEtDTd/66QzFAdJIXWXIK46nhD4Q4UvyXyPEKNEAxwCMCw==flYHYrpEmCNAqTy7w8IUhE7WLX6YjSuA0u3mjF8Ok57yGZ/nFH7CG6KH8HF4GYXq6jyJ91PHNMhBcf+aBE8wyw==JhJIoSvsUl/I7CdrwduN7xqUc1phemtwKSH63WGnsvteVKeEyNNbeEAqZElbHpD2qOpIixochCSPHHCoY0AFNo0HXJ8HPwaZyHJ3zRw+cQ+JbBdnPFKaQBc+AzO4931N8ZJZVrYt8nPRGkD3UyxDL47GeYLU9V/2XlwsKkrjRNk=
";
string publicKey = "l1iFuLPXWfkISoqSyyl1GAPIMK2+a7f9FuwH0P0BGMy+78k9CBulH0S1ETuGwOzbadMdXrujbI9IqDMCBGqX+Z9BbimhP/8Hy3ZNIRdcj6mfAMILBUSaa2N4xfyxdR01KZ4k+x8gXWwF+ZOWfp5TimEEGZ2hTLwZSGfh2GjyYRE=AQAB"; //use the following lines to generate your own set of keys //RSACryptoServiceProvider rsaCSProviderKeyGen = new RSACryptoServiceProvider(); //string publicKey = rsaCSProviderKeyGen.ToXmlString(false); //string privateKey = rsaCSProviderKeyGen.ToXmlString(true); string toEncrypt = "Hello World of Public/Private key encryption-decryption"; RSACryptoServiceProvider rsaCSProviderEncrypter = new RSACryptoServiceProvider(); rsaCSProviderEncrypter.FromXmlString(publicKey); byte []encryptedStringAsByte = rsaCSProviderEncrypter.Encrypt(ByteConverter.GetBytes(toEncrypt),false); string encryptedString = ByteConverter.GetString(encryptedStringAsByte); RSACryptoServiceProvider rsaCSProviderDecrypter = new RSACryptoServiceProvider(); rsaCSProviderDecrypter.FromXmlString(privateKey); byte []decryptedStringAsByte = rsaCSProviderDecrypter.Decrypt(ByteConverter.GetBytes(encryptedString),false); string decryptedString = ByteConverter.GetString(decryptedStringAsByte); Console.WriteLine(decryptedString);

No comments: