I found similar questions but there are differences in details and for that reason I start new question. I have to encrypt 64 bytes with X509 RSA private key. It must be "direct" encryption without hash - like the 64 bytes are the hash output. I tried this:
X509Certificate2 cert = new X509Certificate2("my_cert_with_private_key.pfx", "pwd", X509KeyStorageFlags.Exportable);
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] encrypted = csp.Encrypt(my_data, false);
After many attempts to decrypt this with:
RSACryptoServiceProvider pub = (RSACryptoServiceProvider)cert.PublicKey.Key;
pub.Decrypt(encrypted, false);
I found that actually the encryption by csp
is made with the public key (because test decryption with PrivateKey is ok). Otherwise I get "Key not found" error. It seems that .Net implementation assumes that encryption can be made only with public key and it uses the public key even when you want to use the private key. "Their" idea is to use Sign
for encryption with the private key. Unfortunately I have to specify hash algorithm and did not found how to use "RAW" mode (i.e. no hash). Is there a way to use different classes and do simple encryption with the private key?