2

如何使用 PHP 从 RSA 私钥中删除 passphase 我知道在 OpenSSL 中是这样的: openssl rsa -in key.key -out key.key 我正在 PHP 中搜索与此命令等效的命令。


RSA 命令需要通过

OpenSSL> rsa -in key2.key -out key2.key 
Enter pass phrase for key2.key:
4

2 回答 2

2

这将使用openssl扩展完成相同的操作:

$key = file_get_contents('key2.key');
$password = 'your password or pass phrase';

if (false === ($pkey = openssl_pkey_get_private($key, $password))) {
    die(openssl_error_string());
}
openssl_pkey_export($pkey, $out_key);
file_put_contents('key2.key', $out_key);

一个具体的例子:

$key = <<<EOS
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,775352C44A559B6C

V8EuwC29zy4yuY7Ie+HvyygjKJx4G+VF/SgjjCQR+Q/iLaXcoXhIMBmP9ugQpywu
Tgmg25PruaXl3Mabs2h03aUwLyFEEjcnaVz4IFYGflqDIBbSb/Y4Q9Ef0OjbCwCJ
5pEnD0ATPtb+bptHk7VitvyK9vIN4zrqDeWdpGkqhYZx4SkUDLBhcYYYA3eY8P7y
/yeUmHt2p12W7xF4OWflNj0ot7N2GoofKrAomW0vHVAAlVHj4OVyZYeOEG/8gm2A
a3xo+LS9D2tFJjCtnP5ytczWnsoe18bKlWbjV/IimlkVEqR6jx0jC99eCUHyaSvm
OfU/DHHcooBIJxXB5VfxFbRzjyWYgsAiVf2lThvusRb+j8/Ey28t5CWx8ME2hgmk
hrTPmCFor+Lx/7++cmOFWSNvJU8MrC6jH+q2R3xIPuY=
-----END RSA PRIVATE KEY-----
EOS;
$password = 'superman';

if (false === ($pkey = openssl_pkey_get_private($key, $password))) {
    die(openssl_error_string());
}
openssl_pkey_export($pkey, $out_key);
echo $out_key;
于 2012-12-16T13:23:35.087 回答
2

使用phpseclib,一个纯 PHP RSA 实现

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->setPassword('password');
$rsa->loadKey('...');

$rsa->setPassword();
echo $rsa->getPrivateKey();
?>
于 2012-12-16T15:39:53.497 回答