简体   繁体   中英

PHP equivalent to OpenSSL RSA command

How could I remove passphase from RSA private key using PHP I know that in OpenSSL it is this way: openssl rsa -in key.key -out key.key and I am searching equivalent command to this one in PHP.


RSA command requires the pass

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

This would accomplish the same operation using the openssl extension:

$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);

A concrete example:

$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;

Using phpseclib, a pure PHP RSA implementation :

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

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

$rsa->setPassword();
echo $rsa->getPrivateKey();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM