Anonymous user
Vigenère cipher: Difference between revisions
→{{header|Ruby}}
(Updated second D entry) |
|||
Line 2,205:
=={{header|Ruby}}==
<lang Ruby>
BASE = 'A'.ord
SIZE = 'Z'.ord - BASE + 1
def
end
def
end
def
text.each_char.inject('') do |ciphertext, char|
▲ key_iterator = @key.chars.cycle
▲ offset = key_iterator.next.ord - BASE
▲ ((plain_char.ord - BASE).send(dir, offset) % SIZE + BASE).chr
end
end
Line 2,242 ⟶ 2,231:
Demonstration:
<lang Ruby>
plaintext = 'Beware the Jabberwock, my son! The jaws that bite, the claws that catch!'
key = 'Vigenere cipher'
ciphertext =
recovered =
puts "Original: #{plaintext}"
puts "Encrypted: #{ciphertext}"
puts "Decrypted: #{recovered}"</lang>
{{out}}
<pre>
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
=={{header|Rust}}==
|