Posted By

MMDeveloper on 09/16/08


Tagged

php crypt decrypt encrypt xor


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

Scooter
mothore
Nix
Unreal
FedericoBiccheddu
breign


xor encrypt/decrypt


 / Published in: PHP
 

  1. <?php
  2. class xorCrypt {
  3. private $password = NULL;
  4.  
  5. public function set_key($password) {
  6. $this->password = $password;
  7. }
  8.  
  9. private function get_rnd_iv($iv_len) {
  10. $iv = '';
  11. while ($iv_len-- > 0) {
  12. $iv .= chr(mt_rand() & 0xff);
  13. }
  14. return $iv;
  15. }
  16.  
  17. public function encrypt($plain_text, $iv_len = 16) {
  18. $plain_text .= "\x13";
  19. $n = strlen($plain_text);
  20. if ($n % 16) {
  21. $plain_text .= str_repeat("\0", 16 - ($n % 16));
  22. $i = 0;
  23. $enc_text = $this->get_rnd_iv($iv_len);
  24. $iv = substr($this->password ^ $enc_text, 0, 512);
  25. while ($i < $n) {
  26. $block = substr($plain_text, $i, 16) ^ pack('H*', sha1($iv));
  27. $enc_text .= $block;
  28. $iv = substr($block . $iv, 0, 512) ^ $this->password;
  29. $i += 16;
  30. }
  31. return base64_encode($enc_text);
  32. } else {}
  33. }
  34.  
  35. public function decrypt($enc_text, $iv_len = 16) {
  36. $enc_text = base64_decode($enc_text);
  37. $n = strlen($enc_text);
  38. $i = $iv_len;
  39. $plain_text = '';
  40. $iv = substr($this->password ^ substr($enc_text, 0, $iv_len), 0, 512);
  41. while ($i < $n) {
  42. $block = substr($enc_text, $i, 16);
  43. $plain_text .= $block ^ pack('H*', sha1($iv));
  44. $iv = substr($block . $iv, 0, 512) ^ $this->password;
  45. $i += 16;
  46. }
  47. return stripslashes(preg_replace('/\\x13\\x00*$/', '', $plain_text));
  48. }
  49. }
  50.  
  51. $xorCrypt = new xorCrypt();
  52. $xorCrypt->set_key("Your Secret Key");
  53.  
  54. $text = "hello world!";
  55. $encrypted = $xorCrypt->encrypt($text);
  56.  
  57. echo $encrypted;
  58. echo "<br />";
  59. echo $xorCrypt->decrypt($encrypted);
  60. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Unreal on June 17, 2009

Awesome, thanks!

You need to login to post a comment.