Class: SolitaireCipher

Inherits:
Object show all
Defined in:
lib/quiz1/t/solutions/Thomas Leitner/solitaire.rb,
lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb

Overview

Implements the Solitaire Cipher algorithm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = -1,, method = :fisher_yates) ⇒ SolitaireCipher

Initialize the deck



94
95
96
# File 'lib/quiz1/t/solutions/Thomas Leitner/solitaire.rb', line 94

def initialize( default_algorithm )
  @algorithm = default_algorithm
end

Instance Attribute Details

#algorithms=(value) ⇒ Object (writeonly)

Sets the attribute algorithms

Parameters:

  • value

    the value to set the attribute algorithms to.



102
103
104
# File 'lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb', line 102

def algorithms=(value)
  @algorithms = value
end

#stream=(value) ⇒ Object (writeonly)

Sets the attribute stream

Parameters:

  • value

    the value to set the attribute stream to.



103
104
105
# File 'lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb', line 103

def stream=(value)
  @stream = value
end

Instance Method Details

#decode(msg) ⇒ Object

Decodes the given msg using the internal deck



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/quiz1/t/solutions/Thomas Leitner/solitaire.rb', line 100

def decode( msg )
  msgNumbers = to_numbers( msg )
  cipherNumbers = to_numbers( generate_keystream( msg.length ) )

  resultNumbers = []
  msgNumbers.each_with_index do |item, index|
    item += 26 if item <= cipherNumbers[index]
    temp = item - cipherNumbers[index]
    resultNumbers << temp
  end

  return to_chars( resultNumbers )
end

#decrypt(message) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb', line 131

def decrypt( message )
  raise "bad decrypt message: #{message.inspect}" if message =~ /[^A-Z ]/

  reset
  chars = message.split(//).reject { |c| c == " " }.map { |c| c[0] - 64 }
  key = generate_key( chars.length )
  chars.zip( key ).map { |c,k| ( k >= c ? c + 26 - k : c - k ) }.map { |c| (c+64).chr }.join
end

#encode(msg) ⇒ Object

Encodes the given msg using the internal deck



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/quiz1/t/solutions/Thomas Leitner/solitaire.rb', line 115

def encode( msg )
  msg = msg.gsub(/[^A-Za-z]/, '').upcase
  msg += "X"*(5 - (msg.length % 5)) unless msg.length % 5 == 0

  msgNumbers = to_numbers( msg )
  cipherNumbers = to_numbers( generate_keystream( msg.length ) )

  resultNumbers = []
  msgNumbers.each_with_index do |item, index|
    temp = item + cipherNumbers[index]
    temp = temp - 26 if temp > 26
    resultNumbers << temp
  end

  return to_chars( resultNumbers )
end

#encrypt(message) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb', line 113

def encrypt( message )
  reset

  chars = message.split(//).map { |c| c.upcase }. reject { |c| c !~ /[A-Z]/ }
  chars.concat ["X"] * ( 5 - chars.length % 5 ) if chars.length % 5 > 0
  chars.map! { |c| c[0] - 64 }
  key = generate_key( chars.length )
  code = chars.zip( key ).map { |c,k| ( c + k > 26 ? c + k - 26 : c + k ) }.map { |c| (c+64).chr }

  msg = ""
  (code.length/5).times do
    msg << " " if msg.length > 0
    5.times { msg << code.shift }
  end

  return msg
end

#use_algorithm(keying_algorithm) ⇒ Object



109
110
111
# File 'lib/quiz1/t/solutions/Jamis Buck/lib/cipher.rb', line 109

def use_algorithm( keying_algorithm )
  @algorithm = @algorithms.get( keying_algorithm )
end