I looked around and couldn’t find a pure-ruby implementation of Diffie-Hellman key exchange. Diffie-Hellman key exchange is a nifty way to end up with the same shared secret between Alice and Bob without ever sending the secret key to the other side. It’s used in ISAKMP, SSH and a host of other crypto-based protocols. The code for Diffie-Hellman in Ruby is unbelievably terse to the point you wonder if you actually got it working right. Two things come in handy: Ruby has open classes that you can extend and Ruby has built-in Bignum support. Integers don’t overflow in Ruby, they just keep expanding.
We are going to need to extend the built-in Integer class with two methods. The first one does modular-exponentiation, while the second one counts the number of bits set in a given number. Since Fixnum’s and Bignum’s in Ruby derive from Integer, these two methods are automatically available for all numbers.
First we extend the Integer class:
class Integer # Compute self ^ e mod m def mod_exp e, m result = 1 b = self while e > 0 result = (result * b) % m if e[0] == 1 e = e >> 1 b = (b * b) % m end return result end # A roundabout, slow but fun way of counting bits. def bits_set ("%b" % self).count('1') end end
and here’s the Diffie-Hellman class:
class DH attr_reader :p, :g, :q, :x, :e # p is the prime, g the generator and q order of the subgroup def initialize p, g, q @p = p @g = g @q = q end # generate the [secret] random value and the public key def generate tries=16 tries.times do @x = rand(@q) @e = self.g.mod_exp(@x, self.p) return @e if self.valid? end raise ArgumentError, "can't generate valid e" end # validate a public key def valid? _e = self.e _e and _e.between?(2, self.p-2) and _e.bits_set > 1 end # compute the shared secret, given the public key def secret f f.mod_exp(self.x, self.p) end end
and finally, here’s how you use it
alice = DH.new(53, 5, 23) bob = DH.new(53, 5, 15) alice.generate bob.generate alice_s = alice.secret(bob.e) bob_s = bob.secret(alice.e) puts alice_s puts bob_s
alice_s and bob_s should/will be the same.

Pingback: cool caravans
Pingback: buy guest beds
Pingback: Buy Facebook Fans
Pingback: Hanna
Pingback: clothing
Pingback: swivelOutdoorBarStools
Pingback: guaranteed rankings
Pingback: Escorts in london
Pingback: Vacations in Siesta Key
Pingback: Dallas Family Portrait Photographer
Pingback: Dallas Boudoir Photographer
Pingback: downtown dentist toronto
Pingback: Tesla Turbine
Pingback: bodybuilding supplement
Pingback: Escort girl Paris
Pingback: best bcaa
Pingback: webcam chat
Pingback: scary
Pingback: flights to pakistan
Pingback: muay thai training
Pingback: florida unemployment benefits
Pingback: Dentist Reviews
Pingback: lol
Pingback: Head Shop
Pingback: how to start a cleaning business
Pingback: 50 State Legal
Pingback: fat burning foods
Pingback: free apple ipad 3
Pingback: Atlanta Limousine Service
Pingback: buy google+
Pingback: anti wrinkle placenta cream
Pingback: follow us
Pingback: Prenatal Massage New York City
Pingback: black hair
Pingback: Holiday Homes
Pingback: Tonin
Pingback: hcg diet recipes
Pingback: investment properties
Pingback: smithfield landscaping
Pingback: iphone
Pingback: name numerology calculator free
Pingback: Organo Gold
Pingback: song lyrics
Pingback: Project Payday
Pingback: bingo game cards
Pingback: touch phone
Pingback: como puedo quedar embarazada
Pingback: hypnosis weight loss
Pingback: Outback coupons
Pingback: shutter advertising
Pingback: network marketing
Pingback: Low Interest Payday Loans
Pingback: gynemax
Pingback: what is serotonin
Pingback: how to work from home
Pingback: mma training
Pingback: Divorce Attorneys Oklahoma
Pingback: where to buy coleman grill
Pingback: vw t5 campers
Pingback: respiratory therapy
Pingback: How to Transfer Music from iPhone to Computer
Pingback: Squidoo lenses
Pingback: At Home Laser Hair Removal
Pingback: Free Calculator
Pingback: commercial inspections
Pingback: NYC Hotels
Pingback: Glute workouts
Pingback: backlinks
Pingback: Notarztkurs berlin
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Payday Loans
Pingback: kids halloween costumes
Pingback: Scott Tucker Racing
Pingback: window cleaners
Pingback: tree surgeons Cardiff
Pingback: get more info
Pingback: climbing frames
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Payday Loans
Pingback: Scott Tucker Racing
Pingback: Renegade Millionaire System
Pingback: How To Lose Stomach Fat In Three Steps
Pingback: Fancy dress costumes
Pingback: clearwater homes
Pingback: visit here for insurance rss feed
Pingback: music video production nyc
Pingback: www zeek reward. com
Pingback: BPO Companies
Pingback: Scott Tucker Racing
Pingback: Scott Tucker Racing
Pingback: Scott Tucker CBS
Pingback: Scott Tucker CBS
Pingback: Scott Tucker Leawood
Pingback: Scott Tucker Leawood
Pingback: Scott Tucker Leawood
Pingback: REO Companies
Pingback: broker price opinion companies
Pingback: Website Hosting Solutions
Pingback: natural remedies for cold sores
Pingback: bedding sets