Advertisement
Guest User

Decryption XoR PHP

a guest
Nov 21st, 2011
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. <?php
  2. function hexToStr($hex)
  3. {
  4.     $string='';
  5.     for ($i=0; $i < strlen($hex)-1; $i+=2)
  6.     {
  7.         $string .= chr(hexdec($hex[$i].$hex[$i+1]));
  8.     }
  9.     return $string;
  10. }
  11.  
  12. function convert($text, $key = '') {
  13.     // return text unaltered if the key is blank
  14.     if ($key == '') {
  15.         return $text;
  16.     }
  17.  
  18.     // remove the spaces in the key
  19.     $key = str_replace(' ', '', $key);
  20.     if (strlen($key) < 8) {
  21.         exit('key error');
  22.     }
  23.     // set key length to be no more than 32 characters
  24.     $key_len = strlen($key);
  25.     if ($key_len > 32) {
  26.         $key_len = 32;
  27.     }
  28.  
  29.     // A wee bit of tidying in case the key was too long
  30.     $key = substr($key, 0, $key_len);
  31.  
  32.     // We use this a couple of times or so
  33.     $text_len = strlen($text);
  34.  
  35.     // fill key with the bitwise AND of the ith key character and 0x7C, padded to length of text.
  36.     $lomask = str_repeat("\x7C", $text_len); // Probably better than str_pad
  37.     $himask = str_repeat("\x00", $text_len);
  38.     $k = str_pad("", $text_len, $key); // this one _does_ need to be str_pad
  39.  
  40.     // {en|de}cryption algorithm
  41.     $text = (($text ^ $k) & $lomask) | ($text & $himask);
  42.  
  43.     return $text;
  44. }
  45.  
  46. echo convert(hexToStr('5E5C535C584B40584A4A4E564B5D4503510755020402080C580A015D0D0A5A010206070C0E025C000F005D080E5F5D'), '1234567899999999');
  47.  
  48. echo "\n";
  49.  
  50. ?>
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement