[PHP]:AES对称加密 -- 支持PHP7
[PHP]:AES对称加密 -- 支持PHP7
加密类Aes.php
/** * [AesSecurity aes加密,支持PHP7.1] */ class Aes { /** * [encrypt aes加密] * @param [type] $input [要加密的数据] * @param [type] $key [加密key] * @return [type] [加密后的数据] */ public static function encrypt($input, $key) { $data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA); $data = base64_encode($data); return $data; } /** * [decrypt aes解密] * @param [type] $sStr [要解密的数据] * @param [type] $sKey [加密key] * @return [type] [解密后的数据] */ public static function decrypt($sStr, $sKey) { $decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA); return $decrypted; } }
使用类:
<?php require_once "Aes.php"; $data['name'] = 'Tom'; $data['age'] = '20'; $key = '*mndbaehejay7&#'; $privEncrypt = Aes::encrypt(json_encode($data),$key); echo '加密后:'.$privEncrypt.'<br>'."\n"; $publicDecrypt = Aes::decrypt($privEncrypt,$key); echo '解密后:'.$publicDecrypt.'<br>'."\n";