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

Time:09-11

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:

  • encrypt the message on Person1's side?
  • decrypt the message on my side?

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.

CodePudding user response:

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

  • Related