fork download
  1. <?php
  2.  
  3. class MCrypt
  4. {
  5. private $iv = 'fedcba9876543210'; #Same as in JAVA
  6. private $key = '0123456789abcdef'; #Same as in JAVA
  7. private $code = 'hello';
  8.  
  9. function __construct()
  10. {
  11. }
  12.  
  13. function encrypt($str) {
  14.  
  15. //$key = $this->hex2bin($key);
  16. $iv = $this->iv;
  17.  
  18. $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
  19.  
  20. mcrypt_generic_init($td, $this->key, $iv);
  21. $encrypted = mcrypt_generic($td, $str);
  22.  
  23.  
  24. return bin2hex($encrypted);
  25. }
  26.  
  27. function decrypt($code) {
  28. //$key = $this->hex2bin($key);
  29. $code = $this->hex2bin($code);
  30. $iv = $this->iv;
  31.  
  32. $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
  33.  
  34. mcrypt_generic_init($td, $this->key, $iv);
  35. $decrypted = mdecrypt_generic($td, $code);
  36.  
  37.  
  38. return utf8_encode(trim($decrypted));
  39. }
  40.  
  41. protected function hex2bin($hexdata) {
  42. $bindata = '';
  43.  
  44. for ($i = 0; $i < strlen($hexdata); $i += 2) {
  45. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  46. }
  47.  
  48. return $bindata;
  49. }
  50.  
  51. }
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
Standard output is empty