当前位置:网站首页>PHP uses RSA segment encryption and decryption method
PHP uses RSA segment encryption and decryption method
2022-06-12 10:29:00 【P P PHP】
File source code :
<?php
/**
* RSA Signature class
* Be careful : Public and private keys must be one line strings , And remove the generated head and tail
* belt “----” Two lines of , And take care to remove the newline from the last line
*/
class Rsa
{
public $publicKey = '';
public $privateKey = '';
private $_privKey;
private $encryptBlockSize = 117;// Encrypted cutting length
private $decryptBlockSize = 128;// Decryption cut length
/**
* * private key
*/
private $_pubKey;
/**
* * public key
*/
private $_keyPath;
/**
* * the keys saving path
*/
/**
* * the construtor,the param $path is the keys saving path
*/
function __construct($publicKey = null, $privateKey = null)
{
$this->setKey($publicKey, $privateKey);
}
/**
* Set public and private keys
* @param string $publicKey Public key
* @param string $privateKey Private key
*/
public function setKey($publicKey = null, $privateKey = null)
{
if (!is_null($publicKey))
$this->publicKey = $publicKey;
if (!is_null($privateKey))
$this->privateKey = $privateKey;
}
/**
* * setup the private key
*/
private function setupPrivKey()
{
if (is_resource($this->_privKey))
{
return true;
}
$pem = chunk_split($this->privateKey, 64, "\n");
$pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
$this->_privKey = openssl_pkey_get_private($pem);
return true;
}
/**
* * setup the public key
*/
private function setupPubKey()
{
if (is_resource($this->_pubKey))
{
return true;
}
$pem = chunk_split($this->publicKey, 64, "\n");
$pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
$this->_pubKey = openssl_pkey_get_public($pem);
return true;
}
/**
* * encrypt with the private key
*/
public function privEncrypt($data)
{
if (!is_string($data))
{
return null;
}
$this->setupPrivKey();
// Private key segment encryption
$result='';
$data = str_split($data, $this->encryptBlockSize);
foreach ($data as $block) {
openssl_private_encrypt($block, $encrypted, $this->_privKey);
$result .= $encrypted;
}
return $result ? base64_encode($result) : null;
/*$r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
if ($r)
{
return base64_encode($encrypted);
}
return null;*/
}
/**
* * decrypt with the private key
*/
public function privDecrypt($encrypted)
{
if (!is_string($encrypted))
{
return null;
}
$this->setupPrivKey();
$encrypted = base64_decode($encrypted);
// Segment decryption
$result = '';
$data = str_split($encrypted, $this->decryptBlockSize);
foreach ($data as $block) {
openssl_private_decrypt($block, $decrypted, $this->_privKey);
$result .= $decrypted;
}
return $result ? $result : null;
/* $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);
if ($r)
{
return $decrypted;
}
return null;*/
}
/**
* * encrypt with public key
*/
public function pubEncrypt($data)
{
if (!is_string($data))
{
return null;
}
$this->setupPubKey();
// Block encryption
$result='';
$data = str_split($data, $this->encryptBlockSize);
foreach ($data as $block) {
openssl_public_encrypt($block, $encrypted, $this->_pubKey);
$result .= $encrypted;
}
return $result ? base64_encode($result) : null;
/*$r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);
if ($r)
{
return base64_encode($encrypted);
}
return null;*/
}
/**
* * decrypt with the public key
*/
public function pubDecrypt($crypted)
{
if (!is_string($crypted))
{
return null;
}
$this->setupPubKey();
$crypted = base64_decode($crypted);
$result = '';
$data = str_split($crypted, $this->decryptBlockSize);
foreach ($data as $block) {
openssl_public_decrypt($block, $decrypted, $this->_pubKey);
$result .= $decrypted;
}
return $result ? $result : null;
/* $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);
if ($r)
{
return $decrypted;
}
return null;*/
}
/**
* Construct signature
* @param string $dataString Signed data
* @return string
*/
public function sign($dataString)
{
$this->setupPrivKey();
$signature = false;
openssl_sign($dataString, $signature, $this->_privKey);
return base64_encode($signature);
}
/**
* Verify the signature
* @param string $dataString Signed data
* @param string $signString Signed string
* @return number 1 The signature is correct 0 Signature error
*/
public function verify($dataString, $signString)
{
$this->setupPubKey();
$signature = base64_decode($signString);
$flg = openssl_verify($dataString, $signature, $this->_pubKey);
return $flg;
}
public function __destruct()
{
is_resource($this->_privKey) && @openssl_free_key($this->_privKey);
is_resource($this->_pubKey) && @openssl_free_key($this->_pubKey);
}
}
Usage method :
$rsa =new Rsa($public_key,$private_key);
// Private key decryption
$arginfo = " Public key encryption string ";
$y = $rsa->privDecrypt($arginfo);
$params = json_decode($y,true);
print_r($params);
// Private key encryption
$rsa =new Rsa($public_key,$private_key);
$string = $rsa->privEncrypt(" Encrypted string ");
echo $string;
边栏推荐
- Remote link software xshell and xftp download address
- Valentina Studio Pro for MAC (MAC database management software)
- [MySQL] index invalidation and index optimization
- Machine learning is not something you can use if you want to use it
- Binassii module - converting between binary and ASCII
- Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
- Leetcode2154. Multiply the found value by 2 (binary search)
- Code types and data structures corresponding to the five object types of redis
- PostgreSQL uses stored procedures to splice multiple tables and query data
- 2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
猜你喜欢

远程桌面不能复制粘贴解决办法

How high can C language reach by self-study alone?

Tp6 debugging (trace)

Circuitbreaker fuse of resilience4j - Summary

Pseudo static setting of access database in win2008 R2 iis7.5

On the improvement of 3dsc by harmonic shape context feature HSC

Valentina Studio Pro for Mac(mac数据库管理软件)

The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet

MQTT 协议中文版
![[CEGUI] concept introduction](/img/a6/695d7f1a4e57c8438e2d1a4cad11dc.jpg)
[CEGUI] concept introduction
随机推荐
Pycharm view the current version of opencv
ASP. Net core permission system practice (zero)
ASP.NET Core权限系统实战(零)
Tp6+memcached configuration
使用cpolar远程办公(2)
The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet
淺談調和形狀上下文特征HSC對3DSC的改進
One test for twoorthree years, recording some thoughts on test exchange experience
Valentina Studio Pro for MAC (MAC database management software)
Binassii module - converting between binary and ASCII
学生管理系统
[Mozilla] basic concept analysis of IPDL
数组,整型,字符变量在全局和局部的存在形式
Reverse analysis of Huawei housekeeper software [transfer]
JS string combination
Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
pycharm 查看opencv当前的版本
[experiment] MySQL master-slave replication and read-write separation
Mysql5.6.24 installation free deployment method
PLC如何自行构造移位功能块(FC)