Class: Cipher::Playfair

Inherits:
BiGrammatic show all
Defined in:
lib/cipher/playfair.rb

Overview

Playfair

Bigrammatic substitution through a square alphabet

Alphabet is missing Q by default

Instance Attribute Summary

Attributes inherited from Substitution

#key

Instance Method Summary (collapse)

Methods inherited from BiGrammatic

#check_input, #decode

Methods inherited from Substitution

#decode

Methods inherited from SimpleCipher

#decode

Constructor Details

- (Playfair) initialize(key, type = Key::Playfair::WITH_Q)

initialize



23
24
25
# File 'lib/cipher/playfair.rb', line 23

def initialize(key, type = Key::Playfair::WITH_Q)
  super(Key::Playfair, key, type)
end

Instance Method Details

- (Object) encode(plain_text)

encode



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cipher/playfair.rb', line 29

def encode(plain_text)
  #
  # Do expand the double letters inside
  #
  plain = plain_text.expand

  # Add a "X" if of odd length
  #
  if plain.length.odd? then
    plain << "X"
  end

  check_input(plain)

  cipher_text = plain.scan(/../).inject('') do |text, pt|
    text + @key.encode(pt)
  end
  return cipher_text
end