sy-records/php-rsa

一个使用RSA算法进行加密和解密的PHP类。

v1.1 2018-07-22 04:32 UTC

This package is auto-updated.

Last update: 2024-09-04 20:09:09 UTC


README

一个使用RSA算法进行加密和解密的PHP类。

在查看示例之前,您需要生成RSA的公钥和私钥。

如果您不知道如何生成RSA的公钥和私钥,请阅读这篇博客文章:PHP如何使用OpenSSL生成RSA加密和解密所需的公钥和私钥?

安装

您可以使用composer安装此包。

composer require sy-records/php-rsa -vvv

使用方法

如果您想使用实例化方法,请查看以下内容。

<?php
use syrecords\Rsa;

$privateKey = './key/private_key.pem'; // Private key path
$publicKey = './key/rsa_public_key.pem'; // Public key path
$rsa = new Rsa($privateKey, $publicKey);

$originData = 'https://qq52o.me';

# Encryption with a private key requires public key decryption, called "signature".
$ecryptionData = $rsa->privEncrypt($originData);
$decryptionData = $rsa->publicDecrypt($ecryptionData);

echo 'The data encrypted by the private key is:' . $ecryptionData.PHP_EOL;
echo 'The data after the public key is decrypted is: ' . $decryptionData;

echo '<hr/>';

# Encryption with a public key requires private key decryption, called "encryption".
$ecryptionData = $rsa->publicEncrypt($originData);
$decryptionData = $rsa->privDecrypt($ecryptionData);

echo 'The data after public key encryption is:' . $ecryptionData.PHP_EOL;
echo 'The data after the private key is decrypted is:' . $decryptionData;

如果您想调用静态方法,请查看这个示例!

<?php
use syrecords\staticRsa;

$privateKey = file_get_contents('./key/private_key.pem'); // Private key path
$publicKey = file_get_contents('./key/rsa_public_key.pem'); // Public key path

$originData = 'https://qq52o.me';

$ecryptionData = staticRsa::privEncrypt($originData,$privateKey);
$decryptionData = staticRsa::publicDecrypt($ecryptionData,$publicKey);

echo 'The data encrypted by the private key is:' . $ecryptionData.PHP_EOL;
echo 'The data after the public key is decrypted is: ' . $decryptionData;

echo '<hr/>';

$ecryptionData = staticRsa::publicEncrypt($originData,$publicKey);
$decryptionData = staticRsa::privDecrypt($ecryptionData,$privateKey);
echo 'The data after public key encryption is:' . $ecryptionData.PHP_EOL;
echo 'The data after the private key is decrypted is:' . $decryptionData;

许可证

Apache-2.0