I am creating an x509 certificate using makecert with the following parameters:
makecert -r -pe -n "CN = Client" -ss MyApp
I want to use this certificate to encrypt and decrypt data using RSA algoritm. I look at the generated certificate in the Windows certificate store and everything looks fine (it has a private key, the public key is an RSA key with 1024 bits, etc.)
Now I use this C # code to encrypt data:
X509Store store = new X509Store("MyApp", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySubjectName, "Client", false);
X509Certificate2 _x509 = certs[0];
using (RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_x509.PublicKey.Key)
{
byte[] dataToEncrypt = Encoding.UTF8.GetBytes("hello");
_encryptedData = rsa.Encrypt(dataToEncrypt, true);
}
When I execute the Encrypt method, I get a CryptographicException with the message "Bad key".
I think the code is ok. Perhaps I am not creating the certificate properly. Any comments? Thanks
---------------- EDIT --------------
- , OpenSsl, .