module Autokey

Encryption/decryption using autokey ciphers (en.wikipedia.org/wiki/Autokey_cipher)

Public Instance Methods

build_tabula_recta() click to toggle source

Public: generates the tabula recta needed to encrypt text via autokey cipher.

Parameter(s)

None.

Return Value

Array: a two-dimensional array of characters (26 x 26).

Example

build_tabula_recta

(See en.wikipedia.org/wiki/Tabula_recta for an example.)

# File lib/dekryptos/autokey.rb, line 95
def build_tabula_recta
  table = []
  row = alphabet

  alphabet.each do |letter|
    table << row.rotate(row.index(letter))
  end

  table
end
decrypt(ciphertext, table, keyword) click to toggle source

Public: decrypts ciphertext via autokey cipher.

For each pair formed by the i-th index of the key and ciphertext, we look up the ciphertext letter in the key letter row and add the corresponding letter from the top row to the plaintext. Because the text is enciphered with itself, we continuously append the deciphered text to the key as we go along.

Parameter(s)

ciphertext - String: the text to be decrypted. table - Array: the table generated by build_tabula_recta. keyword - String: the keyword used to encrypt the text.

Return Value

String: the decrypted text.

Example

+decrypt(‘KKRPVYSTWTWP’, build_tabula_recta, ‘KRYPTOS’)+ +=> “ATTACKATDAWN”+

# File lib/dekryptos/autokey.rb, line 67
def decrypt(ciphertext, table, keyword)
  plaintext = ''
  ciphertext.upcase!
  keyword.upcase!

  key = (keyword).split('')

  ciphertext.tr('?', '').split('').each_with_index do |letter, index|
    plaintext << table[0][table[table[0].index(key[index])].index(letter)]
    key << table[0][table[table[0].index(key[index])].index(letter)]
  end

  plaintext
end
encrypt(plaintext, table, keyword) click to toggle source

Public: encrypts plaintext via autokey cipher.

We generate our key by prepending the keyword to the plaintext. Then, for each pair formed by the i-th index of the key and the plaintext, we look up that (row, column) coordinate in the tabula recta and add it to the ciphertext.

Parameter(s)

plaintext - String: the text to be encrypted. table - Array: the table generated by build_tabula_recta. keyword - String: the keyword to be prepended to the plaintext to form the key.

Return Value

String: the encrypted text.

Example

+encrypt(‘ATTACKATDAWN’, build_tabula_recta, ‘KRYPTOS’)+ +=> “KKRPVYSTWTWP”+

# File lib/dekryptos/autokey.rb, line 32
def encrypt(plaintext, table, keyword)
  ciphertext = ''
  plaintext.upcase!
  keyword.upcase!

  key = (keyword + plaintext).split('')

  plaintext.tr('?', '').split('').each_with_index do |letter, index|
    ciphertext << table[table[0].index(letter)][table[0].index(key[index])]
  end

  ciphertext
end

Private Instance Methods

alphabet() click to toggle source

Private: generates an alphabet.

Parameter(s)

None.

Return Value

Array: an array comprising the letters of the alphabet.

# File lib/dekryptos/autokey.rb, line 115
def alphabet
  ('A'..'Z').to_a
end