rsacryptoserviceprovider using x509 certificates c#

.net, cryptography, encryption-asymmetric, security

Solution

I finally found the problem. I wasn't putting the key to makecert to define it as RSA Crypto key.

Problem

i am using a certificate generated by makecert which has both private and public key. The java side uses this public key to encrypt the data and .net decrypts it back. I am trying to decrypt Java's encrypted 64 bit encoded string and getting bad data. To see if all is good on.Net end, I frist tried to encrypt with the public key and then decrypt with private using the same certificate. My code looks like this. ``` X509Certificate2 cert = GetCert(key, StoreName.My, StoreLocation.LocalMachine); RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PrivateKey; RSACryptoServiceProvider publicprovider = (RSACryptoServiceProvider)cert.PublicKey.Key; if (cert.HasPrivateKey) MessageBox.Show("Got private key"); byte[] encrypted = publicprovider.Encrypt(Encoding.UTF8.GetBytes(text), false); byte[] decryptedBytes = provider.Decrypt(encrypted, false); ``` Even here I am getting the error. Am i Missing something? The certificate looks valid with both public and private key.

Original source