Opening RSA secret key from Ruby

I think I know how to create custom RSA encrypted keys, but how can I read one encrypted like ssh-keygen?

I know I can do this:

OpenSSL::PKey::RSA.new(File.read('private_key'))

But then OpenSSL asks me for the passphrase ... How do I pass it to OpenSSL as a parameter?

And how can I create a compatible one generated by ssh-keygen?

I am doing something like this to create secret keys:

pass = '123456'
key = OpenSSL::PKey::RSA.new(1024)
key = "0000000000000000#{key.to_der}"
c = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
c.encrypt
c.key = Digest::SHA1.hexdigest(pass).unpack('a2' * 32).map {|x| x.hex}.pack('c' * 32)
c.iv = iv
encrypted_key = c.update(key)
encrypted_key << c.final

In addition, the keys generated by OpenSSL :: PKey :: RSA.new (1024) (without encryption) do not work when trying to enter the system without a password (i.e. I copy the public key to the server and use it to log in) .

Also, when I open the ssh-keygen file through OpenSSL and then check its contents, it appears to have extra characters at the beginning and end of the key. This is normal?

, . ?

+5
2

. Net:: SSH, :

Net::SSH::KeyFactory.load_private_key 'keyfile', 'passphrase'

, , OpenSSL PKey:: RSA.new, ... , , , OpenSSL Net:: SSH... , - .

, SSH... , , : P... ,

-3

:

http://stuff-things.net/2008/02/05/encrypting-lots-of-sensitive-data-with-ruby-on-rails/

:

OpenSSL:: PKey:: RSA.new(File.read('private_key'), 'passphrase')

.

+9

All Articles