SHARE
TWEET

Untitled

a guest Oct 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. require_once('Aes128CBCEncryptor.php');
  3. echo PHP_EOL.PHP_EOL.PHP_EOL;
  4. echo 'Padding oracle attack' . PHP_EOL .PHP_EOL;
  5. //echo "Tekst: ";
  6. //$text = fgets(STDIN);
  7.  
  8. $text = "Kryptologia to najfajniejszy kurs w roku 2019/2020 119WI2";
  9. echo "Tekst: " . PHP_EOL;
  10. echo $text . PHP_EOL .PHP_EOL;
  11.  
  12. $cipheredText = Aes128CBCEncryptor::encrypt($text);
  13. echo "Zaszyfrowany tekst: " . PHP_EOL;
  14. foreach (str_split($cipheredText, Aes128CBCEncryptor::BLOCK_SIZE) as $block) {
  15.     echo base64_encode($block) . PHP_EOL;
  16. }
  17.  
  18. echo PHP_EOL. '--------' . PHP_EOL .PHP_EOL;
  19. $blocks = str_split($cipheredText, Aes128CBCEncryptor::BLOCK_SIZE);
  20. $blockCount = count($blocks);
  21. $plainText = '';
  22.  
  23. for ($i = 0; $i < $blockCount; $i++) {
  24.     if ($blockCount - ($i+2) >= 0) {
  25.         $C = 'aaaaaaaaaaaaaaa';
  26.         $lastXORs = [];
  27.         for ($k = 1; $k <= Aes128CBCEncryptor::BLOCK_SIZE; $k++) {
  28.             for ($m = 1; $m < $k; $m++) {
  29.                 $C[Aes128CBCEncryptor::BLOCK_SIZE - $m] = chr($k ^ $lastXORs[$m-1]);
  30.             }
  31.             for ($j = 0; $j < 256; $j++) {
  32.                 $C[Aes128CBCEncryptor::BLOCK_SIZE - $k] = chr($j);
  33.                 if (Aes128CBCEncryptor::paddingOracleCheck($C . $blocks[$blockCount - ($i+1)])) {
  34.                     $paddingXor = ($k ^ $j);
  35.                     $lastXORs[] = $paddingXor;
  36.                     $decipheredLetter = (ord($blocks[$blockCount - ($i+2)][Aes128CBCEncryptor::BLOCK_SIZE - $k]) ^ $paddingXor);
  37.                     $plainText = chr($decipheredLetter) . $plainText;
  38.                     continue;
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     $plainText = PHP_EOL . $plainText;
  44. }
  45. echo 'Odszyfrowane bloki: ';
  46. echo $plainText;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top