- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要将 PEM 编码的 X.509 证书加载到 Windows Crypto API 上下文中以用于 C++。他们是那些有 -----BEGIN RSA XXX KEY-----
和 -----END RSA XXX KEY -----
。我找到了 Python 和 .NET 的示例,但它们使用了与普通 Windows Crypto API 无关的特定功能。
我了解如何在获得 HCRYPTKEY 后进行加密/解密。但是,我只是不知道如何在 .PEM 文件中导入 Base64 blob 并获得一个我可以使用的 HCRYPTKEY
。
我有一种奇怪的感觉,它不仅仅是调用 CryptDecodeObject()
。
任何可以让我走上正轨的指针?我已经浪费了两天时间进行“试错”编程,但一无所获。
最佳答案
KJKHyperion在他的answer中说:
I discovered the "magic" sequence of calls to import a RSA public key in PEM format. Here you go:
- decode the key into a binary blob with CryptStringToBinary; pass CRYPT_STRING_BASE64HEADER in dwFlags
- decode the binary key blob into a CERT_PUBLIC_KEY_INFO with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and X509_PUBLIC_KEY_INFO in lpszStructType
- decode the PublicKey blob from the CERT_PUBLIC_KEY_INFO into a RSA key blob with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and RSA_CSP_PUBLICKEYBLOB in lpszStructType
- import the RSA key blob with CryptImportKey
这个序列确实帮助我理解了正在发生的事情,但它并没有按原样工作。第二次调用 CryptDecodeObjectEx
给了我一个错误:“符合 ASN.1 错误标签值”。经过多次尝试理解微软文档,我终于意识到第一次解码的输出不能再解码为ASN,实际上已经准备好导入了。有了这种理解,我在以下链接中找到了答案:
http://www.ms-news.net/f2748/problem-importing-public-key-4052577.html
以下是我自己的程序,它将公钥从 .pem 文件导入到 CryptApi 上下文:
int main()
{
char pemPubKey[2048];
int readLen;
char derPubKey[2048];
size_t derPubKeyLen = 2048;
CERT_PUBLIC_KEY_INFO *publicKeyInfo;
int publicKeyInfoLen;
HANDLE hFile;
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
/*
* Read the public key cert from the file
*/
hFile = CreateFileA( "c:\\pub.pem", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
fprintf( stderr, "Failed to open file. error: %d\n", GetLastError() );
}
if ( !ReadFile( hFile, pemPubKey, 2048, &readLen, NULL ) )
{
fprintf( stderr, "Failed to read file. error: %d\n", GetLastError() );
}
/*
* Convert from PEM format to DER format - removes header and footer and decodes from base64
*/
if ( !CryptStringToBinaryA( pemPubKey, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen, NULL, NULL ) )
{
fprintf( stderr, "CryptStringToBinary failed. Err: %d\n", GetLastError() );
}
/*
* Decode from DER format to CERT_PUBLIC_KEY_INFO
*/
if ( !CryptDecodeObjectEx( X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, derPubKey, derPubKeyLen,
CRYPT_ENCODE_ALLOC_FLAG, NULL, &publicKeyInfo, &publicKeyInfoLen ) )
{
fprintf( stderr, "CryptDecodeObjectEx 1 failed. Err: %p\n", GetLastError() );
return -1;
}
/*
* Acquire context
*/
if( !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) )
{
{
printf( "CryptAcquireContext failed - err=0x%x.\n", GetLastError() );
return -1;
}
}
/*
* Import the public key using the context
*/
if ( !CryptImportPublicKeyInfo( hProv, X509_ASN_ENCODING, publicKeyInfo, &hKey ) )
{
fprintf( stderr, "CryptImportPublicKeyInfo failed. error: %d\n", GetLastError() );
return -1;
}
LocalFree( publicKeyInfo );
/*
* Now use hKey to encrypt whatever you need.
*/
return 0;
}
关于c++ - 将 PEM 编码的 X.509 证书加载到 Windows CryptoAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1231178/
如何从一个 PEM 中确定 SSL 证书到期日期,该 PEM 在一个文件 (pem) 上串联了很多证书? 例子: # cat cert.pem -----BEGIN CERTIFICATE-----
我想让运行测试更容易。为了实现这一点,我想提供项目内部测试中使用的 secret 。 我使用以下命令创建了“fullchain.pem”、“privkey.pem”和“dhparams.pem”: o
注意图片中证书的图标图像上写有标准或个人。有什么不同?最近一次我在处理它,我只制作了带有“个人”的图标。我不知道为什么。有什么不同?两者是如何产生的? 最佳答案 我会这样说,因为您的 .p12 文件包
我正在尝试安装这个项目:https://github.com/versatica/mediasoup-demo 它需要 fullchain.pem和 privkey.pem文件。 如何在 Ubuntu
我正在使用 curl 使用公共(public)证书文件从 https 站点下载数据。 系统信息: 操作系统:软呢帽 14 curl : curl 7.30.0 openssl: OpenSSL 1.0
我正在尝试使用 openssl 加密短文本,但我不知道如何继续。更具体地说:我发现一段代码似乎可以工作,但必须从终端执行指令。是的,我知道,存在 system() 函数,但我想避免它,因为它需要文件夹
我正在使用 OpenSSL 将“me.p12”转换为 PEM。当我生成“me.p12”时,我为其设置了密码。 “me.p12”包含私钥和证书。 当我将其转换为 PEM 时,我运行命令: openssl
我正在使用 Amazon 的 EC2 集群来运行 GraphLab。他们想要我的 .pem 文件的位置,这是我的私钥。 搜索后我仍然在ubuntu中找不到该文件。我正在使用 PuTTY。 最佳答案 .
我正在尝试了解 .pem 文件的布局方式。密码短语是如何验证的? .pem 文件如何检查您尝试打开它时使用的密码? 最佳答案 多种不同类型的数据存储在扩展名为.pem 的文件中。所以你的问题不准确。但
我需要生成一个 cert.pem 和 key.pem 文件来使用 Nginx 加密 http 请求。 在 Linux 上,我会执行以下 OpenSSL 命令: openssl req -x509 -n
我必须以 pem 格式提交 CSR。 我已经使用 OpenSSL 生成了 CSR,但在将其转换为 PEM 格式时遇到了困难。 我必须从 example.csr 获取示例 - csr.pem。如何实现?
我知道这是一个与许多其他问题非常相似的问题,但他们都没有给出直接的答案或对我有用的答案...... 我从 Let's encrypt 获得了两个文件: 证书.pem key.pem 我需要让他们进入
首先,我看过this thread但它不相关并且有不同的问题。 我的 Kafka 属性文件中有以下设置片段: ssl.keystore.type=PEM ssl.keystore.key=/path/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我想下载 Cpanel 自动生成的 SSL 作为 key.pem 和 cert.pem,我在 Cpanel 中找不到任何下载链接,有什么建议吗? 我曾尝试将证书文本复制到文本文件中并使用它,但没有成功
这个问题在这里已经有了答案: Converting pfx to pem using openssl (5 个答案) 关闭 6 年前。
我想通过 AWS Lambda 使用 pysftp 库连接 EC2。我使用以下代码进行连接。 mysftp = pysftp.Connection( host=Constants.MY_HOS
我有这两个文件(certificate.pem 和private_key.pem),它们由我使用的 API 提供。我需要使用这些文件在 Swift 项目中签署我的 http 请求。 我可以用 Post
我的 Pem 文件就是这种格式。 Bag Attributes localKeyID: friendlyName: test subject=/C=GB/ST=London/L=Soho/O=Righ
我从证书颁发机构获得了这些文件: domain.com.p7b domain.com.crt domain.com.ca-bundle 我尝试了这个小代码: var express = require
我是一名优秀的程序员,十分优秀!