天天看点

php rsa签名验签,PHP实现RSA签名和验签

define('ICLOD_CERT_PATH',dirname(__FILE__).'/xx_pri.key' ); //私钥文件

define('ICLOD_CERT_PUBLIC_PATH',dirname(__FILE__).'/xx_pub.key' );//公钥文件

class RSA{

public function sign($data){

$priKey = file_get_contents(ICLOD_CERT_PATH);

$res = openssl_get_privatekey($priKey);

openssl_sign($data, $sign, $res);

openssl_free_key($res);

//base64编码

$sign = base64_encode($sign);

return $sign;

}

public function rsa($data){

$encryptData="";

$priKey = file_get_contents(ICLOD_CERT_PATH);

$res=openssl_get_privatekey($priKey);

$result =openssl_private_encrypt($data, $encryptData, $res,OPENSSL_PKCS1_PADDING);

openssl_free_key($res);

return base64_encode($encryptData);

}

public function decryptRSA($data){

$decryptData ='';

$publickey =file_get_contents(ICLOD_CERT_PUBLIC_PATH);

$res = openssl_pkey_get_public($publickey);

$result=openssl_public_decrypt(base64_decode($data), $decryptData, $res);

return $decryptData;

}

public function verify($data){

$publickey =file_get_contents(ICLOD_CERT_PATH);

$res=openssl_get_publickey($publickey);

$result = (bool)openssl_verify($data['signedValue'], base64_decode($data['sign']), $res);

openssl_free_key($res);

return $result;

}

}

公私钥详情:https://www.cnblogs.com/wt645631686/p/8390936.html