search
HomeBackend DevelopmentPHP Tutorialjson 字符串非对称加密问题

本帖最后由 gwrc_s_d_n 于 2013-08-12 10:20:07 编辑

最近又个项目,是需要在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一次,和传输的介质应该没有问题

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

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-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

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.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

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' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

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

Discover File Downloads in Laravel with Storage::downloadDiscover File Downloads in Laravel with Storage::downloadMar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft