如何使用WinCrypt和C ++以PEM格式导入私钥? [英] How to import private key in PEM format using WinCrypt and C++?

查看:400
本文介绍了如何使用WinCrypt和C ++以PEM格式导入私钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C ++中使用 WinCrypt API



我的应用程序需要对文件进行加密,解密,签名和验证,而且我知道在拥有正确的密钥后该怎么做。但是我的问题实际上是,生成这些密钥的应用程序不是同一应用程序。

  ----- BEGIN RSA私钥----- 
[Base64编码]
---- -结束RSA私钥-----

和:

  ----- BEGIN RSA公用密钥----- 
[Base64编码]
----- END RSA公用密钥-----

经过研究,我发现了如何导入公钥:此处此处,使用以下方法:




  • CreateFile & ReadFile 读取文件内容

  • CryptStringToBinary ,并使用 CRYPT_STRING_BASE64HEADER 从PEM格式转换为DER格式(删除页眉和页脚并从base64解码)

  • CryptDecodeObjectEx X509_PUBLIC_KEY_INFO

  • CryptImportPublicKeyInfo ,以导入密钥



但是现在,我的问题是要使用私钥
任何帮助都会非常感激:)
谢谢。

解决方案

PEM私钥可以通过使用 CryptDecodeObjectEx PKCS_RSA_PRIVATE_KEY 然后调用 CryptImportKey 导入CAPI。



我编写了一个示例,展示了如何使用PEM编码的RSA私钥来使用CAPI签名数据。这是它的链接: http://www.idrix.fr/Root/Samples/capi_pem.cpp



我希望这会有所帮助。


I'm trying to use the WinCrypt API in C++.

My application need to cipher, decipher, sign and verify files, and I know how to do that once I have the correct keys. But my problem is actually that that is NOT the same application which generates those keys.

What I have is public and private keys in files in PEM format :

-----BEGIN RSA PRIVATE KEY-----
[Base64 encoded]
-----END RSA PRIVATE KEY-----

And :

-----BEGIN RSA PUBLIC KEY-----
[Base64 encoded]
-----END RSA PUBLIC KEY-----

After some research, I have found how to import the public key : here and here, using the following methods :

  • CreateFile & ReadFile to read the file content
  • CryptStringToBinary, with CRYPT_STRING_BASE64HEADER to convert from PEM format to DER format (remove header and footer and decode from base64)
  • CryptDecodeObjectEx with X509_PUBLIC_KEY_INFO
  • CryptImportPublicKeyInfo, to import the key

But now, my problem is to do the same thing whith the private key. Any help would be really really appreciated :) Thank you.

解决方案

A PEM private key can be imported into CAPI by using CryptDecodeObjectEx with PKCS_RSA_PRIVATE_KEY and then calling CryptImportKey.

I have written a sample that shows how to use a PEM encoded RSA private key for signing data using CAPI. Here is a link to it : http://www.idrix.fr/Root/Samples/capi_pem.cpp

I hope this will help.

这篇关于如何使用WinCrypt和C ++以PEM格式导入私钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆