function decryptAES128ECB($data, $key, $strictPadding = false) { $blocks = str_split($data, 16); foreach ($blocks as &$block) { $block = _decryptAES128ECB($block, $key); } $plaintext = implode($blocks); try { return removePKCS7Padding($plaintext); } catch (Exception $e) { if ($strictPadding) { throw $e; } return $plaintext; } }
$currentPadding = 16 - $attackPosition; $realBlock[$attackPosition] = $currentPadding ^ $trialByte ^ ord($iv[$attackPosition]); if ($attackPosition === 0) { break 2; } for ($j = 15; $j >= $attackPosition; $j--) { $fauxBlock[$j] = $currentPadding + 1 ^ $realBlock[$j] ^ ord($iv[$j]); } break; } } } return implode(array_map('chr', $realBlock)); } // don't output if we're included into another script. if (!debug_backtrace()) { print "Cracking 10 randomly selected ciphertexts:\n"; for ($j = 0; $j < 10; $j++) { $key = getRandomBytes(16); $iv = getRandomBytes(16); $ciphertext = getRandomCiphertext($key, $iv); $blocks = str_split($ciphertext, 16); array_unshift($blocks, $iv); $blockNum = count($blocks) - 1; for ($i = $blockNum; $i > 0; $i--) { $blocks[$i] = crackBlock($blocks[$i], $key, $blocks[$i - 1]); } array_shift($blocks); print "{$j}: " . removePKCS7Padding(implode($blocks)) . "\n"; } }