PHP7中如何使用AESECBPKCS5Padding进行加密操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计529个文字,预计阅读时间需要3分钟。
本章节为大家介绍PHP7实现AES/ECB/PKCS5Padding加密的方法。以下是一种参考的实现方式,可供朋友参考。
phpclass CryptAES{ /** * 加解密方法 * * @param string $method 加解密方式,'encrypt' 为加密,'decrypt' 为解密 * @param string $data 待处理数据 * @param string $key 密钥 * @param string $iv 初始化向量,对于ECB模式可留空 * @return string 加解密结果 */ public static function encryptDecrypt($method, $data, $key, $iv='') { $cipher='aes-128-cbc'; // 可以根据需要修改加密算法 $result='';
if ($method==='encrypt') { // 加密 $options=OPENSSL_RAW_DATA; $result=openssl_encrypt($data, $cipher, $key, $options, $iv); } elseif ($method==='decrypt') { // 解密 $options=OPENSSL_RAW_DATA; $result=openssl_decrypt($data, $cipher, $key, $options, $iv); }
return $result; }}
此代码段中,`encryptDecrypt` 方法接受四个参数:加解密方式 (`$method`),待处理数据 (`$data`),密钥 (`$key`) 和初始化向量 (`$iv`)。对于ECB模式,`$iv` 可留空。
希望这对大家有所帮助。如有需要,可进一步参考相关文档。
class CryptAES { /** * 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没什么问题) */ 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 : exit('key为必须项'); $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); } }
本文共计529个文字,预计阅读时间需要3分钟。
本章节为大家介绍PHP7实现AES/ECB/PKCS5Padding加密的方法。以下是一种参考的实现方式,可供朋友参考。
phpclass CryptAES{ /** * 加解密方法 * * @param string $method 加解密方式,'encrypt' 为加密,'decrypt' 为解密 * @param string $data 待处理数据 * @param string $key 密钥 * @param string $iv 初始化向量,对于ECB模式可留空 * @return string 加解密结果 */ public static function encryptDecrypt($method, $data, $key, $iv='') { $cipher='aes-128-cbc'; // 可以根据需要修改加密算法 $result='';
if ($method==='encrypt') { // 加密 $options=OPENSSL_RAW_DATA; $result=openssl_encrypt($data, $cipher, $key, $options, $iv); } elseif ($method==='decrypt') { // 解密 $options=OPENSSL_RAW_DATA; $result=openssl_decrypt($data, $cipher, $key, $options, $iv); }
return $result; }}
此代码段中,`encryptDecrypt` 方法接受四个参数:加解密方式 (`$method`),待处理数据 (`$data`),密钥 (`$key`) 和初始化向量 (`$iv`)。对于ECB模式,`$iv` 可留空。
希望这对大家有所帮助。如有需要,可进一步参考相关文档。
class CryptAES { /** * 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没什么问题) */ 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 : exit('key为必须项'); $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); } }

