This adds PKCS8 compliant key export to the OpenSSL::PKey::RSA library that is part of standard Ruby distributions.


key =

# => "-----BEGIN PRIVATE KEY----- ..."

This will export the private key in PKCS8 format, and will export public keys in the PUBKEY format used by OpenSSL. Note the absence of “RSA” in both of the headers.


Using the Gem distribution is probably easiest:

gem install openssl_pkcs8

The contents of openssl_pkcs8.c is mostly taken from the Ruby distribution and is licensed under exactly the same terms as the original.

The remainder is (C) 2012 Scott Tadman, The Working Group Inc. under the MIT License.