PHP OpenSSL base64 decryption

Source: Internet
Author: User

First, a recent project access to the fourth party payment, fourth party payment provided by the document requires both sides of OpenSSL with the private key, the public key for RSA encryption to communicate with each other


Ii. Environmental Notes

1, lamp, LNMP can, my environment is lamp for the time being

2, PHP First install the OpenSSL extension, otherwise use the function will be error, specific OpenSSL extension can go to Baidu or reference blog document


Third, the code implementation

1. Send encrypted data

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/07/4B/wKiom1nHIzmypONkAABnpYPfLKs296.png-wh_500x0-wm_ 3-wmp_4-s_3212391313.png "title=" 1.png "alt=" Wkiom1nhizmyponkaabnpypflks296.png-wh_50 "/>

<?php

Encryption

Composing encrypted data

$arr = Array (

' Name ' + ' Test ',//Membership account for third-party platforms

' Password ' + ' test ',//Membership password for third-party platforms

' Amount ' = ' 100 ',//Transfer amount, minimum 100, integer multiples of 100

' Order_sn ' + ' 123456789abc ', the order number generated by//Trader's Exchange (third party platform needs to be returned after processing)

' Sign ' = ' 86E06157205D5155F5FDDDCF077604FD ', sign generated by//Trader's Exchange (third party platform needs to be returned after processing)

);

First turn to JSON-formatted data

$arr = Json_encode ($arr);

Get an encrypted string based on the private key

$key = "Private key content string";

$res = "-----BEGIN public KEY-----\ n".

WordWrap ($key, +, "\ n", true).

"\ n-----END public KEY-----";

openssl_private_encrypt ($aRR, $data, $res);

Base64 encoding

$data = Base64_encode ($data);

Send a POST request

$to _arr = Array (

' Data ' = $data

);

POST request ignored


2. Get encrypted string decryption

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/07/4D/wKiom1nHRv3yBZwWAACTiCZXr8s684.png-wh_500x0-wm_ 3-wmp_4-s_809127748.png "title=" 2.png "alt=" Wkiom1nhrv3ybzwwaacticzxr8s684.png-wh_50 "/>

<?php

Decrypt

$data = "Data encryption string returned by fourth party";

$key = "Fourth party provided public key content string";

$res = "-----BEGIN public KEY-----\ n".

WordWrap ($pubKey, +, "\ n", true).

"\ n-----END public KEY-----";

Decrypting a string

$crypto = ";

foreach (Str_split (Base64_decode ($data),) as $chunk) {

Openssl_public_decrypt ($chunk, $decryptData, $res);//$decryptData as decrypted data

$crypto. = $decryptData;

}

Parse JSON data to get an array

$arr = Json_decode ($crypto);

Print results

Var_dump ($arr);


This article is from the "12633313" blog, please be sure to keep this source http://12643313.blog.51cto.com/12633313/1968174

PHP OpenSSL base64 decryption

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.