Class: Analyzers::VigenereXor

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

Defined Under Namespace

Classes: EightBitPatternFinder, HammingDistanceKeyLengthFinder, StaticKeylength

Instance Method Summary collapse

Methods included from Utils::Reporting::Console

#jot, #print_delimiter_line, #print_nice, #print_raw

Instance Method Details

#analyse_single(buf, key_length) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/crypto-toolbox/analyzers/vigenere_xor.rb', line 99

def analyse_single(buf,key_length)
  candidate_map = Analyzers::Utils::KeyCandidateMap.create(buf,key_length)

 
  candidate_amount = candidate_map.map{|k,v| v.length}.reduce(&:*)
  if candidate_amount.zero?
    jot("no combinations for keylen #{key_length} (at least one byte has no candidates)",debug: true)
    return []
  end
  jot "Amount of candidate keys: #{candidate_map.map{|k,v| v.length}.reduce(&:*)}. Starting Permutation (RAM intensive)",debug: true

  
  # split the candidate map into head and*tail to create the prduct of all combinations
  head,*tail = candidate_map.map{|k,v|v}
  begin
    combinations = head.product(*tail) 
    # we simply skip too big products
  rescue RangeError => ex
    jot "keylen: #{key_length}: #{ex}"
    return []
  end


  
  if ENV["DEBUG_ANALYSIS"]
    ensure_consistent_result!(combinations,candidate_map)
    print_candidate_decryptions(candidate_map,key_length,buf)
  end

  
  keys = Analyzers::Utils::KeyFilter::AsciiPlain.new(combinations,buf).filter.reject(&:empty?)

  # return the result, not the key
  keys.map do|key|
    key.xor(buf)
  end
end

#analyze(input, keylength_strategy = EightBitPatternFinder.new) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/crypto-toolbox/analyzers/vigenere_xor.rb', line 89

def analyze(input, keylength_strategy=EightBitPatternFinder.new)
  buf = CryptBuffer.from_hex(input)

  keylength_strategy.keylen_for(buf).map do |keylen|
    analyse_single(buf,keylen)
  end.flatten
end