openssl从.pfx提取私钥的代码

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模式私钥成功!");

	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值