企微短剧业务系统

EncDecService.php 952B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Service\ChatGroup;
  3. class EncDecService
  4. {
  5. protected static $method = 'DES-ECB';
  6. protected static $secret = 'YXCHgJ8VWUqTm1K3';
  7. public static function encrypt($dataJson, $timestamp)
  8. {
  9. if (!self::checkEffectiveTime($timestamp)) return [false, '时间戳失效'];
  10. $excStr = openssl_encrypt($dataJson, self::$method, self::$secret, strval($timestamp));
  11. $encStr = base64_encode($excStr);
  12. return [true, $encStr];
  13. }
  14. public static function decrypt($encStr, $timestamp)
  15. {
  16. if (!self::checkEffectiveTime($timestamp)) return [false, '时间戳失效'];
  17. $excStr = base64_decode($encStr);
  18. $dataJson = openssl_decrypt($excStr, self::$method, self::$secret, strval($timestamp));
  19. return [true, $dataJson];
  20. }
  21. protected static function checkEffectiveTime($timestamp)
  22. {
  23. return ((time() - $timestamp) > 60) ? false : true;
  24. }
  25. }