module Monoalphabetic
Assorted tools for encrypting/decrypting monoalphabetic substitution ciphers (en.wikipedia.org/wiki/Substitution_cipher)
Public Instance Methods
rot_13(plaintext)
click to toggle source
Public: encrypts and decrypts text via ROT13 (en.wikipedia.org/wiki/ROT13).
-
Each letter is shifted by 13 places in the alphabet, e.g. A -> N.
-
Because encryption/decryption are symmetric, the same method is used to both encrypt and decrypt text.
Parameter(s)¶ ↑
plaintext
- String
: the text to be encrypted or decrypted.
Return Value¶ ↑
String
: the encrypted/decrypted text.
Example¶ ↑
+rot_13(‘HELLO’)+ +=> “URYYB”+
# File lib/dekryptos/monoalphabetic.rb, line 35 def rot_13(plaintext) fail CharacterError, 'Plaintext must be a string' unless plaintext.respond_to? :upcase letters = plaintext.upcase.split('') ciphertext = [] letters.each do |letter| fail CharacterError, 'Plaintext must be letters only' unless alphabet.include? letter idx_to_check = alphabet.index(letter) if idx_to_check < 13 ciphertext << alphabet[idx_to_check + 13] else ciphertext << alphabet[idx_to_check - 13] end end ciphertext.join('') end