Tannheuser
Tannheuser

Reputation: 416

How to import RSA key from KEY file

I need to encrypt string in my app using RSA key from file .key. It contains something like this:

---BEGIN RSA PRIVAET KEY---
MIICHATIABBgQDi+.....OKh4=
---END RSA PRIVATE KEY---

So, I know that I can use RSACryptoServiceProvider class to encrypt my string, but how can I import key from .key file?

Upvotes: 4

Views: 1406

Answers (1)

President James K. Polk
President James K. Polk

Reputation: 41958

You can use the Bouncycastle C# crypto library, specifically the PEMReader class. PEMReader.readObject() will read it in and return an AsymmetricCipherKeyPair containing the private and public keys. You can then use the DotNetUtilities class to convert to .NET objects.

Upvotes: 2

Related Questions