C # RSA encrypt / decrypt exception

I am trying to set up simple RSA encryption on the server side of a small piece of information that needs to be decrypted on the client side. As a proof of concept, I wrote a few lines to ensure that the public and private keys can be downloaded from xml. However, I try my best to do even the simplest things on my machine:

byte[] bytes = Encoding.UTF8.GetBytes("Some text"); bool fOAEP = true; // seeding a public and private key RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); var publicKey = rsa.ToXmlString(false); var privateKey = rsa.ToXmlString(true); //server side RSACryptoServiceProvider rsaServer = new RSACryptoServiceProvider(); rsaServer.FromXmlString(privateKey); var encrypted = rsaServer.Encrypt(bytes, fOAEP); //client side RSACryptoServiceProvider rsaClient = new RSACryptoServiceProvider(); rsaClient.FromXmlString(publicKey); var decrypted = rsaClient.Decrypt(encrypted, fOAEP); 

The last Decrypt call throws a CryptographicException with the message "Error decoding OAEP add-on." I must be missing something completely obvious here. Do I need to tune rsa instances more, or maybe the initial rsa sample instance?

+1
c # rsacryptoserviceprovider
source share
1 answer

You must use the public key for encryption and the private key for decryption. Look here: RSACryptoServiceProvider decrypt public key

Now back to the RSACryptoServiceProvider Class. The encryption method ONLY encrypts using the public key and the decryption method decrypts only the private key.

+6
source share

All Articles