简体   繁体   中英

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.

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

Then use rsa object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM