Asymmetric Encryption of json strings

Source: Internet
Author: User
Tags control characters openssl rsa
Json string asymmetric encryption. 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 );


Reply to discussion (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.

HttpClient: The quickPost version is available in me, but it is not static. I changed it to static.

I really don't understand,
If it is a transmission problem, I have base64 encoded the encrypted data before transmission. There should be no control characters.
Suppose that openssl rsa cannot encrypt json strings, but it is normal for me to decrypt the strings on the client?

The http protocol will encode the post data. the server must first decode the post data and then decrypt it.

It seems that http does not encode the post, but is a string similar to the get value. Check the sent http request package.

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?

It is normal to decrypt the data locally after encryption. encryption cannot encrypt double quotation marks, which is a bit difficult to say. Therefore, I think the transmission problem may be large. But I don't know how to check it. is it packet capture?

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.

No problem after base64. after base64, it is <127, not affected by urlencode.

I still don't understand what I mean. I mean, the base64 encoded json string can be encrypted, but the json string cannot be directly encrypted. Before transmission, it must be encoded.

It seems that http does not encode the post, but is a string similar to the get value. Check the sent http request package.

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.