Skip to content

Instantly share code, notes, and snippets.

@zszugyi
Created November 30, 2011 02:18
Show Gist options
  • Select an option

  • Save zszugyi/1407691 to your computer and use it in GitHub Desktop.

Select an option

Save zszugyi/1407691 to your computer and use it in GitHub Desktop.
class to convert the public key part of an RSA key to the .NET XML format
require 'openssl'
require 'base64'
class RSAKeyToXmlConverter
def initialize rsa_key_string
@key = OpenSSL::PKey::RSA.new(rsa_key_string)
end
def to_public_key
<<XML_KEY
<RSAKeyValue>
<Modulus>
#{to_base_64(@key.n)}
</Modulus>
<Exponent>
#{to_base_64(@key.e)}
</Exponent>
</RSAKeyValue>
XML_KEY
end
private
def to_base_64 num
hexstring = num.to_s(16).downcase
Base64.encode64([hexstring].pack('H*')).split("\n").join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment