2021-11-02 07:45:20 +01:00
|
|
|
require 'prime'
|
2021-08-09 16:11:43 +02:00
|
|
|
|
|
|
|
def initialize(keys = {})
|
|
|
|
@e ||= keys[:e]
|
|
|
|
@n ||= keys[:n]
|
|
|
|
end
|
|
|
|
|
|
|
|
def cipher(message)
|
|
|
|
message.bytes.map do |byte|
|
2021-11-02 07:45:20 +01:00
|
|
|
cbyte = ((byte.to_i**e) % n).to_s
|
2021-08-09 16:11:43 +02:00
|
|
|
missing_chars = n.to_s.size - cbyte.size
|
2021-11-02 07:45:20 +01:00
|
|
|
'0' * missing_chars + cbyte
|
2021-08-09 16:11:43 +02:00
|
|
|
end.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def decipher(ciphed_message)
|
|
|
|
ciphed_message.chars.each_slice(n.to_s.size).map do |arr|
|
2021-11-02 07:45:20 +01:00
|
|
|
(arr.join.to_i**d) % n
|
|
|
|
end.pack('c*')
|
2021-08-09 16:11:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_keys
|
|
|
|
{ n: n, e: e }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def p
|
|
|
|
@p ||= random_prime_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def q
|
|
|
|
@q ||= random_prime_number
|
|
|
|
end
|
|
|
|
|
|
|
|
def n
|
|
|
|
@n ||= p * q
|
|
|
|
end
|
|
|
|
|
|
|
|
def totient
|
|
|
|
@totient ||= (p - 1) * (q - 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def e
|
|
|
|
@e ||= totient.downto(2).find do |i|
|
|
|
|
Prime.prime?(i) && totient % i != 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def d
|
|
|
|
@d ||= invmod(e, totient)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extended_gcd(a, b)
|
2021-11-02 07:45:20 +01:00
|
|
|
last_remainder = a.abs
|
|
|
|
remainder = b.abs
|
|
|
|
x = 0
|
|
|
|
last_x = 1
|
|
|
|
y = 1
|
|
|
|
last_y = 0
|
2021-08-09 16:11:43 +02:00
|
|
|
while remainder != 0
|
2021-11-02 07:45:20 +01:00
|
|
|
(quotient, remainder) = last_remainder.divmod(remainder)
|
|
|
|
last_remainder = remainder
|
2021-08-09 16:11:43 +02:00
|
|
|
x, last_x = last_x - quotient * x, x
|
|
|
|
y, last_y = last_y - quotient * y, y
|
|
|
|
end
|
|
|
|
|
2021-11-02 07:45:20 +01:00
|
|
|
[last_remainder, last_x * (a < 0 ? -1 : 1)]
|
2021-08-09 16:11:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def invmod(e, et)
|
|
|
|
g, x = extended_gcd(e, et)
|
2021-11-02 07:45:20 +01:00
|
|
|
raise 'The maths are broken!' if g != 1
|
|
|
|
|
2021-08-09 16:11:43 +02:00
|
|
|
x % et
|
|
|
|
end
|
|
|
|
|
|
|
|
def random_prime_number
|
|
|
|
number = Random.rand(1..1000)
|
2021-11-02 07:45:20 +01:00
|
|
|
number = Random.rand(1..1000) until Prime.prime?(number) || number == p || number == q
|
2021-08-09 16:11:43 +02:00
|
|
|
number
|
|
|
|
end
|
|
|
|
|
2021-11-02 07:45:20 +01:00
|
|
|
def main
|
|
|
|
puts 'Enter the message you want to encrypt and decrypt with RSA algorithm: '
|
|
|
|
message = gets.chomp.to_s
|
|
|
|
puts 'Encoded Text:'
|
2021-08-09 16:11:43 +02:00
|
|
|
puts cipher(message)
|
2021-11-02 07:45:20 +01:00
|
|
|
puts 'Decoded Text:'
|
2021-08-09 16:11:43 +02:00
|
|
|
puts decipher(cipher(message))
|
2021-11-02 07:45:20 +01:00
|
|
|
puts "p: #{p}"
|
|
|
|
puts "q: #{q}"
|
|
|
|
puts "e: #{e}"
|
|
|
|
puts "d: #{d}"
|
|
|
|
puts "totient: #{totient}"
|
2021-08-09 16:11:43 +02:00
|
|
|
end
|
|
|
|
|
2021-11-02 07:45:20 +01:00
|
|
|
main
|