lpushpe
lpushpe

Reputation: 63

System.Security.Cryptography.Cryptographic Exception {"Key does not exist.\r\n"}

I'm reading private and public keys from XML String in my C# program. Encryption(with private key) works fine. But when it comes to Decryption(with public Key) it throws following error.

System.Security.Cryptography.Cryptographic Exception {"Key does not exist.\r\n"}

var rsa = new RSACryptoServiceProvider();

rsa.FromXmlString(_privateKey);
rsa.FromXmlString(_publicKey);

byte[] messagee = Encoding.UTF8.GetBytes("win win win");

byte[] encrypted = rsa.Encrypt(messagee, false);
string encString = Encoding.UTF8.GetString(encrypted);

byte[] decrypt = rsa.Decrypt(encrypted,false);
string decString = Encoding.UTF8.GetString(decrypt);

Why is that? I searched every where, but couldn't find any solution.

Thanks in advance.

Upvotes: 1

Views: 6300

Answers (1)

thilipan
thilipan

Reputation: 86

Look at this link, it might be helpful. The problem is in the following lines...

rsa.FromXmlString(_privateKey);
rsa.FromXmlString(_publicKey);

...because private key is overridden by the public key..

Upvotes: 1

Related Questions