Class: Analyzers::Utils::KeyFilter::AsciiPlain

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto-toolbox/analyzers/utils/key_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(keys, ciphertext) ⇒ AsciiPlain

Returns a new instance of AsciiPlain.



8
9
10
11
12
13
14
# File 'lib/crypto-toolbox/analyzers/utils/key_filter.rb', line 8

def initialize(keys,ciphertext)
  @keys = keys
  @c = @ciphertext = ciphertext
  @keylen = keys.first.length
  @detector = Analyzers::Utils::HumanLanguageDetector.new
  @spell_checker = Analyzers::Utils::SpellChecker.new("en_US")
end

Instance Method Details

#filterObject



16
17
18
19
20
21
22
23
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_filter.rb', line 16

def filter
  # how often is the key repeated 
  reps = @c.bytes.length / @keylen
  result =[]

  

  
  # should we fork here ?
  @keys.each_with_index do |key,i| #  i is used as a simple counter only !
    test = CryptBuffer.new(@c.bytes[0,@keylen]).xor(key).str
    repkey = CryptBuffer.new((key*reps) + key[0,(@c.bytes.length % reps).to_i])
    str  = @c.xor(repkey).to_s
    
    # NOTE: we dont need the ASCII check provided by the human language detector
    # since the key selection is usually based on ascii value checks
    if @spell_checker.human_language?(str)
      result << repkey
      break
    else
      if (i % 50000).zero?
        puts "[Progress] #{i}/#{@keys.length} (#{(i.to_f/@keys.length*100).round(4)}%)"
      end
    end
  end
  return result
end