Refactor x2

master
E. Almqvist 3 years ago
parent 7de25c02d8
commit fdaf12ec58
  1. 33
      ma5/rsa/rsa.rb
  2. 12
      ma5/rsa/test.rb

@ -63,13 +63,17 @@ module RSA
return str
end
def inspect(endchar="\n")
def to_si
return @data.join " "
end
def to_s
pattern = "c" * @data.length
return "\'#{@data.pack(pattern)}\'#{endchar}"
return @data.pack(pattern)
end
def encrypt(pubkey)
e, n = pubkey
private def crypt(key)
e, n = key
crypt = []
@data.each do |c|
cr = (c ** e) % n
@ -79,23 +83,20 @@ module RSA
return crypt
end
def decrypt(privkey)
d, n = privkey
crypt = []
@data.each do |c|
cr = (c ** d) % n
crypt << cr
end
def encrypt(key)
self.crypt(key.pubkey)
end
return crypt
def decrypt(key)
self.crypt(key.privkey)
end
def encrypt!(pubkey)
@data = self.encrypt(pubkey)
def encrypt!(key)
@data = self.encrypt(key)
end
def decrypt!(privkey)
@data = self.decrypt(privkey)
def decrypt!(key)
@data = self.decrypt(key)
end
end
end

@ -42,11 +42,13 @@ msg = gets.chomp
data = RSA::Data.new(msg)
puts
data.encrypt!(key.pubkey)
puts "Encrypted bytes: #{data.raw}"
data.encrypt!(key)
puts "Encrypted bytes (hex): #{data.raw}"
puts "Encrypted bytes (dec): #{data.to_si}"
puts
data.decrypt!(key.privkey)
puts "Decrypted bytes: #{data.raw}"
puts "Decrypted msg: #{data.inspect}"
data.decrypt!(key)
puts "Decrypted bytes (hex): #{data.raw}"
puts "Decrypted bytes (dec): #{data.to_si}"
puts "Decrypted msg: #{data.to_s}"

Loading…
Cancel
Save