PKCS7Encoder.Class.php 866 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace JinDouYun\Controller;
  3. /**
  4. * PHP7.1及其之上版本的回调加解密类库
  5. * 该版本依赖openssl_encrypt方法加解密,注意版本依赖 (PHP 5 >= 5.3.0, PHP 7)
  6. */
  7. class PKCS7Encoder
  8. {
  9. public static $block_size = 32;
  10. function encode($text)
  11. {
  12. $block_size = PKCS7Encoder::$block_size;
  13. $text_length = strlen($text);
  14. $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size);
  15. if ($amount_to_pad == 0) {
  16. $amount_to_pad = PKCS7Encoder::block_size;
  17. }
  18. $pad_chr = chr($amount_to_pad);
  19. $tmp = "";
  20. for ($index = 0; $index < $amount_to_pad; $index++) {
  21. $tmp .= $pad_chr;
  22. }
  23. return $text . $tmp;
  24. }
  25. function decode($text)
  26. {
  27. $pad = ord(substr($text, -1));
  28. if ($pad < 1 || $pad > PKCS7Encoder::$block_size) {
  29. $pad = 0;
  30. }
  31. return substr($text, 0, (strlen($text) - $pad));
  32. }
  33. }
  34. ?>