Class: Analyzers::Utils::KeyCandidateMap

Inherits:
Object
  • Object
show all
Includes:
Utils::Reporting::Console
Defined in:
lib/crypto-toolbox/analyzers/utils/key_candidate_map.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Reporting::Console

#jot, #print_delimiter_line

Class Method Details

.create(input_buf, keylen) ⇒ Object

factory method for easy use



15
16
17
# File 'lib/crypto-toolbox/analyzers/utils/key_candidate_map.rb', line 15

def self.create(input_buf,keylen)
  new.run(input_buf,keylen)
end

Instance Method Details

#run(input_buf, keylen) ⇒ Object

Algorithm 1) for each position of the key: (key_byte_pos) 2) create a stream of all the nth bytes of the keylen 3) xor any possible byte value (guess) with all nth’s bytes 4) select those guesses that decipher the nth-byte stream to only english plain ascii chars



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/crypto-toolbox/analyzers/utils/key_candidate_map.rb', line 24

def run(input_buf,keylen)
  candidate_map ={}
  (0..(keylen-1)).each do |key_byte_pos|
    
    # create an array of every nth byte of the input. ( thus a pseudo stream of the nth bytes )
    # 1) create an enumerator of the nth positions. e.g for iteration 0: [0,7,14,...]
    # 2) Next: Map the positions to bytes of the input buffer
    #
    # NOTE: regular implementation without cryptbuffer magic:
    # nth_stream = (key_byte_pos).step(input_buf.bytes.length() -1, keylen).map{|i| input_buf.bytes[i]}
    # nth_byte_stream2 = CryptBuffer.new(nth_stream)

    nth_byte_stream = input_buf.nth_bytes(keylen,offset: key_byte_pos)
    candidate_map[key_byte_pos] = 0.upto(255).select{|guess| nth_byte_stream.xor_all_with(guess).bytes.all?{|byte| acceptable_char?(byte) } }
    
    jot("found #{candidate_map[key_byte_pos].inspect} bytes for position: #{key_byte_pos}",debug: true)
  end
  candidate_map
end