PHP/JS/LINUX:JS encryption (RSA public Key Cryptography) PHP decryption (RSA private key decryption)
One:
JS RSA Plugin
Https://github.com/UFO0001/WX_RSA
Or:
Https://files.cnblogs.com/files/achengmu/WX_RSA-master.zip
Two:
Server generated by code: Private_key/public_key Two files
The PHP code is as follows:
<?php/** * Created by Phpstorm. * User:administrator * DATE:2017/11/10 * time:15:29 */class rsalogic {private $public _key = ';//public Key private $p Rivate_key = "; Private key Private $public _key_resource = '; Public key Resource Private $private _key_resource = "; Private key Resource/** * Schema function * @param [string] $public _key_file [public key file Address] * @param [string] $private _key_file [private key file Address] */Public function __construct ($public _key_file, $private _key_file) {try {if (!file_exists ($p Ublic_key_file) | | !file_exists ($private _key_file)) {throw new Exception (' key file no exists '); if (false = = ($this->public_key = file_get_contents ($public _key_file)) | | false = = ($this->private_key = file_get_contents ($private _key_file)) {throw new Exception (' read key file Fail '); } if (false = = ($this->public_key_resource = $this->is_bad_public_key ($this->public_key)) | | false = = ( $this->PRIvate_key_resource = $this->is_bad_private_key ($this->private_key)) {throw new Exception (' Public ke Y or private key no usable '); }} catch (Exception $e) {die ($e->getmessage ()); }} Private Function Is_bad_public_key ($public _key) {return openssl_pkey_get_public ($public _key); } Private Function Is_bad_private_key ($private _key) {return openssl_pkey_get_private ($private _key); /** * Generate a pair of public and private keys to successfully return a public-private key array failed to return FALSE * * * Create_key () {$res = Openssl_pkey_new (); if ($res = = false) return false; Openssl_pkey_export ($res, $private _key); $public _key = openssl_pkey_get_details ($res); Return Array (' public_key ' = $public _key["key"], ' private_key ' = $private _key); }/** * Encrypted with private key */Public Function Private_encrypt ($input) {openssl_private_encrypt ($input, $output, $t His->private_key_resource); Return base64_eNcode ($output); /** * Decrypt secret key after encryption */Public Function Public_decrypt ($input) {Openssl_public_decrypt (Base64_decode ($input), $output, $this->public_key_resource); return $output; }/** * Encrypted with public key */Common function Public_encrypt ($input) {openssl_public_encrypt ($input, $output, $thi S->public_key_resource); Return Base64_encode ($output); }/** * Decrypt the public key after encrypting the ciphertext * * * PRIVATE_DECRYPT ($input) {Openssl_private_decrypt (Base64_deco De ($input), $output, $this->private_key_resource); return $output; }}
Example:
$all = $this->getpost (' all '); $public _key = dirname (__dir__). ' /logic/public_key.txt '; $private _key = dirname (__dir__). ' /logic/private_key.txt '; $rsa = new rsalogic ($public _key, $private _key); $all = $rsa->private_decrypt ($all); echo $all; exit;
PHP/JS/LINUX:JS encryption (RSA public Key Cryptography) PHP decryption (RSA private key decryption)