Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created June 22, 2017 16:40
Show Gist options
  • Select an option

  • Save davidbalbert/336e497b70bd43b9f6c5e6b323d887af to your computer and use it in GitHub Desktop.

Select an option

Save davidbalbert/336e497b70bd43b9f6c5e6b323d887af to your computer and use it in GitHub Desktop.
Convert an RSA public key from PKCS#8 format to PKCS#1 format
#!/usr/bin/env ruby
#
# Converts an RSA public key from PKCS#8 format to PKCS#1 format. This is useful if you don't have OpenSSL 1.1
# See https://stackoverflow.com/questions/18039401/how-can-i-transform-between-the-two-styles-of-public-key-format-one-begin-rsa
key = STDIN.read
lines = key.split("\n")
only_key = lines[1..-2].join("")
without_header = only_key[32..-1]
new_key = without_header.chars.each_slice(64).map { |chars| chars.join }.join("\n")
puts "-----BEGIN RSA PUBLIC KEY-----"
puts new_key
puts "-----END RSA PUBLIC KEY-----"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment