openssl 在 windows 命令行中使用,命令行中可以带空格,openssl中也可以带空格
从.pfx中提取私钥的三步关键点,第1步如果有密码,要输入密码:
openssl pkcs12 -in 1.pfx -nocerts -nodes -out 1.key
openssl rsa -in 1.key -out 1_pri.key
openssl pkcs8 -topk8 -inform PEM -in 1_pri.key -outform PEM -nocrypt -out 1_pri_pkcs8.key
char exeFullPath[MAX_PATH]; // Full path
GetModuleFileName(NULL,exeFullPath,MAX_PATH);
std::string strPath=(std::string)exeFullPath; // Get full path of the file
size_t pos = strPath.find_last_of('\\', strPath.length());
std::string path = strPath.substr(0, pos+1);
std::string path_cmd = path;
std::string path_ssl = path;
size_t pos_cmd = path_cmd.find_first_of(' ');
while (pos_cmd != std::string::npos){
size_t pos_a = path_cmd.find_first_of('\\', pos_cmd);
size_t pos_b = path_cmd.find_last_of('\\', pos_cmd);
if (pos_a!= std::string::npos) {
path_cmd.replace(pos_a, 1,"\"\\");
++pos_a;
}
if (pos_b!= std::string::npos) {
path_cmd.replace(pos_b, 1,"\\\"");
++pos_a;
}
pos_cmd = path_cmd.find_first_of(' ', pos_a);
}
size_t pos_ssl = path_ssl.find_first_of(' ');
while (pos_ssl != std::string::npos){
path_ssl.replace(pos_ssl, 1,"\" \"");
pos_ssl = path_ssl.find_first_of(' ', pos_ssl + 2);
}
// openssl pkcs12 -in 1.pfx -nocerts -nodes -out 1.key
std::string command_rd = path_cmd + "openssl.exe pkcs12 -in ";
CString strFile = _T("");
CString strFileName;
CFileDialog dlgFile(TRUE, NULL, path.c_str(), OFN_HIDEREADONLY, _T("(*.pfx;)|*.pfx;||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
strFileName = dlgFile.GetFileTitle();
}
if (!strFile.IsEmpty()){
std::string srcFile = CT2A(strFile.GetBuffer());
size_t pos_ssl = srcFile.find_first_of(' ');
while (pos_ssl != std::string::npos){
srcFile.replace(pos_ssl, 1,"\" \"");
pos_ssl = srcFile.find_first_of(' ', pos_ssl + 2);
}
command_rd += srcFile;
command_rd += " -nocerts -nodes -out ";
command_rd += path_ssl;
command_rd += "1.key";
TRACE(command_rd.c_str());
int ret = system(command_rd.c_str());
if (ret != 0){
MessageBox("提取密钥对失败!");
return;
}
// openssl rsa -in 1.key -out 1_pri.key
command_rd = path_cmd + "openssl.exe rsa -in ";
command_rd += path_ssl;
command_rd += "1.key -out ";
command_rd += path_ssl;
command_rd += "1_pri.key";
TRACE(command_rd.c_str());
ret = system(command_rd.c_str());
if (ret != 0){
MessageBox("提取私钥失败!");
return;
}
// openssl rsa -in 1.key -out 1_pri.key
command_rd = path_cmd + "openssl.exe rsa -in ";
command_rd += path_ssl;
command_rd += "1.key -out ";
command_rd += path_ssl;
command_rd += "1_pri.key";
TRACE(command_rd.c_str());
ret = system(command_rd.c_str());
if (ret != 0){
MessageBox("提取私钥失败!");
return;
}
// openssl pkcs8 -topk8 -inform PEM -in 1_pri.key -outform PEM -nocrypt -out 1_pri_pkcs8.key
command_rd = path_cmd + "openssl pkcs8 -topk8 -inform PEM -in ";
command_rd += path_ssl;
command_rd += "1_pri.key -outform PEM -nocrypt -out ";
command_rd += path_ssl;
command_rd += "1_pri_pkcs8.key";
TRACE(command_rd.c_str());
ret = system(command_rd.c_str());
if (ret != 0){
MessageBox("提取pkcs8模式私钥失败!");
return;
}
MessageBox("提取pkcs8模式私钥成功!");
}