| 12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace addons\QiDingDong\common\helper;
- class AesHelper
- {
- public static $aesMethod = "AES-128-CBC";
- public static $aesOptions = OPENSSL_RAW_DATA;
- public static $aesIv = "106d46f29f9ea389";
- /**
- * 对字符串进行加密
- *
- * @param mixed $plaintext
- * @return array [密文,加密key]
- */
- public static function encrypt($plaintext)
- {
- $key = time() . mt_rand(100000, 999999);
- $ciphertext = openssl_encrypt(json_encode($plaintext), self::$aesMethod, $key, self::$aesOptions, self::$aesIv);
- return [base64_encode($ciphertext), $key];
- }
- /**
- * 对密文进行解密
- *
- * @param string $ciphertext 密文
- * @param string $key 加密密钥
- * @return string|array|int 解密后数据
- */
- public static function decrypt(string $ciphertext, string $key)
- {
- $original_plaintext = openssl_decrypt(base64_decode($ciphertext), self::$aesMethod, $key, self::$aesOptions, self::$aesIv);
- return json_decode($original_plaintext, true);
- }
- }
|