Class: Key::Wheatstone

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

Overview

Wheatstone

The Wheatstone cipher (not to be confused with Playfair, also from the author) is a polyalphabetic stream cipher represented by a mechanical device with two synchronized rotors just like a clock called the Wheatstone Cryptograph.

For more details see members.aon.at/cipherclerk/Doc/Whetstone.html www.apprendre-en-ligne.net/crypto/instruments/index.html bit.ly/983rDL

All in all, this is a sliding system where the plain text “wheel/alphabet” slides over repetition of the ciphertext wheel/alphabet.

Constant Summary

BASE =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

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

- (Wheatstone) initialize(start, plw = BASE, ctw = BASE)

initialize

Either we give full alphabets are keys or just words. In the latter case, generate full alphabets as usual through #keyshuffle



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/key/wheatstone.rb', line 38

def initialize(start, plw = BASE, ctw = BASE)
  super(start)
  if plw.length != BASE.length then
    #
    # Assume plw is a word we use as a base to generate an alphabet with #keyshuffle including space (as +)
    #
    plw = keyshuffle(plw, BASE)
  end
  plw = "+" + plw

  if ctw.length != BASE.length then
    #
    # Assume ctw is a word we use as a base to generate an alphabet with #keyshuffle
    #
    ctw = keyshuffle(ctw, BASE)
  end

  # We use array versions of the keys
  #
  @aplw = plw.each_char.to_a
  @actw = ctw.each_char.to_a

  # We always start at the space (named +) on plaintext
  #
  @curpos = 0

  # Starting letter for ciphertext is not the first one, set ctpos to its position
  #
  @ctpos = ctw.index(start)

  @l_aplw = @aplw.size
  @l_actw = @actw.size
end

Instance Attribute Details

- (Object) actw

Returns the value of attribute actw



29
30
31
# File 'lib/key/wheatstone.rb', line 29

def actw
  @actw
end

- (Object) aplw

Returns the value of attribute aplw



29
30
31
# File 'lib/key/wheatstone.rb', line 29

def aplw
  @aplw
end

- (Object) ctpos

Returns the value of attribute ctpos



29
30
31
# File 'lib/key/wheatstone.rb', line 29

def ctpos
  @ctpos
end

- (Object) curpos

Returns the value of attribute curpos



29
30
31
# File 'lib/key/wheatstone.rb', line 29

def curpos
  @curpos
end

Instance Method Details

- (Object) decode(c)



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/key/wheatstone.rb', line 85

def decode(c)
  a = @actw.index(c)
  if a <= @ctpos
    # we have made a turn
    off = (a + @l_actw) - @ctpos
  else
    off = a - @ctpos
  end
  @ctpos = a
  @curpos = (@curpos + off) % @l_aplw
  @aplw[@curpos]
end

- (Object) encode(c)

– initialize



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/key/wheatstone.rb', line 72

def encode(c)
  a = @aplw.index(c)
  if a <= @curpos
    # we have made a turn
    off = (a + @l_aplw) - @curpos
  else
    off = a - @curpos
  end
  @curpos = a
  @ctpos = (@ctpos + off) % @l_actw
  @actw[@ctpos]
end