Module: Transposition
- Defined in:
- lib/dekryptos/transposition.rb
Overview
Encryption/decryption using keyed columnar transposition (en.wikipedia.org/wiki/Transposition_cipher)
Instance Method Summary collapse
-
#decrypt(ciphertext, width) ⇒ Object
Public: decrypts text via keyed columnar transposition.
-
#encrypt(plaintext, width) ⇒ Object
Public: encrypts text via keyed columnar transposition.
Instance Method Details
#decrypt(ciphertext, width) ⇒ Object
Public: decrypts text via keyed columnar transposition.
The decryption method converts the ciphertext to an array, each row within which comprises width characters. Each row in the array is reversed and the entire array is transposed, after which the array is converted back to a string.
Parameter(s)
ciphertext - String: the text to be decrypted. width - Integer: the width of each row in the intermediary table required for transposition.
Return Value
String: the decrypted text.
Example
Assuming K3 is part 3 of the Kryptos sculpture: decrypt(decrypt(K3, 4), 48) => # Results in the plaintext
59 60 61 62 63 64 65 66 |
# File 'lib/dekryptos/transposition.rb', line 59 def decrypt(ciphertext, width) matrix = ciphertext.chars.each_slice(width).map(&:join) matrix.each_with_index do |_, index| matrix[index] = matrix[index].split('') end matrix.map(&:reverse).transpose.join('') end |
#encrypt(plaintext, width) ⇒ Object
Public: encrypts text via keyed columnar transposition.
The encryption method converts the plaintext to an array, each row within which comprises width characters. The array is then transposed and each row is reversed, after which the array is converted back to a string.
Parameter(s)
plaintext - String: the text to be encrypted. width - Integer: the width of each row in the intermediary table required for transposition.
Return Value
String: the encrypted text.
Example
Assuming pt is the plaintext version of K3: encrypt(encrypt(pt, 7), 84) => # Results in K3
30 31 32 33 34 35 36 37 |
# File 'lib/dekryptos/transposition.rb', line 30 def encrypt(plaintext, width) matrix = plaintext.chars.each_slice(width).map(&:join) matrix.each_with_index do |_, index| matrix[index] = matrix[index].split('') end matrix.transpose.map(&:reverse).join('') end |