I have a client which talks to a server and communication is encrypted using RSA and AES combination. The RSA key pair which I had generated was without passphrase. So, to make it secure I recently added the passphrase. Every thing works fine server side, but the Private key generated by PHP doesn't contain encryption info, due to which I can't load it client side. Following is the php code:
$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);
You can try it on http://phpfiddle.org/ there is no enryption info, as if it is assuming that, since it works server side!
My client side code works only when no passphrase is used in RSA key generation.
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;
}