2 Replies - 3561 Views - Last Post: 01 February 2012 - 10:21 AM Rate Topic: -----

#1 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

How do you use OpenPGP for ruby?

Posted 30 January 2012 - 05:31 PM

Hey, so I'm kind of an encryption noob who used to think that anything you pass over then internet is up for grabs to any malicious eves droppers, so I just avoided network communication of decryption/encryption algorithms/keys where possible. Then I got curious about how SSL types of technologies worked and I came across this http://www.youtube.c...h?v=jJrICB_HvuI which really helped me understand the concept of asymmetrical encryption where a message can be encrypted by one party to the point where they are unable to decrypt the message at all (and yet the other party will be able to decrypt it using the 'private' half of the encryption key).

Anyway, I'm noticing the ruby gem OpenPGP for ruby, and I'd like to experiment with it, just to do a little IRC hello world type of string encryption app, but I'm not sure exactly where to go to learn how to use that gem.

What's the first thing you do to learn how to use something like OpenPGP for ruby? The only information I can find is at its git repo https://github.com/bendiken/openpgp

It looks like there's a chance it's a little bugged too, because all I can get this code to do is hang on the last line:
require 'rubygems'
require 'openpgp'

gpg = OpenPGP::Engine::GnuPG.new(:homedir => '~/.gnupg')
key_id = gpg.gen_key({
  :key_type      => 'RSA',
  :key_length    => 256,
  :subkey_type   => 'ELG-E',
  :subkey_length => 256,
  :name          => 'J. Random Hacker',
  :comment       => nil,
  :email         => 'jhacker@example.org',
  :passphrase    => 'secret passphrase',
})



I'm on ruby 1.9.2 on a Debian system.

Is This A Good Question/Topic? 0
  • +

Replies To: How do you use OpenPGP for ruby?

#2 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: How do you use OpenPGP for ruby?

Posted 30 January 2012 - 09:35 PM

I think your'e right about a bug. According to github, the repo hasn't had a commit since 2010 and there are recent issues posted about problems with Ruby 1.9.2 and hanging when creating new keypairs. If you want to look into OpenPGP more, you can try their website (http://www.openpgp.org/), which admittedly doesn't give much. Other than that, I haven't much experience. Interesting stuff though. May need to take a deeper look when I can get pass my piles of work.

This post has been edited by Skaggles: 30 January 2012 - 09:36 PM
Reason for edit:: typos

Was This Post Helpful? 1
  • +
  • -

#3 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: How do you use OpenPGP for ruby?

Posted 01 February 2012 - 10:21 AM

Thanks, I'm glad it wasn't just me, I had been banging my head trying to snoop through their code and figure out what I was doing wrong.

I did find something that seems to work. It's based on the OpenSSL library.

http://nunojob.wordp...ecrypt-in-ruby/
require 'openssl'
require 'base64'  # he had a typo here

class Rudolph
  class Crypt
    def initialize data_path
      @data_path = data_path
      @private   = get_key 'id_rsa'
      @public    = get_key 'id_rsa.pub'
    end

    def encrypt_string message
      Base64::encode64(@public.public_encrypt(message)).rstrip
    end

    def decrypt_string message
      @private.private_decrypt Base64::decode64(message)
    end

    def self.generate_keys data_path
      rsa_path = File.join(data_path, 'rsa')
      privkey  = File.join(rsa_path, 'id_rsa')
      pubkey   = File.join(rsa_path, 'id_rsa.pub')
      unless File.exists?(privkey) || File.exists?(pubkey)
        keypair  = OpenSSL::PKey::RSA.generate(1024)
        Dir.mkdir(rsa_path) unless File.exist?(rsa_path)
        File.open(privkey, 'w') { |f| f.write keypair.to_pem } unless File.exists? privkey
        File.open(pubkey, 'w') { |f| f.write keypair.public_key.to_pem } unless File.exists? pubkey
      end
    end

    private
    def get_key filename
      OpenSSL::PKey::RSA.new File.read(File.join(@data_path, 'rsa', filename))
    end
  end
end



Usage (creates a "~/.delme" directory):
keys_dir = File.expand_path "~/.delme"

Dir.mkdir keys_dir unless Dir.exists? keys_dir


# Generate the keys (You can use ssh-keygen -t rsa -b 2048 for safer encryption)
Rudolph::Crypt.generate_keys keys_dir

# Make an encrypting/decrypting object
s = Rudolph::Crypt.new keys_dir

secret_msg = s.encrypt_string "This is private"

exposed_msg = s.decrypt_string secret_msg

puts "The secret message was encrypted and then decrypted to:  \"#{exposed_msg}\""


# `rm -rf ~/.delme` # consider deleting that directory that was made


This post has been edited by NotarySojac: 01 February 2012 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1