JSON string Asymmetric encryption problem

Source: Internet
Author: User
Tags control characters decrypt openssl rsa asymmetric encryption
This post was last edited by Gwrc_s_d_n on 2013-08-12 10:20:07

Another recent project is the need to send HTTP requests in PHP, and to use asymmetric encryption. The part where encryption is required is the JSON string.
Now test the problem is, if directly with the public key encryption JSON string to the server side, will be decrypted failure (client test encryption and decryption normal), if Base64_encode after the encryption server, can decrypt the success (this is a bit troublesome, JSON to Base64 once, Encrypted and then Base64 once again).

Would you like to ask if you should not encrypt the JSON string directly? Or am I having a problem sending an HTTP request? is to use http_client hair request, directly with Fsockopen also not.


$data [' username '] = ' test133ed ';
$data [' content '] = Base64_encode (Json_encode (' Brid ' + ' 22228222883956 ', ' money ' = 10000000, ' time ' + Time ()))); If there's no base64_encode here, it can't be decrypted without a pass.
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::quickpost which function is this?

General Base64 after can, not before, most of the original data contains control characters (not visible), and then passed as a string caused by the loss of bytes, should be changed to byte mode, specific to the description of the module you use, I do not know the OpenSSL

Httpclient::quickpost I under the version of Ah, but not static, I changed to static.

Really don't understand,
If it is a transmission problem, I have encrypted data base64 encoded before transmission, there should be no control characters ah.
Suppose that OpenSSL RSA cannot encrypt the JSON string, but is it normal for me to decrypt the client?

The HTTP protocol will encode the post data, and the server will decode the post data before decrypting it.

It seems that HTTP does not encode the post, which is a string of strings that resemble get values. Look at the HTTP request packet that you sent.

Whether the need for Base64 is an algorithmic decision, not a matter of no trouble
You should track the results of each step in order to determine if it's where you didn't get the right

At least the JSON string after Json_encode contains a large number of double quotes, how do you know it doesn't affect the encryption algorithm?

Because encryption and then the decryption of the normal, encryption can not encrypt double quotes this is a bit unreasonable, so I think the transmission problem may be larger. But don't know how to check, grab the bag?

It seems that HTTP does not encode the post, which is a string of strings that resemble get values. Look at the HTTP request packet that you sent.

Of course it's coded, urlencode. More than 127 will be encoded.

Why base64 after no problem, base64 after all is <127, not affected by UrlEncode.

Upstairs still don't understand my meaning, I mean the encryption Base64 encoded JSON string can, directly encrypt the JSON string can not. As for the transmission, it must be coded.

Like HTTP does not encode a post, it is a string similar to a get value. Look at the HTTP request packet that you sent.

Through Base64 encryption once, accept the time, again Base64 once, and transmit the media should be no problem

  • Related Article

    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.