Class: CryptoChallanges::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto-toolbox/crypto_challanges/solver.rb

Instance Method Summary collapse

Instance Method Details

#letter_count(str) ⇒ Object



12
13
14
15
16
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 12

def letter_count(str)
  str.downcase.each_char.with_object({}) do |c,h|
    h[c] = (h.fetch(c,0) + 1) if c =~ /[A-Za-z ]/
  end
end

#letter_freq(str) ⇒ Object



18
19
20
21
22
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 18

def letter_freq(str)
  counts   = letter_count(str)
  quotient = counts.values.reduce(&:+).to_f
  counts.sort_by{|k,v| v}.reverse.to_h.each_with_object({}){|(k,v),hsh| hsh[k] = (v/quotient) }
end

#solve1(input) ⇒ Object



4
5
6
7
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 4

def solve1(input)
  #CryptoChallanges::Set1::Challange1::Solver.run(input)
  CryptBuffer.from_hex(input).base64
end

#solve2(c1, c2) ⇒ Object



8
9
10
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 8

def solve2(c1,c2)
  (CryptBuffer.from_hex(c1) ^ CryptBuffer.from_hex(c2)).hex.downcase
end

#solve3(input) ⇒ Object



24
25
26
27
28
29
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 24

def solve3(input)
  candidates = (1..256).map{ |guess| CryptBuffer.from_hex(input).xor_all_with(guess) }
  detector = Analyzers::Utils::HumanLanguageDetector.new
  
  detector.human_language_entries(candidates).first.to_s
end

#solve4(hexstrings) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 31

def solve4(hexstrings)
  detector = Analyzers::Utils::HumanLanguageDetector.new
  result = hexstrings.map{|h| CryptBuffer.from_hex(h)}.map.with_index do |c,i|
    candidates = (1..256).map{ |guess| c.xor_all_with(guess) }
    matches = detector.human_language_entries(candidates)

    matches.empty? ? nil : matches
  end
  result.flatten.compact.map(&:str).first
end

#solve5(input, key) ⇒ Object



42
43
44
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 42

def solve5(input,key)
  CryptBuffer(input).xor(key,expand_input: true).hex
end

#solve6(input) ⇒ Object



46
47
48
49
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 46

def solve6(input)
  buffer = CryptBuffer.from_base64(input)
  Analyzers::VigenereXor.new.analyze(buffer.hex,Analyzers::VigenereXor::HammingDistanceKeyLengthFinder.new)
end

#solve7(input, key) ⇒ Object



51
52
53
54
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 51

def solve7(input,key)
  data = CryptBuffer.from_base64(input).str
  Ciphers::Aes.new(128,:ECB).decipher(data,key)
end

#solve8(ciphers) ⇒ Object



56
57
58
# File 'lib/crypto-toolbox/crypto_challanges/solver.rb', line 56

def solve8(ciphers)
  binding.pry
end