1、加密代碼以下:加密
/** * * @param string $string 須要加密的字符串 * @param string $key 密鑰 * @return string */ public static function encrypts($string, $key) { $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16); // openssl_encrypt 加密不一樣Mcrypt,對祕鑰長度要求,超出16加密結果不變 $data = openssl_encrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA); $encrypted = strtolower(bin2hex($data)); return $encrypted; }
2、解密代碼以下:spa
/** * @param string $string 須要解密的字符串 * @param string $key 密鑰 * @return string */ public static function decrypts($string, $key) { $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; }