class CryptoToolchain::Tools::InteractiveXor
Attributes
ciphertexts[R]
Public Class Methods
new(ciphertexts)
click to toggle source
# File lib/crypto_toolchain/tools/interactive_xor.rb, line 13 def initialize(ciphertexts) @ciphertexts = ciphertexts end
Public Instance Methods
attempt(index, *plains)
click to toggle source
index is the index of the ciphertext against which you are making an attempt Plains are the plaintexts you want to try as possibilities
# 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
execute()
click to toggle source
# File lib/crypto_toolchain/tools/interactive_xor.rb, line 17 def execute binding.pry end
keys_for(plains, index)
click to toggle source
# 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)
click to toggle source
# 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