对接第三方api,由于第三方使用java加密,需要先对内容base64处理,因此这里也是先对传输内容进行base64处理后进行openssl_sign加密,但是返回的是乱码。
$privary = file_get_contents('private_key.pem');
$pkeyid = openssl_pkey_get_private($privary);
$content = '<?xml version="1.0" encoding="UTF8"?><request><head><msgId>115454545454545hghghghgh5465ghghg</msgId><timeStamp>20160110103012304</timeStamp><session></session><userCode>a77b0ae7-af59-4</userCode></head><body><cityMsg><searchTxt>g</searchTxt><page>1</page><count>20</count></cityMsg></body></request>';
$content = base64_encode($content);
openssl_sign($content, $signature, $privary, "SHA256");
print $signature;
这里是输出的内容:
mm|†Ñ±ù¼®²OW®W‰EdŽáWѤ{†Q&3tÎtþù¹¤5ŸJYLß,@±½Qlî†ÓûñB=ÑF͵OPÔZaîöÃý¯s\^¼žFOôâåä6µµ¬Js¤våaãSchq üî*ªo³¢3<‹adÿ«˜Lí™q.è &ÂW¬b+…Çênƒy·“KM`–GrìmÈà”è#\ÕQ"½H™Wßr-CB8Ä\ lÅŵü34½˜–þæw÷ w"xåívwѯÎÄ*꾈Ñd0ÉÿíbÉ‹#V+Ï×úÚë$JÀ†J=C™‹ë)öó
正确的加密结果应该是这样的:
bW0TF3yG0bH5vK6yA09XrleJRWSO4VfDkaR7AIZRJjN0znT+FvkSuaQ1n0pZBkzfLI1Asb1RbAjuhtP78UI90UbNtU9Q1Fph7vbDH/2vc1xevJ5GT/Ti5eQ2tbWsSnPCpHblYQTjU39jaHEN/O4qqm+zoggzEjyLYWT/q5hM7ZlxLgvoDAkmwlcbrGIrhcfqFG6DeZC3k0tNYMKWR3LsbcjgjZToI1zVUSK9SJmBV99yLUMOQjjEXBsgbMXFtR78MzS9mJb+5nf3Co13Injl7XZ30a/OxCrqvojRZDDJ/+1iyYsjVisUAa3P1/qP2uskSsCGSj0UQ5mL650TKfbzFg==
不论是
RSA
,还是DES
、AES
等加密算法,加密之后本质就是是ascii字符串
(0x00-0xff),要可视化或者传输 可以
base64 encode
解密需要先
base64 decode
然后解密