小智 49
您可以使用phpseclib,一个纯PHP RSA实现:
<?php
include('Crypt/RSA.php');
$privatekey = file_get_contents('private.key');
$rsa = new Crypt_RSA();
$rsa->loadKey($privatekey);
$plaintext = new Math_BigInteger('aaaaaa');
echo $rsa->_exponentiate($plaintext)->toBytes();
?>
Run Code Online (Sandbox Code Playgroud)
Pra*_*tik 23
class MyEncryption
{
public $pubkey = '...public key here...';
public $privkey = '...private key here...';
public function encrypt($data)
{
if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
$data = base64_encode($encrypted);
else
throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
return $data;
}
public function decrypt($data)
{
if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
$data = $decrypted;
else
$data = '';
return $data;
}
}
Run Code Online (Sandbox Code Playgroud)
2017年(或此后)写入的旨在加入严重加密的申请不应再使用RSA.PHP公钥加密有更好的选择.
人们在决定使用RSA加密时会犯两个大错误:
sodium_crypto_box_seal()(libsodium)$keypair = sodium_crypto_box_keypair();
$publicKey = sodium_crypto_box_publickey($keypair);
// ...
$encrypted = sodium_crypto_box_seal(
$plaintextMessage,
$publicKey
);
// ...
$decrypted = sodium_crypto_box_seal_open(
$encrypted,
$keypair
);
Run Code Online (Sandbox Code Playgroud)
简单而安全.Libsodium将以PHP 7.2提供,或者通过PECL提供早期版本的PHP.如果您需要纯PHP 填充,请获取paragonie/sodium_compat.
2017年使用RSA的唯一原因是,"我禁止安装PECL扩展,因此不能使用libsodium,因为某些原因也不能使用paragonie/sodium_compat."
您的协议应如下所示:
不要自己实现,请查看EasyRSA.
| 归档时间: |
|
| 查看次数: |
107937 次 |
| 最近记录: |