当前位置:网站首页>PHP RSA encryption
PHP RSA encryption
2022-04-21 07:55:00 【Mars Murong】
<?php
class Rsa
{
private $privateKeyFilePath = 'rsa_private_key.pem';
private $publicKeyFilePath = 'rsa_public_key.pem';
// Encrypted data
private $encryptData = '';
private function test()
{
extension_loaded('openssl') or die('php need openssl Extended support ');
(file_exists($this->privateKeyFilePath) && file_exists($this->privateKeyFilePath)) or die(' Incorrect file path ');
$privateKey = openssl_pkey_get_private(file_get_contents($this->privateKeyFilePath));
$publicKey = openssl_pkey_get_public(file_get_contents($this->publicKeyFilePath));
($privateKey && $publicKey) or die(' The key or public key is not available ');
}
/**RSA Signature function
* $data For the data to be signed , such as URL
* The signature uses the game party's confidential private key , It must be without pkcs8 The conversion . The result needs to be base64 Code for URL Transmission of
* return Sign Signature
*/
public static function sign($data)
{
$priKey = file_get_contents('rsa_private_key.pem');
// Convert to openssl secret key , It must be without pkcs8 Converted private key
$res = openssl_get_privatekey($priKey);
// call openssl Built in signature method , Generate signature $sign
openssl_sign($data, $sign, $res);
openssl_free_key($res);
$sign = base64_encode($sign);
return $sign;
}
/**RSA Verify the signature
* $data For the data string to be verified
* $sign Is the signature data that needs to be verified , It's directly from URL Taken in $_POST["sign"] Type parameter , There will be... In the function base64_decode Of .
* return Whether the signature is passed , by BOOL value
*/
public static function verify($data, $sign)
{
// Read public key file , That is, the public key disclosed by the signer , To verify this data Whether it is really sent by the signatory
$pubKey = file_get_contents('key/rsa_public_key.pem');
$res = openssl_get_publickey($pubKey);
// call openssl Built in method signature verification , return bool value
$result = (bool)openssl_verify($data, base64_decode($sign), $res);
openssl_free_key($res);
return $result;
}
public function encrypt($originalData)
{
$encryptData = '';
// echo ' The original data is :', $originalData, PHP_EOL;
/// Encrypt with private key
if (openssl_private_encrypt($originalData, $encryptData, openssl_pkey_get_private(file_get_contents($this->privateKeyFilePath)))) {
// After encryption Sure base64_encode After that, it is convenient to transmit in the website
// echo ' Encryption succeeded , Encrypted data (base64_encode after ) by :', base64_encode($encryptData), PHP_EOL;
return ($encryptData);
} else {
// exit(' Encryption failed ');
return false;
}
}
public function decrypt($encryptData)
{
// Decryption with public key
// Decrypted data
$decryptData = '';
if (openssl_public_decrypt($encryptData, $decryptData, openssl_pkey_get_public(file_get_contents($this->publicKeyFilePath)))) {
return ($decryptData);
} else {
return false;
}
}
/**
* Encrypt with a private key
*/
public function private_encrypt($input)
{
openssl_private_encrypt($input, $output, file_get_contents($this->privateKeyFilePath));
return base64_encode($output);
}
/**
* Decrypt The ciphertext encrypted by private key
*/
public function public_decrypt($input)
{
openssl_public_decrypt(base64_decode($input), $output, file_get_contents($this->publicKeyFilePath));
return $output;
}
/**
* Encrypt with a public key
*/
public function public_encrypt($input)
{
openssl_public_encrypt($input, $output, $this->public_key_resource);
return base64_encode($output);
}
/**
* Decrypt Ciphertext encrypted by public key
*/
public function private_decrypt($input)
{
openssl_private_decrypt(base64_decode($input), $output, $this->private_key_resource);
return $output;
}
}
//$Rsa = new Rsa;
//$encrypt =$Rsa->encrypt($Rsa->originalData);
//$decrypt = $Rsa->decrypt($encrypt);
//var_dump( base64_encode($encrypt), $decrypt);
//die;
版权声明
本文为[Mars Murong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210629261816.html
边栏推荐
- Leetcode topic -- 120 Minimum length sum of triangles, simple DP
- SHA256 Hashes
- leetcode题目--120.三角形最小长度和,简单dp
- Build openwrt file system for i.mx6q development board
- 2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中
- php 二维数组转一维数组
- 服务器部署svn环境
- Time and Duration and Epoch
- Advanced C language pointer (1. First and second order pointers)
- C# asp.net 调百度文字识别接口
猜你喜欢

在vscode 中安装go插件并配置go环境以运行go

物联网操作系统Zephyr(入门篇)之1.0 Zephyr简介

Steps for umlet beginners

云原生KubeSphere实战多租户系统实战

动态规划定点突破 --leetcode题目64.最小路径和

【图像融合】基于拉普拉斯金字塔+小波变换实现图像融合含Matlab源码

Fuzzy query between two tables of SQL server and steps of importing Excel data into SQL Server

蓝牙开源协议栈BTstack之1.0 BTstack简介

Flutter first experience

第五站孔孟之乡-----------走迷宫之最短路径
随机推荐
Use of basic components of flutter
asp.net re正则表达式,筛选返回的字符串中时间单号金额,把不规范的时间格式转化为正确的时间格式
leetcode 19. Delete the penultimate node of the linked list
leetcode 27. Removing Elements
asp. Net re regular expression, filter the amount of time order number in the returned string, and convert the non-standard time format into the correct time format
oracle 管理之《sql命令》
PostgreSQL 15 will soon support merge statements in the SQL standard
kubesphere3.0忘记admin的密码
php 无限极分类(递归)
Environment Variables
PHP outputs all times between two specified dates
GoLang项目开发基础
leetcode 209. Minimum length subarray
Postgre(pg)-SQL脚本记录
playwright click timeout属性,等待元素出现的时间
Common SQL
leetcode题目--386.字典序排数,DFS
Unity performance optimization UI
蓝牙开源协议栈BTstack之1.0 BTstack简介
IDEA 使用@Autowired注解报红的解决办法