Skip to content

Instantly share code, notes, and snippets.

@blanklin030
Last active May 31, 2018 12:17
Show Gist options
  • Save blanklin030/66419da86d0ef651b5854476a6b2b4fc to your computer and use it in GitHub Desktop.
Save blanklin030/66419da86d0ef651b5854476a6b2b4fc to your computer and use it in GitHub Desktop.
use openssl to replace mcrypt in php7
class AesCipher {
/**
* 加密密钥
* @var string
*/
private $key = MemberCode::MOBILE_ENCRYPT_KEY;
/**
* 加密算法
* @var string
*/
private $method = 'AES-128-ECB';
/**
* openssl_encrypt加密
* 相当于
* 将mcrypt_encrypt的加密结果
* 再执行一次base64_encode
* @param $encrypt
* @param string $key
* @return string
*/
public function encrypt($encrypt, $key="") {
if (empty($key)){
$key = $this->key;
}
$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$encrypted = openssl_encrypt($encrypt, $this->method, $key2);
return $encrypted;
}
/**
* openssl_decode解密
* 相当于
* 先将加密结果执行一次base64_decode
* 然后再通过mcrypt_encrypt解密
* @param $decrypt
* @param string $key
* @return string
*/
public function decrypt($decrypt, $key='') {
if (empty($key)){
$key = $this->key;
}
$key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
$decrypted = openssl_decrypt($decrypt, $this->method, $key2);
return $decrypted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment