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, #print_nice, #print_raw

Constructor Details

#initializeKeyCandidateMap

Returns a new instance of KeyCandidateMap.



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

def initialize
  @lang_detector = Analyzers::Utils::AsciiLanguageDetector.new
end

Class Method Details

.create(input_buf, keylen) ⇒ Object

factory method for easy use



18
19
20
# File 'lib/crypto-toolbox/analyzers/utils/key_candidate_map.rb', line 18

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crypto-toolbox/analyzers/utils/key_candidate_map.rb', line 27

def run(input_buf,keylen)
  candidate_map = (0..(keylen-1)).each_with_object({}) do |key_byte_pos,hsh|
=begin
# Letter frquency testing
      freqs = letter_freq(nth_byte_stream.xor_all_with(guess).str)
      diff = FREQUENCIES.keys - freqs.keys
      binding.pry  if     nth_byte_stream.xor_all_with(guess).bytes.all?{|byte| acceptable_char?(byte) } &&
                          ((diff.map{|c| FREQUENCIES[c]}.reduce(&:+)||0) > 16)
=end


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