Skip to content

Instantly share code, notes, and snippets.

@aburgd
Last active December 1, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aburgd/a76ee10ed393fa7b4f3a to your computer and use it in GitHub Desktop.
Save aburgd/a76ee10ed393fa7b4f3a to your computer and use it in GitHub Desktop.
Diffie-Hellman Key Exchange calculations program
class Diffie
def self.publicKey(base, modPrime, aSecret)
# puts "Enter your shared prime (g): "
# base = Integer(gets.chomp)
# puts "Enter your second prime (p): "
# modPrime = Integer(gets.chomp)
# puts "Enter your secret prime (a): "
# aSecret = Integer(gets.chomp)
publicKey = base ** aSecret % modPrime
print "Share this with your contact: " + publicKey.to_s
end
def self.shareSecret(publicKey, modPrime, aSecret)
# puts "Enter the shared secret from your contact (A): "
# publicKey = Integer(gets.chomp)
# puts "Enter your second prime (p) :"
# modPrime = Integer(get.chomp)
# puts "Enter your secret prime (a): "
# aSecret = Integer(gets.chomp)
shareSecret = publicKey ** aSecret % modPrime
print "Compare this with your contact: " + shareSecret.to_s
end
end
puts "Are you computing the public key or the shared secret?"
answer = gets.chomp
if answer.downcase.eql? "public key" then
puts "Enter your shared prime (g): "
base = Integer(gets.chomp)
puts "Enter your second prime (p): "
modPrime = Integer(gets.chomp)
puts "Enter your secret prime (a): "
aSecret = Integer(gets.chomp)
puts "Share this with your contact: " + Diffie.publicKey(base, modPrime, aSecret).to_s + "\n"
elsif answer.downcase.eql? "shared secret" then
puts "Enter the shared secret from your contact (A): "
publicKey = Integer(gets.chomp)
puts "Enter your second prime (p) :"
modPrime = Integer(gets.chomp)
puts "Enter your secret prime (a): "
aSecret = Integer(gets.chomp)
puts "Compare this with your contact: " + Diffie.shareSecret(publicKey, modPrime, aSecret).to_s + "\n"
else
puts "Goodbye."
end
$ ruby DHKE.rb
Are you computing the public key or the shared secret?
public key
Enter your shared prime (g):
7
Enter your second prime (p):
5
Enter your secret prime (a):
2
Share this with your contact: 4Share this with your contact:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment