OpenSSL PHP代码不会生成PKCS#8格式的加密私钥
$passphrase = 'Hello World';
$config = array ("digest_alg" => "sha256","private_key_bits" => 2048,"private_key_type" => OPENSSL_KEYTYPE_RSA );
// Create the private and public key
$res = openssl_pkey_new ( $config );
/* Extract the private key from $res to $privKey */
openssl_pkey_export ( $res, $priv_key, $passphrase );
/* Extract the public key from $res to $pubKey */
$pub_key = openssl_pkey_get_details ( $res );
$pub_key = $pub_key["key"];
$pkey_pair = array ('priv_key' => $priv_key,'pub_key' => $pub_key );
/* Print it */
var_dump($pkey_pair);但是PhpSeclib可以:
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS8);
extract($rsa->createKey());
echo $privatekey;现在,是否可以使用phpseclib将openssl_pkey_new()生成的私钥转换为PKCS#8。
我没有使用phpseclib进行签名和加密,因为与php openssl函数相比,它的速度要慢得多。我的客户希望加密的私钥是PKCS#8格式的。
发布于 2015-02-26 23:39:18
如果OpenSSL可用,phpseclib应该使用它(并且库和头文件版本不会不匹配;您应该使用最新版本0.3.10,因为匹配要求略有放宽)。
也就是说,您可以这样转换RSA密钥:
<?php
include('Crypt/RSA.php');
$rsa = new Crypt_RSA();
//$rsa->setPassword('password');
$rsa->loadKey('...');
//$rsa->setPassword(); // clear the password if there was one
$privatekey = $rsa->getPrivateKey();
$publickey = $rsa->getPublicKey();
?>默认情况下,getPrivateKey()返回一个PKCS1格式的私钥。您可以通过执行$rsa->getPrivateKey(CRYPT_RSA_PRIVATE_FORMAT_PUTTY);使其以其他格式返回键
https://stackoverflow.com/questions/28728596
复制相似问题