PHP AES进行数据库加密 tp5.0
tp5.0
1.加密类库路径 \extend\aes\Aes.php
2.公共调用方法路径 \application\common.php; 加密方法encrypt;解密方法decrypt
3.控制器调用使用方法: 加密:encryp('字段'); 解密 decrypt('加密后字段');
类库文件AES
<?php namespace aes; # 生成命名空间 class Aes { /** * var string $method 加解密方法,可通过openssl_get_cipher_methods()获得 */ protected $method; /** * var string $secret_key 加解密的密钥 */ protected $secret_key; /** * var string $iv 加解密的向量,有些方法需要设置比如CBC */ protected $iv; /** * var string $options (不知道怎么解释,目前设置为0没什么问题) 大概理解模式 NoPadding模式 PKCS5Padding模式 */ protected $options; /** * 构造函数 * * @param string $key 密钥 * @param string $method 加密方式 * @param string $iv iv向量 * @param mixed $options 还不是很清楚 * */ public function __construct($key, $method = 'aes-128-ECB', $iv = '', $options = 0) { // key是必须要设置的 $this->secret_key = isset($key) ? $key : 'abcdefgh20161234'; $this->method = $method; // 密码算法 $this->iv = $iv; $this->options = $options; } /** * 加密方法,对数据进行加密,返回加密后的数据 * @param string $data 要加密的数据 * @return string */ public function encrypt($data) { return openssl_encrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } /** * 解密方法,对数据进行解密,返回解密后的数据 * @param string $data 要解密的数据 * @return string */ public function decrypt($data) { return openssl_decrypt($data, $this->method, $this->secret_key, $this->options, $this->iv); } /**----执行 NoPadding 模式-并且使用base64处理----------------------------------------------------*/ /** * 加密方法,对数据进行加密,返回加密后的数据 * @param string $data 要加密的数据 * @return string * */ public function encryptBase($data) { return openssl_encrypt($data, $this->method, $this->secret_key, 1,$this->iv); } /** * 解密方法,对数据进行解密,返回解密后的数据 * @param string $data 要解密的数据 * @return string */ public function decryptBase($data) { $decrypted = openssl_decrypt($data, $this->method, $this->secret_key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING,$this->iv); $str = preg_replace('/[\x00-\x1F\x7F]/', '', $decrypted); //去除 空格 return $str; } }
# 另一种解决解决JAVA 端加密使用了SHAIPRNG
/** * 加密方法,对数据进行加密,返回加密后的数据 * @param string $data 要加密的数据 * @return string * @注释:加密方法 处理JAVA端加密时使用了SHA1PRNG */ public function encrypt($data) { $data_key = substr(openssl_digest(openssl_digest($this->secret_key, 'sha1', true), 'sha1', true), 0, 16); return openssl_encrypt($data, $this->method, $data_key, $this->options, $this->iv); } /** * 解密方法,对数据进行解密,返回解密后的数据 * @param string $data 要解密的数据 * @return string * @注释:加密方法 处理JAVA端加密时使用了SHA1PRNG */ public function decrypt($data) { $data_key = substr(openssl_digest(openssl_digest($this->secret_key, 'sha1', true), 'sha1', true), 0, 16); return openssl_decrypt($data, $this->method, $data_key, $this->options, $this->iv); }
公共调用类库 注:调用类库 use aes\Aes; 需在AES生成命名空间
/** * [encrypt description] * @Author:XuXianGang * @Method:加密 * @DateTime:2022-08-11T09:51:30+0800 * @param string $value [description] * @return [type] [description] */ function encrypt ($value='') { $aes = new Aes("xiangang"); // 该参数为key(加密参数值) $encode = $aes->encrypt($value); return $encode; } /** * [decrypt description] * @Author:XuXianGang * @Method:解密 * @DateTime:2022-08-11T09:51:38+0800 * @param string $value [description] * @return [type] [description] */ function decrypt ($value='') { $aes = new Aes("xiangang"); // 该参数为key(解密参数值) $encode = $aes->decrypt($value); return $encode; }
另一钟加密解密方式 并且使用 Base64加密解密
/** * [encrypt description] * @Author:XuXianGang * @Method:加密 先aes加密 -- 在base64解密 * @DateTime:2022-08-11T09:51:30+0800 * @param string $value [description] * @return [type] [description] */ function encryptBase ($value='') { $aes = new Aes("abcdefgh20161234"); // 该参数为key(加密参数值) $encrypt = $aes->encryptBase($value); //加密 $encryptBase64 = base64_encode($encrypt); return $encryptBase64; } /** * [decrypt description] * @Author:XuXianGang * @Method:解密 先base64 解密 在aes 加密 * @DateTime:2022-08-11T09:51:38+0800 * @param string $value [description] * @return [type] [description] */ function decryptBase ($value='') { $aes = new Aes("abcdefgh20161234"); // 该参数为key(解密参数值) //先Base64 $decryptBase64 = base64_decode($value); $encode = $aes->decryptBase($decryptBase64); return $encode; }

浙公网安备 33010602011771号