class Analyzers::VigenereXor::EightBitPatternFinder

Public Instance Methods

keylen_for(buf) click to toggle source
# File lib/crypto-toolbox/analyzers/vigenere_xor.rb, line 39
def keylen_for(buf)
  # Example: "100100" || nil
  key_pattern = find_pattern(buf)
  
  assert_key_pattern!(key_pattern)
  
  report_pattern_info(key_pattern)

  [key_pattern.length]
end

Private Instance Methods

assert_key_pattern!(key_pattern) click to toggle source
# File lib/crypto-toolbox/analyzers/vigenere_xor.rb, line 52
def assert_key_pattern!(key_pattern)
  if key_pattern.nil?
    $stderr.puts "failed to find keylength by ASCII-8-Bit anlysis"
    exit(1)
  end
end
find_pattern(buf) click to toggle source
# File lib/crypto-toolbox/analyzers/vigenere_xor.rb, line 64
def find_pattern(buf)
  bitstring = buf.nth_bits(7).join("")
  
  1.upto(buf.bytes.length).map do |ksize|
    parts = bitstring.scan(/.{#{ksize}}/)
    if parts.uniq.length == 1
      parts.first
    else
      nil
    end
  end.compact.first
end
report_pattern_info(key_pattern) click to toggle source
# File lib/crypto-toolbox/analyzers/vigenere_xor.rb, line 59
def report_pattern_info(key_pattern)
  jot "Found recurring key pattern: #{key_pattern}"
  jot "Detected key length: #{key_pattern.length}"
end