用于微信转账到银行卡的实名验证(公钥加密)
https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_7
//采用标准RSA算法,公钥由微信侧提供
//PASK#1 转 PASK#8格式 公钥
$public_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A' . $public_key;
//组装公钥
$public_key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(trim($public_key), 64, "\n", true) . "\n-----END PUBLIC KEY-----\n";
//RSA编码(需要编码的数据,编码结果,公钥,编码类型)
openssl_public_encrypt($data, $crypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING);
//使用GuzzleHttp\Client发起请求时,添加证书文件
$config = array(
'cert' => 'cert.pem',
'ssl_key' => 'key.pem',
);
百度小程序支付(密钥加密,公钥解密)
https://dianshang.baidu.com/platform/doclist/index.html#!/doc/nuomiplus_2_base/sign_v2.md
// 加密
// 将待签名字符串和业务方私钥使用SHA1WithRSA签名算法得出最终签名
$privateKey = '-----BEGIN RSA PRIVATE KEY-----' . "\n";
$privateKey .= implode("\n", str_split($privateKey, 64)) . "\n";
$privateKey .= '-----END RSA PRIVATE KEY-----';
$privateKey = openssl_pkey_get_private($privateKey);
openssl_sign($data, $sign, $privateKey);
openssl_free_key($privateKey);
return base64_encode($sign);
// 解密
$publicKey = '-----BEGIN PUBLIC KEY-----' . "\n";
$publicKey .= implode("\n", str_split($publicKey, 64)) . "\n";
$publicKey .= '-----END PUBLIC KEY-----';
$publicKey = openssl_pkey_get_public($publicKey);
$result = (bool)openssl_verify($signData, base64_decode($sign), $publicKey);
openssl_free_key($publicKey);