Old high school files. Lessson notes/codes/projects etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hsf/ma5/rsa/test.rb

56 lines
961 B

3 years ago
#!/usr/bin/ruby
3 years ago
require_relative "rsa"
3 years ago
Primes = File.read("smallprimes.txt").chomp.split(",").map do |pstr|
3 years ago
pstr.to_i
3 years ago
end
3 years ago
def get_random_primes
puts "Fetching random primes..."
p1, p2 = Primes.sample, Primes.sample
return p1, p2
end
def choose_primes
print "1st prime: "
p1 = gets.chomp.to_i
print "2nd prime: "
p2 = gets.chomp.to_i
return p1, p2
end
puts "[1] Random primes"
puts "[2] Choose primes"
ch = gets.chomp.to_i
case ch
when 1
p1, p2 = get_random_primes
when 2
p1, p2 = choose_primes
else
p1, p2 = get_random_primes
end
3 years ago
puts "p1=#{p1}, p2=#{p2}"
puts
key = RSA::Key.new(p1, p2)
3 years ago
puts key.inspect
3 years ago
print "Message to be encrypted: "
msg = gets.chomp
data = RSA::Data.new(msg)
3 years ago
puts
3 years ago
3 years ago
data.encrypt!(key)
puts "Encrypted bytes (hex): #{data.raw}"
puts "Encrypted bytes (dec): #{data.to_si}"
3 years ago
puts
3 years ago
3 years ago
data.decrypt!(key)
puts "Decrypted bytes (hex): #{data.raw}"
puts "Decrypted bytes (dec): #{data.to_si}"
puts "Decrypted msg: #{data.to_s}"
3 years ago