1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| /** * @param string $string 需要加密的字符串 * @param string $key 密钥 * @return string */ function encrypt($string, $key = '9c093d47ea6feb8a5af5bd19fb758ece') { $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16); $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA); $data = strtoupper(bin2hex($data)); return $data; }
/** * @param string $string 需要解密的字符串 * @param string $key 密钥 * @return string */ function decrypt($string, $key = '9c093d47ea6feb8a5af5bd19fb758ece') { $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16); $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $key, OPENSSL_RAW_DATA); return $decrypted; }
|