Class: CryptoToolchain::Tools::InteractiveXor

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/tools/interactive_xor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ciphertexts) ⇒ InteractiveXor

Returns a new instance of InteractiveXor.



13
14
15
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 13

def initialize(ciphertexts)
  @ciphertexts = ciphertexts
end

Instance Attribute Details

#ciphertextsObject (readonly)

Returns the value of attribute ciphertexts.



12
13
14
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 12

def ciphertexts
  @ciphertexts
end

Instance Method Details

#attempt(index, *plains) ⇒ Object

index is the index of the ciphertext against which you are making an attempt Plains are the plaintexts you want to try as possibilities



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 37

def attempt(index, *plains)
  validate_length!(plains)
  keys = keys_for(plains, index)
  ciphertexts.each_with_index do |ct, i|
    line = keys.map do |k|
      _len = [ct.bytesize, k.bytesize].min
      ct[0..._len] ^ k[0..._len]
    end.join("     |     ")
    puts "#{i}\t#{line}"
  end
  nil
end

#executeObject



17
18
19
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 17

def execute
  binding.pry
end

#keys_for(plains, index) ⇒ Object



28
29
30
31
32
33
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 28

def keys_for(plains, index)
  plains.map.with_index do |pl|
    len = [pl.length, ciphertexts[index].length].min
    ciphertexts[index][0...len] ^ pl[0...len]
  end
end

#validate_length!(plains) ⇒ Object



21
22
23
24
25
26
# File 'lib/crypto_toolchain/tools/interactive_xor.rb', line 21

def validate_length!(plains)
  len = plains.first.length
  plains.each do |pl|
    raise ArgumentError.new("must have same length") unless pl.length == len
  end
end