为什么我无法使用 PEM_read_RSAPublicKey 读取 openssl 生成的 RSA 公钥? [英] Why I can't read openssl generated RSA pub key with PEM_read_RSAPublicKey?
问题描述
我正在尝试像这样读取用 openssl 生成的 RSA 公钥:
I'm trying to read a RSA public key generated with openssl like this:
Private Key:
openssl genrsa -out mykey.pem 1024
Public Key afterwards:
openssl rsa -in mykey.pem -pubout > somewhere.pub
然后我尝试阅读:
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
有什么想法吗?
我读过 openssl 生成 RSA 公钥的 X509 密钥.但我什至无法加载 X509 公钥.
I've read that openssl generate a X509 key of the RSA public key. But I could not manage to load even a X509 pub key.
谢谢
推荐答案
您可以尝试 PEM_read_RSA_PUBKEY()
而不是 PEM_read_RSAPublicKey()
.
You might try PEM_read_RSA_PUBKEY()
instead of PEM_read_RSAPublicKey()
.
这都是关于格式的.
openssl 生成的默认公钥文件格式是 PEM 格式.
The default public key file format generated by openssl is the PEM format.
PEM_read_RSA_PUBKEY()
读取 PEM 格式.PEM_read_RSAPublicKey()
读取 PKCS#1 格式.
PEM_read_RSA_PUBKEY()
reads the PEM format. PEM_read_RSAPublicKey()
reads the PKCS#1 format.
因此,如果您想坚持使用 PEM_read_RSAPublicKey()
,您可以通过在生成公钥.
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 读取 openssl 生成的 RSA 公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!