Elmo
Elmo

Reputation: 6471

Encrypting Data with RSA in .NET

I have a DER file with sha1RSA as the Signature Algorithm. I have to encrypt some data using it.

Can anyone tell me how do I load the DER file and use the RSA public key in it to encrypt my data in .NET?

Upvotes: 0

Views: 1883

Answers (1)

Richard Schneider
Richard Schneider

Reputation: 35477

DER or Distinguished Encoding Rules is a method for encoding a data object, such as an X.509 certificate, to be digitally signed or to have its signature verified.

The X.509 certificate only contains the public key. You need the private key to decrypt!

Typically private keys are exchanged in .PFX files, which are password protected.

-- EDIT --

Sorry I misread your question. Yes, you can encrypt with the public key of X.509 certificate. You can load the .der by using System.Security.Cryptography.X509Certificates.X509Certificate2.Import method.

Then convert the public and encrypt, something like:

rsa = (RSACryptoServiceProvider) certificate.PublicKey.Key;
encryptedText = rsa.Encrypt(msg, true);

Upvotes: 1

Related Questions