我有一个与服务器通信的客户端,并且使用 RSA 和 AES 组合对通信进行加密。我生成的 RSA 密钥对没有密码。因此,为了确保安全,我最近添加了密码。每件事都在服务器端运行良好,但 PHP 生成的私钥不包含加密信息,因此我无法在客户端加载它。以下是php代码:
$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 );
var_dump($pkey_pair);
您可以在http://phpfiddle.org/上尝试它没有加密信息,就好像它假设那样,因为它在服务器端工作!
我的客户端代码仅在 RSA 密钥生成中未使用密码时才有效。
try
{
Botan::AutoSeeded_RNG rng;
Botan::DataSource_Memory privKeyMem(privKey);
Botan::RSA_PrivateKey *rsaKey = dynamic_cast <Botan::RSA_PrivateKey*>
(Botan::PKCS8::load_key(privKeyMem, rng, passphrase.c_str()));
if(!rsaKey)
{
std::cout << "The loaded key is not a RSA key!\n";
return false;
}
....
.....
}
catch(...)
{
cout<<"Exception: could not load private key";
return false;
}