PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/crypt.php

https://github.com/aponxi/php-cryptoxi
PHP | 55 lines | 38 code | 11 blank | 6 comment | 4 complexity | e31ba220f9920cda1f727ed0d059d15c MD5 | raw file
  1. <?php
  2. function hexit ($str) {
  3. return bin2hex($str);
  4. }
  5. function binit ($str){
  6. return pack("H*",$str);
  7. }
  8. /*
  9. Hex to bin - bin to hex conversion example
  10. ===========================================
  11. */
  12. #echo hexit('brown fox'); echo '</br> binit: '; echo binit('62726f776e20666f78'); echo '</br>';
  13. #==========================================
  14. function mysql_aes_encrypt($val,$ky)
  15. {
  16. $key="2c3fec85b5f6b0a1cd5a1010c67eb772";
  17. for($a=0;$a<strlen($ky);$a++)
  18. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  19. $mode=MCRYPT_MODE_ECB;
  20. $enc=MCRYPT_RIJNDAEL_128;
  21. $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
  22. $result = mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
  23. return hexit($result);
  24. }
  25. function mysql_aes_decrypt($val,$ky)
  26. {
  27. $value = binit($val);
  28. $key="2c3fec85b5f6b0a1cd5a1010c67eb772";
  29. for($a=0;$a<strlen($ky);$a++)
  30. $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
  31. $mode = MCRYPT_MODE_ECB;
  32. $enc = MCRYPT_RIJNDAEL_128;
  33. $dec = @mcrypt_decrypt($enc, $key, $value, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
  34. 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));
  35. }
  36. $key = $_POST['key'];
  37. $crypt = $_POST['tocrypt'];
  38. $encrypted = mysql_aes_encrypt ($crypt,$key);
  39. echo $encrypted;
  40. if($_POST['todecrypt']!==''){
  41. $decrypt = $_POST['todecrypt']; $decrypted = 'x-.-x'.mysql_aes_decrypt($decrypt,$key);
  42. echo $decrypted; }
  43. else {echo 'x-.-xDecrypted text will appear here.';}
  44. ?>