php - Using openssl_sign to encrypt content and output garbled characters
typecho
typecho 2017-06-19 09:07:12
0
1
2968

Connecting to the third-party API, since the third party uses Java encryption, the content needs to be base64 processed first. Therefore, the transmitted content is first base64 processed and then openssl_sign encrypted, but the returned code is garbled.

$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;

Here is the output:

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™‹ë)öó

The correct encryption result should be like this:

bW0TF3yG0bH5vK6yA09XrleJRWSO4VfDkaR7AIZRJjN0znT+FvkSuaQ1n0pZBkzfLI1Asb1RbAjuhtP78UI90UbNtU9Q1Fph7vbDH/2vc1xevJ5GT/Ti5eQ2tbWsSnPCpHblYQTjU39jaHEN/O4qqm+zoggzEjyLYWT/q5hM7ZlxLgvoDAkmwlcbrGIrhcfqFG6DeZC3k0tNYMKWR3LsbcjgjZToI1zVUSK9SJmBV99yLUMOQjjEXBsgbMXFtR78MzS9mJb+5nf3Co13Injl7XZ30a/OxCrqvojRZDDJ/+1iyYsjVisUAa3P1/qP2uskSsCGSj0UQ5mL650TKfbzFg==
typecho
typecho

Following the voice in heart.

reply all(1)
曾经蜡笔没有小新

Whether it is RSA, or DES, AES and other encryption algorithms, after encryption, it is essentially an ascii string (0x00-0xff),

To visualize or transmit, you can base64 encode

Decryption requires first base64 decode and then decryption

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!