PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/cryptoxi.php

https://github.com/aponxi/php-cryptoxi
PHP | 57 lines | 40 code | 8 blank | 9 comment | 2 complexity | 84f376dfb131d36323a15ef2ed29c952 MD5 | raw file
  1. <?PHP
  2. class cryptoxi{
  3. var $publickey = ""; //$_POST['key'];
  4. //var $encrypt = ""; // $_POST['tocrypt'];
  5. //var $decrypt = "";
  6. var $privatekey = "2c3fec85b5f6b0a1cd5a1010c67eb772";
  7. function cryptoxi($publickey, $privatekey){
  8. $this->publickey = $publickey;
  9. $this->privatekey = $privatekey;
  10. }
  11. //$encrypted = mysql_aes_encrypt ($crypt,$key);
  12. private function hexit ($str) {
  13. return bin2hex($str);
  14. }
  15. private function binit ($str){
  16. return pack("H*",$str);
  17. }
  18. /*
  19. Hex to bin - bin to hex conversion example
  20. ===========================================
  21. */
  22. #echo hexit('brown fox'); echo '</br> binit: '; echo binit('62726f776e20666f78'); echo '</br>';
  23. #==========================================
  24. function encryptxi($val)
  25. {
  26. $ky = $this->publickey;
  27. $key= $this->privatekey;
  28. for($a=0;$a<strlen($ky);$a++)
  29. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  30. $mode=MCRYPT_MODE_ECB;
  31. $enc=MCRYPT_RIJNDAEL_128;
  32. $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
  33. $result = mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
  34. return $this->hexit($result);
  35. }
  36. function decryptxi($val)
  37. {
  38. $ky = $this->publickey;
  39. $value = $this->binit($val);
  40. $key=$this->privatekey;
  41. for($a=0;$a<strlen($ky);$a++)
  42. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  43. $mode = MCRYPT_MODE_ECB;
  44. $enc = MCRYPT_RIJNDAEL_128;
  45. $dec = @mcrypt_decrypt($enc, $key, $value, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
  46. return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
  47. }
  48. }
  49. ?>