Hans
Hans

Reputation: 2852

How to set RSACryptoServiceProvider Public Key?

I have a RSA public key stored in a file. The file is plain text and reads as:

-----BEGIN RSA PUBLIC KEY-----
Mdflkjlkf8u8f84rkrjfgk8r7u8t758tkjfedfkjldfjhfjdfdjfkdjfkdfdfdfs
Ddfldkfdjfkjljfldfdlfhkdhfkdhfkdhfkdhfkjdffdnodhmndhiufdofhodhfd
xWCnQ6QOIwKqRY6lklk09erjbds6erottgkjirt895t5tixaevJlMmrZGLaITW66
xVjbPvdpjMniFiemtwIDAQAB
-----END RSA PUBLIC KEY-----

I want to feed this public key to an object of RSACryptoServiceProvider and encrypt an arbitrary message using Encrypt. Unfortunately, I couldn't figure out how to set PK. Any help is more than welcome.

Upvotes: 0

Views: 2117

Answers (1)

i486
i486

Reputation: 6573

X509Certificate2 cert = new X509Certificate2("pub_key.crt");
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PublicKey.Key;

Then use rsa object.

Upvotes: 1

Related Questions