CSharp-easy-RSA-PEM icon indicating copy to clipboard operation
CSharp-easy-RSA-PEM copied to clipboard

C# Class library with sample app to load and save PEM encoded keys into RSACryptoServiceProvider.

CSharp-easy-RSA-PEM

License: GNU GENERAL PUBLIC LICENSE v3 Author: Christoffer Järnåker Description: Class library with sample app to load and save PEM encoded keys into RSACryptoServiceProvider.

This library was made to facilitate an easy way to interact with PEM encoded certificates from C# on Windows. I made this as this seems to be an ongoing issue for C# developers, cause by the fact that MS apparently wants us to only save our keys in XML format. Well, that doesn't work that well if you want your keys to be used outside of Windows, and you can't use keys made in the most commonly available format: ASN.1 PEM encoded, e.g. what you usually get out of OpenSSL and such likes. It's made up from bits and pieces of code found on the net, mixed together with my own, so please look at the end of this doc for some sources and credits.

Example on how to use on private and public keys generated by OpenSSL and saved in PEM format (most common):

      // Load private key. This is stored in RSA format.
      string priv = File.ReadAllText("keys\\private.rsa.key");
      RSACryptoServiceProvider privc = Crypto.DecodeRsaPrivateKey(priv);

      // Load public key. This is stored in X.509 format
      string publ = File.ReadAllText("keys\\public.rsa.key");
      RSACryptoServiceProvider publc = Crypto.DecodeX509PublicKey(publ);

      // Encrypt with public key, decrypt with private.
      string supersecret = Crypto.EncryptString("Hello", publc);
      string decodesecret = Crypto.DecryptString(supersecret, privc);

Sources and credits Some sources are simply meantioned here though I don't use any of its code. But inspiration and help on the path of understanding is also worth mentioning ;)

Thanks Jeffrey Walton for the awsome implementation of AsnKeyBuilder and AsnKeyParser. He also provides an great in depth article on what keys in what formats fits where. Really good base for understanding therelation between RSA, PKCS #8, X.509 and ASN.1. http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys

Thanks Mario Majčica for showing the interaction between certificates and keys http://www.codeproject.com/Articles/162194/Certificates-to-DB-and-Back

Thanks to Iridium and abasan for showing the basic structure of the RSA PEM format. http://stackoverflow.com/questions/23734792/c-sharp-export-private-public-rsa-key-from-rsacryptoserviceprovider-to-pem-strin

Thanks Michel Gallant for a great implementation. I learned a lot from your code. http://www.jensign.com/opensslkey/opensslkey.cs