blob: c7111e428150d3ea61d34b8602e7141386ef69fe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#!/usr/bin/env ruby
PLAIN_ALPHABET = ('A'..'Z').to_a
def cipher_alphabet_for(key)
PLAIN_ALPHABET.rotate(key)
end
def decode(ciphertext, key)
mapping = Hash[cipher_alphabet_for(key).zip(PLAIN_ALPHABET)]
ciphertext.chars.map { |x| mapping[x] || ' ' }.join
end
key = ENV.fetch("KEY", 5).to_i
ciphertext = ENV.fetch("CIPHERTEXT", "RTAJ TZY FY IFBS")
width = (PLAIN_ALPHABET.size*2)-1
puts "# Caesar Cipher"
puts ""
puts "| --------------- | --------------------------------------------------- |"
puts "| Plain Alphabet | #{PLAIN_ALPHABET.join(",").ljust(width)} |"
puts "| Cipher Alphabet | #{cipher_alphabet_for(key).join(",").ljust(width)} |"
puts "| --------------- | --------------------------------------------------- |"
puts "| Key | #{key.to_s.ljust(width)} |"
puts "| Ciphertext | #{ciphertext.to_s.ljust(width)} |"
puts "| Plaintext | #{decode(ciphertext, key).to_s.ljust(width)} |"
|