Class: Key::Playfair

Inherits:
SKey show all
Includes:
Crypto
Defined in:
lib/key/playfair.rb

Overview

Playfair

The Playfair cipher was invented by Charles Wheatstone but popularized by his friend, the Baron Playfair.

Description of the cipher on en.wikipedia.org/wiki/Playfair_cipher JS version on www.simonsingh.net/The_Black_Chamber/playfaircipher.htm

Playfair is a bigrammatic cipher but it does not use a Polybius square

Constant Summary

CODE_WORD =
[ 0, 1, 2, 3, 4 ]
WITH_J =
0
WITH_Q =
1
PLF_ENCODE =

– gen_rings

1
PLF_DECODE =
4

Constants included from Crypto

Crypto::BASE

Constants inherited from SKey

SKey::BASE

Instance Attribute Summary (collapse)

Attributes inherited from SKey

#alpha, #ralpha

Instance Method Summary (collapse)

Methods included from Crypto

#addmod10, #chainadd, #expand5to10, #find_hole, #keyshuffle, #normalize, #p1_encode, #str_to_numeric, #submod10

Constructor Details

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

initialize



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/key/playfair.rb', line 34

def initialize(key, type = WITH_Q)
  super(key.gsub(%r{\s*}, ''))
  @alpha = Hash.new
  if type == WITH_J then
    @base = 'ABCDEFGHIJKLMNOPRSTUVWXYZ'
  else
    @base = 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
  end
  @full_key = (@key + @base).condensed
  gen_rings()
end

Instance Attribute Details

- (Object) full_key (readonly)

Returns the value of attribute full_key



30
31
32
# File 'lib/key/playfair.rb', line 30

def full_key
  @full_key
end

Instance Method Details

- (Object) decode(c)

decode



72
73
74
# File 'lib/key/playfair.rb', line 72

def decode(c)
  return encode_or_decode(c, PLF_DECODE)
end

- (Object) encode(c)

encode



66
67
68
# File 'lib/key/playfair.rb', line 66

def encode(c)
  return encode_or_decode(c, PLF_ENCODE)
end

- (Object) gen_rings

gen_rings



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/key/playfair.rb', line 48

def gen_rings
  ind = 0
  word = @full_key.dup
  CODE_WORD.each do |i|
    CODE_WORD.each do |j|
      c = word[ind]
      @alpha[c.chr] = [ i, j ]
      @ralpha[[ i, j ]] = c.chr
      ind += 1
    end
  end
end