Module: Crypto
- Included in:
- Cipher::ChaoCipher, Cipher::DisruptedTransposition, Cipher::Wheatstone, Key::Playfair, Key::SCKey, Key::TKey, Key::VICKey, Key::Wheatstone
- Defined in:
- lib/crypto_helper.rb
Overview
– String
Defined Under Namespace
Classes: HoleArea
Constant Summary
- BASE =
keyshuffle
Form an alphabet formed with a keyword, re-shuffle everything to make it less predictable (i.e. checkerboard effect)
Shuffle the alphabet a bit to avoid sequential allocation of the code numbers. This is actually performing a transposition with the word itself as key.
Regular rectangle
Key is ARABESQUE condensed into ARBESQU (len = 7) (height = 4) Let word be ARBESQUCDFGHIJKLMNOPTVWXYZ/-
First passes will generate
A RBESQUCDFGHIJKLMNOPTVWXYZ/- c=0 0 x 6 AC RBESQUDFGHIJKLMNOPTVWXYZ/- c=6 1 x 6 ACK RBESQUDFGHIJLMNOPTVWXYZ/- c=12 2 x 6 ACKV RBESQUDFGHIJLMNOPTWXYZ/- c=18 3 x 6 ACKVR BESQUDFGHIJLMNOPTWXYZ/- c=0 0 x 5 ACKVRD BESQUFGHIJLMNOPTWXYZ/- c=5 1 x 5 … ACKVRDLWBFMXEGNYSHOZQIP/UJT-
Irregular rectangle
Key is SUBWAY condensed info SUBWAY (len = 6) (height = 5)
S UBWAYCDEFGHIJKLMNOPQRTVXZ/- c=0 0 x 5 SC UBWAYDEFGHIJKLMNOPQRTVXZ/- c=5 1 x 5 SCI UBWAYDEFGHJKLMNOPQRTVXZ/- c=10 2 x 5 SCIO UBWAYDEFGHJKLMNPQRTVXZ/- c=15 3 x 5 SCIOX UBWAYDEFGHJKLMNPQRTVZ/- c=20 4 x 5 SCIOXU BWAYDEFGHJKLMNPQRTVZ/- c=0 0 x 4 … SCIOXUDJPZBEKQ/WFLR-AG YHMNTV c=1 1 x 1 SCIOXUDJPZBEKQ/WFLR-AGM YHNTV c=2 2 x 1 SCIOXUDJPZBEKQ/WFLR-AGMT YHNV c=3 3 x 1 SCIOXUDJPZBEKQ/WFLR-AGMTYHNV
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Instance Method Summary (collapse)
-
- (Object) addmod10(a, b)
addmod10.
-
- (Object) chainadd(a)
chainadd.
-
- (Object) expand5to10(a)
expand5to10.
-
- (Object) find_hole(kw, ph = 'AT ONE SIR')
find_hole.
- - (Object) keyshuffle(key, base = BASE)
-
- (Object) normalize(a)
normalize.
-
- (Object) p1_encode(p1, p2)
p1_encode.
-
- (Object) str_to_numeric(str)
str_to_numeric.
-
- (Object) submod10(a, b)
submod10.
Instance Method Details
- (Object) addmod10(a, b)
addmod10
Addition modulo 10
244 245 246 247 |
# File 'lib/crypto_helper.rb', line 244 def addmod10(a, b) raise DataError if a.length != b.length (0..a.length-1).collect {|i| (a[i] + b[i]) % 10 } end |
- (Object) chainadd(a)
chainadd
- a0, a1, a2, a3, a4
-
is transformed into
- b0, b1, b2, b3, b4
-
b0 = a0 + a1 b1 = a1 + a2 b2 = a2 + a3 b3 = a3 + a4 b4 = a4 + b0
224 225 226 227 228 229 |
# File 'lib/crypto_helper.rb', line 224 def chainadd(a) b = a.dup len = a.length a.each_with_index{|e,i| b[i] = (e + b[(i+1) % len]) % 10 } b end |
- (Object) expand5to10(a)
expand5to10
Use chainadd to generate the expanded key
235 236 237 238 |
# File 'lib/crypto_helper.rb', line 235 def (a) expd = chainadd(a) (a + expd) end |
- (Object) find_hole(kw, ph = 'AT ONE SIR')
find_hole
Given a keyword with spaces, output an array with their positions
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/crypto_helper.rb', line 333 def find_hole(kw, ph = 'AT ONE SIR') if kw.class == String then kwn = kw[0..(ph.length - 1)].to_numeric10 elsif kw.class == Array kwn = kw.dup else raise DataError, 'Must be either String or Array of integers' end long = Array.new short = Array.new i = 0 ph.scan(/./).each do |c| if c == ' ' then long << kwn[i] else short << kwn[i] end i += 1 end long.compact end |
- (Object) keyshuffle(key, base = BASE)
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/crypto_helper.rb', line 301 def keyshuffle(key, base = BASE) word = (key + base).condensed.dup len = key.condensed.length height = base.length / len # Odd rectangle # if (base.length % len) != 0 height = height + 1 end res = '' (len - 1).downto(0) do |i| 0.upto(height - 1) do |j| if word.length <= (height - 1) then return res + word else c = word.slice!(i * j) if not c.nil? then res = res + c.chr end end end end res end |
- (Object) normalize(a)
normalize
187 188 189 |
# File 'lib/crypto_helper.rb', line 187 def normalize(a) a.collect!{|e| (e + 1) % 10 } end |
- (Object) p1_encode(p1, p2)
p1_encode
This encoding method uses the array p2 to encode array p1 in a simplified tabular substitution
204 205 206 207 208 209 210 |
# File 'lib/crypto_helper.rb', line 204 def p1_encode(p1, p2) r = Array.new p1.each do |e| r << p2[(e + 10) % 10 - 1] end r end |
- (Object) str_to_numeric(str)
str_to_numeric
Returns an array of each digit
195 196 197 |
# File 'lib/crypto_helper.rb', line 195 def str_to_numeric(str) str.split('').collect{|i| i.to_i } end |
- (Object) submod10(a, b)
submod10
Substraction modulo 10 (step 1)
253 254 255 256 |
# File 'lib/crypto_helper.rb', line 253 def submod10(a, b) len = a.length (0..len-1).collect{|i| (a[i] - b[i] + 10) % 10 } end |