C++ 使用密码短语BOOST ASIO load key.pem

C++ 使用密码短语BOOST ASIO load key.pem,c++,ssl,openssl,boost-asio,pem,C++,Ssl,Openssl,Boost Asio,Pem,目前我正在处理以下问题: ... ctx.use_certificate_chain_file("./C/cert.pem"); ctx.use_private_key_file("./C/key.pem", boost::asio::ssl::context::pem); ctx.load_verify_file("./C/ca.pem"); ... 到目前为止,所有的工作都是完美的,但是我真正需要做的是加载相同的密钥。PEM,但是用密码,查看根据信息调用发现的ASI

目前我正在处理以下问题:

...
    ctx.use_certificate_chain_file("./C/cert.pem");
    ctx.use_private_key_file("./C/key.pem", boost::asio::ssl::context::pem);
    ctx.load_verify_file("./C/ca.pem");
...

到目前为止,所有的工作都是完美的,但是我真正需要做的是加载相同的密钥。PEM,但是用密码,查看根据信息调用发现的ASIO文档,并允许处理加密的PEM文件,请记住,我更熟悉Python之类的高级语言,所以C++不是我的强项


非常感谢您的帮助,谢谢

您应该熟悉python的回调

首先定义回调函数:

using namespace boost::asio;

// this function is called to obtain password info about an encrypted key
std::string my_password_callback(
    std::size_t max_length,  // the maximum length for a password
    ssl::context::password_purpose purpose ) // for_reading or for_writing
{
    std::string password; 
    // security warning: !! DO NOT hard-code the password here !!
    // read it from a SECURE location on your system
    return password;
}
然后使用
set\u password\u callback()
设置回调:

如果要使用类方法作为回调

class server {
    std::string password_callback(); //NOTE: no parameters
    // ...
};
您可以使用
boost::bind()
设置回调:

#include <boost/bind.hpp>

void server::startup() {
    ctx_.set_password_callback(
        boost::bind(&server::password_callback,this) );
    // ...
}
#包括
void服务器::启动(){
ctx设置密码回调(
boost::bind(&server::password_回调,this));
// ...
}
在任何一种情况下,都将抛出一个
boost::system::system_错误
异常(基于
std::exception
) 如果密钥无法解密,可能是因为密码错误或找不到文件

#include <boost/bind.hpp>

void server::startup() {
    ctx_.set_password_callback(
        boost::bind(&server::password_callback,this) );
    // ...
}