Coder Social home page Coder Social logo

philr / putty-key Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 1.0 154 KB

Ruby PuTTY private key (.ppk) library. Supports reading and writing with a refinement to OpenSSL::PKey to allow key conversion.

License: MIT License

Ruby 100.00%
ruby putty ppk putty-private-key puttygen ssh

putty-key's Introduction

PuTTY::Key

RubyGems Tests Coverage Status

PuTTY::Key is a Ruby implementation of the PuTTY private key (ppk) format (versions 2 and 3), handling reading and writing .ppk files. It includes a refinement to Ruby's OpenSSL library to add support for converting DSA, EC and RSA private keys to and from PuTTY private key files. This allows OpenSSH ecdsa, ssh-dss and ssh-rsa private keys to be converted to and from PuTTY's private key format.

Installation

To install the PuTTY::Key gem, run the following command:

gem install putty-key

To add PuTTY::Key as a Bundler dependency, add the following line to your Gemfile:

gem 'putty-key'

Compatibility

PuTTY::Key is compatible with Ruby MRI 2.1.0+ and JRuby 9.1.0.0+.

Formats

Format 2 and 3 .ppk files are supported. Format 1 (not supported) was only used briefly early on in the development of the .ppk format and was never included in a PuTTY release. Format 2 is supported by PuTTY version 0.52 onwards. Format 3 is supported by PuTTY version 0.75 onwards. By default, PuTTY::Key::PPK saves files using format 2. Format 3 can be selected with the format parameter.

libargon2 is required to load and save encrypted format 3 files. Binaries are typically available with your OS distribution. For Windows, binaries are available from the argon2-windows repository. Use either Argon2OptDll.dll for CPUs supporting AVX or Argon2RefDll.dll otherwise.

Usage

To use PuTTY::Key, it must first be loaded with:

require 'putty/key'

The included refinement to Ruby's OpenSSL library can then either be activated in the lexical scope (file, class or module) where it will be used with:

using PuTTY::Key

or installed globally by calling:

PuTTY::Key.global_install

The following sections give examples of how PuTTY::Key can be used.

Converting a .pem formatted key file to an unencrypted .ppk file

require 'openssl'
require 'putty/key'
using PuTTY::Key    # or PuTTY::Key.global_install

pem = File.read('key.pem', mode: 'rb')
pkey = OpenSSL::PKey.read(pem)
ppk = pkey.to_ppk
ppk.comment = 'Optional comment'
ppk.save('key.ppk')

Use ppk.save('key.ppk', format: 3) to save a format 3 file instead of format 2.

Generating a new RSA key and saving it as an encrypted .ppk file

require 'openssl'
require 'putty/key'
using PuTTY::Key    # or PuTTY::Key.global_install

rsa = OpenSSL::PKey::RSA.generate(2048)
ppk = rsa.to_ppk
ppk.comment = 'RSA 2048'
ppk.save('rsa.ppk', 'Passphrase for encryption')

Use ppk.save('rsa.ppk', 'Passphrase for encryption', format: 3) to save a format 3 file instead of format 2.

Converting an unencrypted .ppk file to .pem format

require 'openssl'
require 'putty/key'
using PuTTY::Key    # or PuTTY::Key.global_install

ppk = PuTTY::Key::PPK.new('key.ppk')
pkey = OpenSSL::PKey.from_ppk(ppk)
pem = pkey.to_pem
File.write('key.pem', pem, mode: 'wb')

Decrypting a .ppk file and re-saving it without encryption

require 'putty/key'

ppk = PuTTY::Key::PPK.new('rsa.ppk', 'Passphrase for encryption')
ppk.save('rsa-plain.ppk')

Use ppk.save('rsa-plain.ppk', format: 3) to save a format 3 file instead of format 2.

API Documentation

API documentation for PuTTY::Key is available on RubyDoc.info.

License

PuTTY::Key is distributed under the terms of the MIT license. A copy of this license can be found in the included LICENSE file.

GitHub Project

Source code, release information and the issue tracker can be found on the PuTTY::Key GitHub project page.

putty-key's People

Contributors

philr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

rchekaluk

putty-key's Issues

Could not parse PKey: unsupported

Hi Guys, I hit some issue:

D:\git\vgwp\dbasm19>vagrant version
Installed Version: 2.4.1
Latest Version: 2.4.1

D:\git\vgwp\dbasm19>vagrant plugin install vagrant-multi-putty
Installing the 'vagrant-multi-putty' plugin. This can take a few minutes...
Fetching putty-key-1.1.1.gem
Fetching vagrant-multi-putty-1.6.0.gem
Installed the plugin 'vagrant-multi-putty (1.6.0)'!
D:\git\vgwp\dbasm19>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/git/vgwp/dbasm19/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa
D:\git\vgwp\dbasm19>vagrant putty
d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:114:in `read': Could not parse PKey: unsupported (OpenSSL::PKey::PKeyError)
        from d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:114:in `block in get_putty_key_file'
        from <internal:kernel>:90:in `tap'
        from d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:112:in `get_putty_key_file'
        from d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:94:in `putty_connect'
        from d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:53:in `block in execute'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/lib/vagrant/plugin/v2/command.rb:249:in `block in with_target_vms'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/lib/vagrant/plugin/v2/command.rb:238:in `each'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/lib/vagrant/plugin/v2/command.rb:238:in `with_target_vms'
        from d:/vghome/gems/3.1.4/gems/vagrant-multi-putty-1.6.0/lib/vagrant-multi-putty/command.rb:51:in `execute'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/lib/vagrant/cli.rb:67:in `execute'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/lib/vagrant/environment.rb:319:in `cli'
        from C:/Program Files/Vagrant/embedded/gems/gems/vagrant-2.4.1/bin/vagrant:248:in `<main>'

I have no problems to convert
D:\git\vgwp\dbasm19\.vagrant\machines\default\virtualbox\private_key
with puttygen 0.8 with a confirmation comment "Successfully imported foreign key (OpenSSH SSH-2 private key (new format)) [...].

What else I may check to investigate this issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.