从读.pem文件RSA密钥RSA的结构用C [英] Reading RSA keys from .PEM file to RSA structure in C
问题描述
JKJS
观察:
假设RSA私钥由以下命令创建:
Suppose RSA private key is created by following commands:
openssl genrsa -out mykey.pem 1024
然后,是没有问题的用C从该文件读取键:
Then, there is no problem reading key from that file in C:
RSA *privatekey=NULL;
privatekey=PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL);
if(privatekey==NULL)
ERR_print_errors_fp(stderr);
但是,如果RSA密钥是通过以下命令创建:
But, if RSA keypair is created by following command:
openssl req -newkey rsa:1024 -sha1 -keyout mykey.pem -out rootreq.pem
然后从错误mykey.pem结果读数。
为什么呢?
Then reading from mykey.pem results in error. Why?
JKJS
Hardik
JKJS Hardik
推荐答案
在第二,命令,你给一个密码?打开通过在任何编辑器中的第二个命令创建的密钥? - 你在haders看到ENCRYPTED
In the 2nd, command, did you give a passphrase? Open the key created by the 2nd command in any editor - do you see ENCRYPTED in the haders?
如果是的话,这就是为什么你不能使用 PEM_read_RSAPrivateKey
,因为你逝去的第三个参数为NULL阅读。
If yes, then that's why you aren't able to read it using PEM_read_RSAPrivateKey
because you are passing 3rd param as NULL.
传递一个回调来获得在调用密码 PEM_read_RSAPrivateKey
。
Pass a callback to get a password in the call to PEM_read_RSAPrivateKey
.
另外,你可以用下面的命令从键删除密码
Alternately you can remove the password from the key by using the following command
openssl rsa -in mykey.pem -out mykey1.pem.
这会问你一个密码 - 如果你给正确的密码,你会得到一个未加密的形式钥匙 mykey1.pem
This will ask you for a passphrase - if you give the right passphrase, you will get the key in an unencrypted form in mykey1.pem
这篇关于从读.pem文件RSA密钥RSA的结构用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!