EncryptUtil.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: bingone
  5. * Date: 16/1/19
  6. * Time: 下午6:18
  7. */
  8. class EncryptUtil
  9. {
  10. public static function encryptForYunpian($msg, $key)
  11. {
  12. $key = EncryptUtil::getTeaKey($key);
  13. $info=EncryptUtil::getBytes($msg);
  14. $retLen = count($info) + 8 - count($info) % 8;
  15. }
  16. public static function encrypt($content, $offset, $key, $times){
  17. $tempInt=EncryptUtil::bytesToInteger($content,$offset);
  18. $y=$tempInt[0];
  19. $z=$tempInt[1];
  20. $sum=0;
  21. $a=$key[0];$b=$key[1];$c=$key[2];$d=$key[3];
  22. $delta=0x9e3779b9;
  23. for ($i = 0; $i < $times; $i++) {
  24. $sum += $delta;
  25. $y += (($z<<4) + $a) ^ ($z + $sum) ^ (($z>>5) + $b);
  26. $z += (($y<<4) + $c) ^ ($y + $sum) ^ (($y>>5) + $d);
  27. }
  28. $tempInt[0]=$y;
  29. $tempInt[1]=$z;
  30. return EncryptUtil::integerToBytes($tempInt, 0);
  31. }
  32. public static function decrypt($bytes){
  33. }
  34. public static function bytesToInteger($bytes, $position) {
  35. $val = 0;
  36. $val = $bytes[$position + 3] & 0xff;
  37. $val <<= 8;
  38. $val |= $bytes[$position + 2] & 0xff;
  39. $val <<= 8;
  40. $val |= $bytes[$position + 1] & 0xff;
  41. $val <<= 8;
  42. $val |= $bytes[$position] & 0xff;
  43. return $val;
  44. }
  45. public static function integerToBytes($val) {
  46. $byt = array();
  47. $byt[0] = ($val & 0xff);
  48. $byt[1] = ($val >> 8 & 0xff);
  49. $byt[2] = ($val >> 16 & 0xff);
  50. $byt[3] = ($val >> 24 & 0xff);
  51. return $byt;
  52. }
  53. public static function getBytes($string)
  54. {
  55. $bytes = array();
  56. for ($i = 0; $i < strlen($string); $i++) {
  57. $bytes[] = ord($string[$i]);
  58. }
  59. return $bytes;
  60. }
  61. public static function getTeaKey($key)
  62. {
  63. $ints = array();
  64. $ints[0] = intval(substr($key, 0, 8),16);
  65. $ints[1] = intval(substr($key, 8, 8),16);
  66. $ints[2] = intval(substr($key, 16, 8),16);
  67. $ints[3] = intval(substr($key, 24, 8),16);
  68. return $ints;
  69. }
  70. public static function strToHex($string)//字符串转十六进制
  71. {
  72. $hex = "";
  73. for ($i = 0; $i < strlen($string); $i++)
  74. $hex .= dechex(ord($string[$i]));
  75. $hex = strtoupper($hex);
  76. return $hex;
  77. }
  78. }
  79. print_r(EncryptUtil::getTeaKey("12345678123456781234567812345678"));
  80. print_r(ord('a'));