简体   繁体   English

为什么我无法使用 PEM_read_RSAPublicKey 读取 openssl 生成的 RSA 公钥?

[英]Why I can't read openssl generated RSA pub key with PEM_read_RSAPublicKey?

I'm trying to read a RSA public key generated with openssl like this:我正在尝试读取使用 openssl 生成的 RSA 公钥,如下所示:

Private Key:
    openssl genrsa -out mykey.pem 1024

Public Key afterwards:
    openssl rsa -in mykey.pem -pubout > somewhere.pub

Then I try to read:然后我尝试阅读:

FILE *keyfile = fopen("somewhere.pub", "r");
RSA *rsa_pub = PEM_read_RSAPublicKey(keyfile, NULL, NULL, NULL);
//rsa_pub == NULL!

When I'm reading the private key it works当我阅读私钥时,它会起作用

FILE *keyfile = fopen("mykey.pem", "r");
RSA *rsa_pri = PEM_read_RSAPrivateKey(keyfile, NULL, NULL, NULL);
//all good

Any ideas?有任何想法吗?

I've read that openssl generate a X509 key of the RSA public key.我读过 openssl 生成 RSA 公钥的 X509 密钥。 But I could not manage to load even a X509 pub key.但是我什至无法加载 X509 公钥。

Thanks谢谢

You might try PEM_read_RSA_PUBKEY() instead of PEM_read_RSAPublicKey() .您可以尝试PEM_read_RSA_PUBKEY()而不是PEM_read_RSAPublicKey()

This is all about formats.这都是关于格式的。

The default public key file format generated by openssl is the PEM format. openssl 生成的默认公钥文件格式是 PEM 格式。

PEM_read_RSA_PUBKEY() reads the PEM format. PEM_read_RSA_PUBKEY()读取 PEM 格式。 PEM_read_RSAPublicKey() reads the PKCS#1 format. PEM_read_RSAPublicKey()读取 PKCS#1 格式。

So if you want to stick to PEM_read_RSAPublicKey() you could generate the public key file using the PKCS#1 format by specifying the -outform DER option when generating the public key.因此,如果您想坚持使用PEM_read_RSAPublicKey()您可以通过在生成公钥时指定-outform DER选项,使用 PKCS#1 格式生成公钥文件。

it seems there are two format of rsa public key, with different encoding.似乎有两种格式的 rsa 公钥,具有不同的编码。

A. RSA_PUBKEY A. RSA_PUBKEY

RSA* rsaPubKey = PEM_read_bio_RSA_PUBKEY( bio, NULL, 0, pass ) ;

read PUBKEY with this format用这种格式读取PUBKEY

-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

generated by产生于

$ openssl rsa -in key.pri -pubout -out key.pub1

B. RSAPublicKey B. RSAP 公钥

RSA* rsaPubKey = PEM_read_bio_RSAPublicKey( bio, NULL, 0, pass ) ;

read PublicKey with this format用这种格式读取PublicKey

-----BEGIN RSA PUBLIC KEY-----
...
-----END RSA PUBLIC KEY-----

generated by产生于

$ openssl rsa -in key.pri -RSAPublicKey_out -out key.pub2

convert兑换

A to B format A到B格式

$ openssl rsa -in key.pub1 -pubin -RSAPublicKey_out -out key.pub2_

B to A format B转A格式

$ openssl rsa -in key.pub2 -RSAPublicKey_in -pubout -out key.pub1_

The openssl rsa utility saves the public key using the function PEM_write_bio_RSA_PUBKEY and not PEM_write_bio_RSAPubicKey. openssl rsa 实用程序使用函数 PEM_write_bio_RSA_PUBKEY 而不是 PEM_write_bio_RSAPubicKey 保存公钥。 So, if you want your program to be compatible with its output, then you should use PEM_write_bio_RSA_PUBKEY and PEM_read_bio_RSA_PUBKEY for saving/loading public key files.因此,如果您希望您的程序与其输出兼容,那么您应该使用 PEM_write_bio_RSA_PUBKEY 和 PEM_read_bio_RSA_PUBKEY 来保存/加载公钥文件。

http://openssl.6102.n7.nabble.com/RSA-public-private-keys-only-work-when-created-programatically-td12532.html http://openssl.6102.n7.nabble.com/RSA-public-private-keys-only-work-when-created-programatically-td12532.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM