Wolfgang Amadeus
Wolfgang Amadeus

Reputation: 396

How to encrypt a message with Private Key and decrypt it elsewhere with only the Public Key

Lets say, Person1 has two files: person1.public.key and person1.private.key. This person uses his person1.private.key to encrypt a message and send it to me.

I only have the file person1.public.key, and I want to decrypt the message sent to me.

How to:

I did search online, but all examples I saw was the code generating the keys on runtime. I want to use the keys in the files.

Upvotes: 0

Views: 1357

Answers (1)

jitter
jitter

Reputation: 54605

Did you try using RSA.ImportFromPem(...)?

It supports the following PEM labels:

  • PUBLIC KEY
  • PRIVATE KEY
  • RSA PRIVATE KEY
  • RSA PUBLIC KEY
var keyString = loadFileIntoReadyOnlySpan();
var rsaKey = RSA.Create();
rsaKey.ImportFromPem(keyString);

I assume you know/can figure out how to load a file

Upvotes: 1

Related Questions