| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /**
- * 图像加密解密.
- * @auth nj911<nj-x@gmail.com>
- * @date: 2021-04-18 15:36
- */
- namespace App\Repositories\Utils;
- use File;
- class ImageCryptRepository
- {
- /**
- * AES密钥,所有项目都代码里写死.
- * @var string
- */
- protected $secret = 'OlL7iiVrC23VWioD';
- /**
- * 算法.写死,不可变.
- * @var string
- */
- protected $cipher = 'AES-128-ECB';
- /**
- * 向量.
- * @var string
- */
- protected $iv = '';
- /**
- * 标记.
- * @var int
- */
- protected $option = OPENSSL_RAW_DATA;
- /**
- * 加密字节数据.
- * @param $raw
- * @param $toPath
- * @return bool|int
- */
- public function encodeRaw($raw)
- {
- return aes_encrypt($raw, $this->secret, $this->iv, $this->cipher, $this->option);
- }
- /**
- * 解密字节数据
- * @param $raw
- * @return string
- */
- public function decodeRaw($raw)
- {
- return aes_decrypt($raw, $this->secret, $this->iv, $this->cipher, $this->option);
- }
- /**
- * 加密并保存.
- * @param $raw
- * @param $toPath
- * @return bool|int
- */
- public function encode($raw, $toPath)
- {
- if (File::exists($toPath)) {
- File::delete($toPath);
- }
- return File::put($toPath, $this->encodeRaw($raw));
- }
- /**
- * 解密并保存.
- * @param $raw
- * @param $toPath
- * @return bool|int
- */
- public function decode($raw, $toPath)
- {
- if (File::exists($toPath)) {
- File::delete($toPath);
- }
- return File::put($toPath, $this->decodeRaw($raw));
- }
- }
|