这是哪种私钥格式?

发布于 2025-01-06 11:26:07 字数 1456 浏览 0 评论 0原文

我有一个使用 OpenSSL 制作并使用 RSA 加密的私钥,如下所示:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQILJ8rNTk54VICAggA
MBQGCCqGSIb3DQMHBAiXuThuf12hMwSCBMhrm8H2j7yzLBOIvSWbLoyUmIn7e9zA
6PLDzj1dos+h+wAkPN0IgJlkdoBBXlnF7tzGdoctOYOKiu/p+HLzMnvHaDfNSFwz
5zx4yyZsKtf90gT6IVuJy7TMiLK3gk4gdZJc820OeRr1FjdRV3iVpTXbVfpwG/B3
IbV0PX3ZaHeR1m8vWI0/XQB7ZL7lLT+3MO66X/PAMTPA9joFMxuPFasIb2uXSqe/
CJNOyY4iW89uYHFiOe53e4ggUdT/rupoR4OSNKKEH5f1y+FIuH1xNvlmi14hJn+k
KOOi89vVIcvh4abCM+jhYwpPOGB8Sz2bAXsYocgZPvKtKakHlis7RcSsd5Usx9Nt
tgQ=
-----END ENCRYPTED PRIVATE KEY-----

我已将其存储到 user.key 文件中。

实际上我正在用 Objective-C 实现一个 CMS 加密和解密项目。现在我正在尝试使用证书、私钥等解密我的加密容器文件... 我有点困惑应该选择 OpenSSL 中的哪种方法将上述密钥加载到 EVP_PKEY 对象中。

它建议我使用以下格式:

  • FORMAT_ASN1
  • FORMAT_PEM
  • FORMAT_PKCS12
  • FORMAT_MSBLOB
  • FORMAT_PVK

我知道它不是 PEMPKCS12。我猜是 ASN1PVK 但我真的不知道,而且我没有在 OpenSSL 文档中找到任何关于此的进一步信息。

谁能告诉我上述密钥的格式是什么?

编辑: 为了更具体地说明我的问题:我将上述密钥加载到 BIO 中。我可以使用 BIO_read(keybio, ...) 成功读取此 BIO 并且它包含密钥(以 ascii 格式)。现在我想从此 BIO 获取 EVP_PKEY。 我试过了……

EVP_PKEY *key = PEM_read_bio_PrivateKey(keybio, NULL, 0, NULL);

但这行不通。当我调用它时,它要求我在控制台中输入密码,当我这样做时,密钥是NULL。 :-/

I've a private key made with OpenSSL and encrypted with RSA like this:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQILJ8rNTk54VICAggA
MBQGCCqGSIb3DQMHBAiXuThuf12hMwSCBMhrm8H2j7yzLBOIvSWbLoyUmIn7e9zA
6PLDzj1dos+h+wAkPN0IgJlkdoBBXlnF7tzGdoctOYOKiu/p+HLzMnvHaDfNSFwz
5zx4yyZsKtf90gT6IVuJy7TMiLK3gk4gdZJc820OeRr1FjdRV3iVpTXbVfpwG/B3
IbV0PX3ZaHeR1m8vWI0/XQB7ZL7lLT+3MO66X/PAMTPA9joFMxuPFasIb2uXSqe/
CJNOyY4iW89uYHFiOe53e4ggUdT/rupoR4OSNKKEH5f1y+FIuH1xNvlmi14hJn+k
KOOi89vVIcvh4abCM+jhYwpPOGB8Sz2bAXsYocgZPvKtKakHlis7RcSsd5Usx9Nt
tgQ=
-----END ENCRYPTED PRIVATE KEY-----

And I've stored it into a user.key file.

Actually I'm implementing a CMS encrypt and decrypt project in objective-c. Now I'm trying to decrypt my encrypted container file using certificates, private key, and so on...
I'm a little bit confused which method from OpenSSL I should choose to load the aforementioned key into an EVP_PKEY object.

It suggests me the following formats:

  • FORMAT_ASN1
  • FORMAT_PEM
  • FORMAT_PKCS12
  • FORMAT_MSBLOB
  • FORMAT_PVK

I know that it's not PEM and PKCS12. I would guess ASN1 or PVK but I really don't know and I haven't found any further information on this in the OpenSSL docu.

Can anyone tell me please which format the aforementioned key has?

EDIT:
To specify my question a bit more: I have the aforementioned key loaded into a BIO. I can read this BIO with BIO_read(keybio, ...) successfully and it contains the key (in ascii). Now I want to get an EVP_PKEY from this BIO.
I've tried...

EVP_PKEY *key = PEM_read_bio_PrivateKey(keybio, NULL, 0, NULL);

... but this doesn't work. When I call this it asks me to enter the passphrase in the console and when I do this, the key is NULL. :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧故 2025-01-13 11:26:07

密钥是封装在 PEM 中的 PKCS#8 EncryptedPrivateKeyInfo-容器。

您可以执行以下操作将其放入 EVP_PKEY 对象中:

// This must be called once: 
OpenSSL_add_all_algorithms();

BIO* bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);

X509_SIG* p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
if(p8==NULL){
  BIO_printf(bio_err, "Error decoding key\n");
  ERR_print_errors(bio_err);
  return;
}
PKCS8_PRIV_KEY_INFO* p8inf = PKCS8_decrypt(p8, password, strlen(password));
if(p8inf==NULL){
  BIO_printf(bio_err, "Error decrypting key\n");
  ERR_print_errors(bio_err);
  return;
}
EVP_PKEY* pkey = EVP_PKCS82PKEY(p8inf);
if(pkey==NULL){
  BIO_printf(bio_err, "Error converting key\n");
  ERR_print_errors(bio_err);
  return;
}

The key is a PKCS#8 EncryptedPrivateKeyInfo wrapped in a PEM-container.

You can do something like this to get it into an EVP_PKEY-object:

// This must be called once: 
OpenSSL_add_all_algorithms();

BIO* bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);

X509_SIG* p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
if(p8==NULL){
  BIO_printf(bio_err, "Error decoding key\n");
  ERR_print_errors(bio_err);
  return;
}
PKCS8_PRIV_KEY_INFO* p8inf = PKCS8_decrypt(p8, password, strlen(password));
if(p8inf==NULL){
  BIO_printf(bio_err, "Error decrypting key\n");
  ERR_print_errors(bio_err);
  return;
}
EVP_PKEY* pkey = EVP_PKCS82PKEY(p8inf);
if(pkey==NULL){
  BIO_printf(bio_err, "Error converting key\n");
  ERR_print_errors(bio_err);
  return;
}
朕就是辣么酷 2025-01-13 11:26:07

OpenSSL 使用该(扩展)PEM 格式来存储 PKCS#8 编码的私钥。

That (extended) PEM format is used by OpenSSL for PKCS#8 encoded private keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文