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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
