PHP开发接口使用RSA解密数据
KarlXu 2017-04-26 14:29:15原创浏览量2275
// rsa 私钥解密
private static function rsa_priv_decode($data)
{
return urldecode(
self::privDecrypt(
$data,
file_get_contents(base_path('keys/rsa_private_key.pem')) //私钥的路径
)
);
}
/*
* 获取私钥
*/
private static function getPrivateKey($privateKey)
{
return openssl_pkey_get_private($privateKey);
}
/**
* 私钥解密
*/
public static function privDecrypt($encrypted, $privateKey)
{
if (!is_string($encrypted)) {
return null;
}
return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey($privateKey))) ? $decrypted : null;
}
//解密数据
$param = self::rsa_priv_decode($data);
return empty($param) ? false : $param;