This commit is contained in:
Vitor Oliveira 2021-11-01 23:45:20 -07:00
parent d2445070fb
commit 4fc9eba58a
5 changed files with 63 additions and 70 deletions

View file

@ -30,4 +30,3 @@ class CaesarCipher
end.join
end
end

View file

@ -19,4 +19,3 @@ class CaesarCipherTest < Minitest::Test
assert_equal decrypted, plaintext
end
end

View file

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

View file

@ -1,48 +1,39 @@
class Node
attr_accessor :data, :parent, :rank
def initialize(data)
@data = data
@parent = self
@rank = 0
end
def parent
@parent
end
def parent=(parent)
@parent = parent;
end
def rank
@rank
end
def rank=(rank)
@rank = rank
end
attr_accessor :data, :parent, :rank, :parent, :rank
def initialize(data)
@data = data
@parent = self
@rank = 0
end
end
class DisjointSets
def make_set(d)
Node.new(d)
end
def make_set(d)
Node.new(d)
end
def find_set(x)
raise ArgumentError unless x.class <= Node
x.parent=(find_set(x.parent)) unless x.parent == x
x.parent
end
def find_set(x)
raise ArgumentError unless x.class <= Node
def union_set(x, y)
px = find_set(x)
py = find_set(y)
return if px == py
if px.rank > py.rank
py.parent = px
elsif py.rank > px.rank
px.parent = py
else
px.parent = py
py.rank += 1
end
x.parent = (find_set(x.parent)) unless x.parent == x
x.parent
end
def union_set(x, y)
px = find_set(x)
py = find_set(y)
return if px == py
if px.rank > py.rank
py.parent = px
elsif py.rank > px.rank
px.parent = py
else
px.parent = py
py.rank += 1
end
end
end
ds = DisjointSets.new
@ -53,4 +44,4 @@ ds.union_set(one, two)
puts ds.find_set(one) == ds.find_set(two) # should be true
ds.union_set(one, three)
puts ds.find_set(two) == ds.find_set(three) # should be true
puts one.rank + two.rank + three.rank == 1 # should be true
puts one.rank + two.rank + three.rank == 1 # should be true

View file

@ -3,13 +3,13 @@
# Reference: https://en.wikipedia.org/wiki/Ohm's_law
def ohms_law(i, r)
if(i > 0 && r > 0)
if i > 0 && r > 0
"The voltage for given #{i} ampheres current and #{r} ohms resistance is #{r * i} volts."
else
raise
end
rescue
"Error: Please provide valid inputs only!"
rescue StandardError
'Error: Please provide valid inputs only!'
end
# Valid inputs
@ -25,7 +25,7 @@ puts(ohms_law(5, -10))
# Error: Please provide valid inputs only!
puts(ohms_law(-5, -10))
# Error: Please provide valid inputs only!
puts(ohms_law(5, "10"))
puts(ohms_law(5, '10'))
# Error: Please provide valid inputs only!
puts(ohms_law("a", 10))
puts(ohms_law('a', 10))
# Error: Please provide valid inputs only!