Advertisement
zamcsaba

ITSec HW 1

Mar 7th, 2021
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2.  
  3. // Basic settings
  4. $filename_v1_1 = "LabProfile-v1.1.crypt";
  5. $filename_v1 = "LabProfile-v1.crypt";
  6. $blockSize = 16;
  7.  
  8. // reading input files as hex strings
  9. $content_v1 = read_file_as_hex($filename_v1);
  10. $content_v1_1 = read_file_as_hex($filename_v1_1);
  11.  
  12. $result = "";
  13. // We know the real 9th block and we can start from there
  14. $previousR2 = "20202020202020202020202020202020";
  15. for ($i = 9; $i < (strlen($content_v1) / $blockSize / 2); $i++) {
  16.     // first xor the same blocks
  17.     $r1 = xor_blocks($content_v1, $i, $content_v1_1, $i, $blockSize);
  18.     // then xor the result with the previous message
  19.     $r2 = xor_strings($previousR2, $r1);
  20.     // then convert it to readable string
  21.     $result .= hex_to_string($r2);
  22.     $previousR2 = $r2;
  23. }
  24. echo $result;
  25.  
  26. // XOR two blocks from the encrypted bytes
  27. function xor_blocks(string $hex1, int $blockNum1, string $hex2, int $blockNum2, int $blockSize): string
  28. {
  29.     // Split the string into octets
  30.     $chunk1 = get_block($hex1, $blockNum1, $blockSize);
  31.     $chunk2 = get_block($hex2, $blockNum2, $blockSize);
  32.     return xor_strings($chunk1, $chunk2);
  33. }
  34.  
  35. // get a block by block number and block size
  36. function get_block(string $hex, int $blockNum, int $blockSize): string
  37. {
  38.     return substr($hex, (2 * ($blockNum - 1) * $blockSize), $blockSize * 2);
  39. }
  40.  
  41. // xor two hex strings and returns the result as hex string
  42. function xor_strings(string $hex1, string $hex2): string
  43. {
  44.     $octets_1 = str_split($hex1, 2);
  45.     $octets_2 = str_split($hex2, 2);
  46.  
  47.     $result = '';
  48.     for ($i = 0; $i < count($octets_1); $i++) {
  49.         // Calculate XOR (converting to decimal numbers first then back to hex string)
  50.         $res = strtoupper(dechex(hexdec($octets_1[$i]) ^ hexdec($octets_2[$i])));
  51.         // Pad with 0 if necessary
  52.         $result .= strlen($res) == 1 ? ("0" . $res) : $res;
  53.     }
  54.     return $result;
  55. }
  56.  
  57. // read a file and convert it to hex strings
  58. function read_file_as_hex(string $filename): string
  59. {
  60.     $handle = fopen($filename, "r");
  61.     $content = strtoupper(unpack("H*", fread($handle, filesize($filename)))[1]);
  62.     fclose($handle);
  63.     return $content;
  64. }
  65.  
  66. // convert a hex string to ascii characters
  67. function hex_to_string(string $hex): string
  68. {
  69.     $string = '';
  70.     $octets = str_split($hex, 2);
  71.     for ($i = 0; $i < count($octets); $i++) {
  72.         $string .= chr(hexdec($octets[$i]));
  73.     }
  74.     return $string;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement