json 字符串非对称加密问题
最近又个项目,是需要在PHP里发送http请求的,并且要用非对称加密。其中需加密的部分是json字符串。
现在测试问题是,如果直接用公钥加密json串传到服务器端,就会解密失败(客户端测试加密再解密正常),如果base64_encode后再加密传服务器,就能解密成功(这样有点麻烦,json要base64一次,加密后又要base64一次)。
想问下,是本就不应该直接对json字符串加密呢?还是我发送HTTP请求有问题?是用http_client发的请求,直接用fsockopen也不行。
$data['username'] = 'test133ed';
$data['content'] = base64_encode(json_encode(array('brid' => '22228222883956', 'money' => 10000000, 'time' => time()))); //这里不用base64_encode的话,传过去就不能解密
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);
回复讨论(解决方案)
HttpClient::quickPost 这个是哪家函数?
一般base64之后可以,之前不可以,多数是原来的数据含有控制字符(不可视),然后以字符串方式传递引起字节丢失,应改为字节方式传递,具体看你所用模块的说明,偶也不怎么晓得openssl
HttpClient::quickPost 我下的版本里有啊,只不过不是静态的,我改成了静态的而已。
实在不明白,
假如说是传输问题,我在传输前把加密数据base64编码了呀,应该不会有控制字符呀。
假如说openssl rsa不能对json字符串加密,但我再客户端解密是正常的呀?
http协议会对post的数据编码,服务器端先要对post数据解码,然后再解密吧
貌似http不会对post编码,就是一串类似get传值那种字符串而已。看发送的http请求包就知道了
是否需要 base64 是算法决定的,而不是麻不麻烦的事情
你应该跟踪每一步的计算结果,以便确定是否是你哪里没弄对
至少 json_encode 后的 json 串中含有大量的双引号,你如何就知道不会影响加密算法呢
因为加密后再本端解密就正常,加密不能加密双引号这个有点说不过去,所以我觉得还是传输出问题的可能比较大。但又不知道怎么检查,抓包么?
貌似http不会对post编码,就是一串类似get传值那种字符串而已。看发送的http请求包就知道了
当然会编码,urlencode啊.大于127的都会被编码。
为啥base64后没问题,base64后都是
楼上还是没理解我的意思,我是说加密base64编码的json字符串可以,直接加密json串就不可以。至于传输前,肯定是要编码的
似http不会对post编码,就是一串类似get传值那种字符串而已。看发送的http请求包就知道了
通过base64加密一次,接受的时候,再base64一次,和传输的介质应该没有问题
。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
