Json string asymmetric encryption

Source: Internet
Author: User
Asymmetric Encryption of json strings This post was last edited by gwrc_s_d_n from 2013-08-12:20:07 for another project. it is required to send http requests in PHP and use asymmetric encryption. The encrypted part is a json string. Now the test problem is that if the json string is directly encrypted with the public key to the server, the decryption will fail (the client will test the encryption and then decrypt it normally). If the base json string is asymmetric encrypted

This post was last edited by gwrc_s_d_n at 10:20:07

Recently, another project needs to send http requests in PHP and use asymmetric encryption. The encrypted part is a json string.
Now the test problem is that if the json string is directly encrypted with the public key to the server, the decryption will fail (the client will test the encryption and then decrypt it normally). if base64_encode is followed by the encrypted transfer server, it can be decrypted successfully (this is a little troublesome, json requires base64 once, and base64 again after encryption ).

I want to ask, shouldn't I encrypt the json string directly? Or is there a problem when I send an HTTP request? It is a request sent using http_client, and fsockopen cannot be used directly.


$ Data ['username'] = 'test133ed ';
$ Data ['content'] = base64_encode (json_encode (array ('brid' => '2013', 'Money' => 22228222883956, 'Time' => time (); // If base64_encode is not used here, it cannot be decrypted if it is passed in.
Openssl_sign ($ data ['content'], $ data ['sign'], $ privkey );

Openssl_public_encrypt ($ data ['sign'], $ sign, $ serverkey );
$ Data ['sign'] = base64_encode ($ sign );

Openssl_public_encrypt ($ data ['content'], $ data ['content'], $ serverkey );
$ Data ['content'] = base64_encode ($ data ['content']);

$ PageContents = HttpClient: quickPost ('http: // www. test. t: 80/index. php/agent/store', $ data );
$ Result = explode (',', $ pageContents );
Print_r ($ result );

Share:


------ Solution --------------------
HttpClient: Which function is quickPost?

Generally, it can be performed after base64, but not before. most of the original data contains control characters (not visible), which are then transmitted in string mode, resulting in loss of bytes. it should be transferred in byte mode instead, for more information, see the description of the modules you are using.
------ Solution --------------------
Whether base64 is required depends on the algorithm, rather than the trouble.
You should track the computing results of each step to determine if you are not correct

At least the json string after json_encode contains a large number of double quotation marks. how do you know it will not affect the encryption algorithm?
------ Solution --------------------
Reference:
It seems that http does not encode the post, but is a string similar to the get value. Check the sent http request package.


Of course, it will be encoded. if the URL code is greater than 127, it will be encoded.
------ Solution --------------------
No problem after base64. after base64, it is <127, not affected by urlencode.
------ Solution --------------------
It is encrypted once through base64. when it is accepted, it is base64 again, and there should be no problem with the transmitted media.
.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.