- RSA
$private_key = ;
$public_key = ;
$pu_key = openssl_pkey_get_public($public_key);
$pi_key = openssl_pkey_get_private($private_key);
$data = 'hello word';
//公钥加密
openssl_public_encrypt($data, $encrypted, $pu_key);
$encrypted = base64_encode($encrypted);
echo $encrypted,"<br/>";
// 私钥解密
openssl_private_decrypt(base64_decode($encrypted), $decrypted, $pi_key,OPENSSL_PKCS1_OAEP_PADDING);
echo $decrypted;
md5
hash('md5', 'hello word', false)
sha512
hash('sha512', 'hello word', false)
sha384
hash('sha384', 'hello word', false)
AES
// $iv 长度必须为16
$method = "AES-128-CBC";
$iv_length = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_length);
// 加密
$encrypted = openssl_encrypt($encrypted, $method, $key, 0, $iv);
// 解密
$decrypted = openssl_decrypt($encrypted, $method, $key, 0,$iv);