Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Basic settings
- $filename_v1_1 = "LabProfile-v1.1.crypt";
- $filename_v1 = "LabProfile-v1.crypt";
- $blockSize = 16;
- // reading input files as hex strings
- $content_v1 = read_file_as_hex($filename_v1);
- $content_v1_1 = read_file_as_hex($filename_v1_1);
- $result = "";
- // We know the real 9th block and we can start from there
- $previousR2 = "20202020202020202020202020202020";
- for ($i = 9; $i < (strlen($content_v1) / $blockSize / 2); $i++) {
- // first xor the same blocks
- $r1 = xor_blocks($content_v1, $i, $content_v1_1, $i, $blockSize);
- // then xor the result with the previous message
- $r2 = xor_strings($previousR2, $r1);
- // then convert it to readable string
- $result .= hex_to_string($r2);
- $previousR2 = $r2;
- }
- echo $result;
- // XOR two blocks from the encrypted bytes
- function xor_blocks(string $hex1, int $blockNum1, string $hex2, int $blockNum2, int $blockSize): string
- {
- // Split the string into octets
- $chunk1 = get_block($hex1, $blockNum1, $blockSize);
- $chunk2 = get_block($hex2, $blockNum2, $blockSize);
- return xor_strings($chunk1, $chunk2);
- }
- // get a block by block number and block size
- function get_block(string $hex, int $blockNum, int $blockSize): string
- {
- return substr($hex, (2 * ($blockNum - 1) * $blockSize), $blockSize * 2);
- }
- // xor two hex strings and returns the result as hex string
- function xor_strings(string $hex1, string $hex2): string
- {
- $octets_1 = str_split($hex1, 2);
- $octets_2 = str_split($hex2, 2);
- $result = '';
- for ($i = 0; $i < count($octets_1); $i++) {
- // Calculate XOR (converting to decimal numbers first then back to hex string)
- $res = strtoupper(dechex(hexdec($octets_1[$i]) ^ hexdec($octets_2[$i])));
- // Pad with 0 if necessary
- $result .= strlen($res) == 1 ? ("0" . $res) : $res;
- }
- return $result;
- }
- // read a file and convert it to hex strings
- function read_file_as_hex(string $filename): string
- {
- $handle = fopen($filename, "r");
- $content = strtoupper(unpack("H*", fread($handle, filesize($filename)))[1]);
- fclose($handle);
- return $content;
- }
- // convert a hex string to ascii characters
- function hex_to_string(string $hex): string
- {
- $string = '';
- $octets = str_split($hex, 2);
- for ($i = 0; $i < count($octets); $i++) {
- $string .= chr(hexdec($octets[$i]));
- }
- return $string;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement