1

I have a privatekey.pem file which is protected by a password, how can I open it with the password in c++ and convert it to std::string? I have written this function:

auto read_file(std::string private_key_path)-> std::string
{
std::string keypass= "pass";
char * keypass_byte= const_cast<char*>( keypass.c_str() );

//open the private key file
FILE *fp = fopen(private_key_path.c_str(), "r");
EVP_PKEY *pkey = PEM_read_PrivateKey(fp, NULL, NULL, 
 keypass_byte);

How To convert it to string????
return private_key;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
sama
  • 333
  • 2
  • 11
  • 2
    You'll need a library that understands the file format. You are probably using one anyway to actually use the decrypted key (what are you going to use it for?). Library suggestions are off-topic here. – user17732522 Nov 29 '22 at 11:09
  • 1
    Related: [https://stackoverflow.com/questions/17400058/how-to-use-openssl-lib-pem-read-to-read-public-private-key-from-a-string](https://stackoverflow.com/questions/17400058/how-to-use-openssl-lib-pem-read-to-read-public-private-key-from-a-string) – drescherjm Nov 29 '22 at 13:33
  • @drescherjm I have changed the function, and I do not know how can I convert RSA* or EVP_PKEY to string. Do you Know how can I do this? – sama Nov 30 '22 at 11:53

0 Answers0