1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace App\Service\ChatGroup;
- class EncDecService
- {
- protected static $method = 'DES-ECB';
- protected static $secret = 'YXCHgJ8VWUqTm1K3';
- public static function encrypt($dataJson, $timestamp)
- {
- if (!self::checkEffectiveTime($timestamp)) return [false, '时间戳失效'];
- $excStr = openssl_encrypt($dataJson, self::$method, self::$secret, strval($timestamp));
- $encStr = base64_encode($excStr);
- return [true, $encStr];
- }
- public static function decrypt($encStr, $timestamp)
- {
- if (!self::checkEffectiveTime($timestamp)) return [false, '时间戳失效'];
- $excStr = base64_decode($encStr);
- $dataJson = openssl_decrypt($excStr, self::$method, self::$secret, strval($timestamp));
- return [true, $dataJson];
- }
- protected static function checkEffectiveTime($timestamp)
- {
- return ((time() - $timestamp) > 60) ? false : true;
- }
- }
|