class CryptoToolchain::Tools::EcbPrependChosenPlaintextAttack
Constants
- PAD
Attributes
oracle[R]
Public Class Methods
new(oracle: CryptoToolchain::BlackBoxes::EcbPrependChosenPlaintextOracle.new)
click to toggle source
oracle must return an encrypted string via encrypt
# File lib/crypto_toolchain/tools/ecb_prepend_chosen_plaintext_attack.rb, line 9 def initialize(oracle: CryptoToolchain::BlackBoxes::EcbPrependChosenPlaintextOracle.new) @oracle = oracle unless oracle.encrypt(PAD * blocksize * 10).is_ecb_encrypted?(@blocksize) raise ArgumentError.new("Oracle does not appear to encrypt with ECB") end end
Public Instance Methods
execute()
click to toggle source
# File lib/crypto_toolchain/tools/ecb_prepend_chosen_plaintext_attack.rb, line 16 def execute (0..Float::INFINITY).each_with_object("") do |block_index, solved| from_block = (0...blocksize).each_with_object("") do |i, solved_in_block| padding_length = blocksize - (solved_in_block.bytes.length) - 1 padding = PAD * padding_length target = oracle.encrypt(padding).in_blocks(blocksize)[block_index] dict = (0..255).map(&:chr).each_with_object({}) do |chr, memo| guess = padding + solved + solved_in_block + chr output = oracle.encrypt(guess).in_blocks(blocksize)[block_index] memo[output] = chr break(memo) if output == target end if !dict.has_key?(target) return "#{solved}#{solved_in_block}" end solved_in_block << dict.fetch(target) end solved << from_block end end