0

我正在尝试通过 openSSL 和 VC++ 读取 PrivateKey,我收到此错误“ OpenSSL error: error:0906D064:lib(9):func(109):reason(100)”..

我的私钥:

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCnHVS+q65lvG2xocltTYgPGt9FaysGZTrcOwHedo8tX1dyPrcx
2I8x/cvB9nmfdAkt65aGFAlBZrofbPCr2Mq4wDdvIZ31KSuyMQI4T68ylWNT89Ge
wQF6AOkpY1E2bW+oDXc+MpbtpYXY1rUJAS/Abt5vXi7gwKN9FSJ3mm9bjQIDAQAB
AoGACXuxnmxRpjZOJ0FeE9TNfsXwm5jcRS2jbHHwxjYGI/YAGVyTusFmRtj3Iheh
iTnld3SiAxPJ/qscrsHY2nef8Up60V7RF8bs+sbICnHbdz8ZoKxN9dEFCwJkIl55
dv0GHjox6UgWUUkUAiHCM2EgG5eOOQ8PsuXY8LpPWLvfuyUCQQDbkFG9y/Q/3lmb
CdYfBpsP3qvma+fdgCw9lRXTowhu0rKen/CC3rFkHMeHfSW9GHuR8QbPYdVA6d9H
Y7KxYa7LAkEAwtjTEo/zAVexH/+YgiqL6w89BHlTAmwIEkXpqtAnE86kDDCekYdE
fRIdGK1zHDOUddMAhwoJQjJzy/NJreQ8BwJBAMoJ6U3vKZjD8Ex8Jq5yE6nsyt3D
mZ73XL5mO6l9sjrYY0kX/+dNKIro+KoyfNGef8bxtcSLUALlsnIsybf0HTUCQQCR
DD4cvGJHJpOp4WkTxT6Bjsd6lCKyU9+yUq8/RFNC0HqYxHzWkx7uCFT2sPBXFyK2
j4v9+v+ncs13DzZTJ+tzAkBI6UWHtsn839nrAT32M8PEPF/TuDmqebMhFOaTl4an
W0Jr8w8iGk2gvQS2cXEPNh4XT9AgcTKDQkhui4RgxK4F
-----END RSA PRIVATE KEY-----

我的代码:

#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/sha.h>

#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include <openssl/x509.h>


int main(int argc, char** argv)
{
    BIO *mem = NULL; 
    RSA *rsa_private = NULL;
    char *private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQCnHVS+q65lvG2xocltTYgPGt9FaysGZTrcOwHedo8tX1dyPrcx2I8x/cvB9nmfdAkt65aGFAlBZrofbPCr2Mq4wDdvIZ31KSuyMQI4T68ylWNT89GewQF6AOkpY1E2bW+oDXc+MpbtpYXY1rUJAS/Abt5vXi7gwKN9FSJ3mm9bjQIDAQABAoGACXuxnmxRpjZOJ0FeE9TNfsXwm5jcRS2jbHHwxjYGI/YAGVyTusFmRtj3IhehiTnld3SiAxPJ/qscrsHY2nef8Up60V7RF8bs+sbICnHbdz8ZoKxN9dEFCwJkIl55dv0GHjox6UgWUUkUAiHCM2EgG5eOOQ8PsuXY8LpPWLvfuyUCQQDbkFG9y/Q/3lmbCdYfBpsP3qvma+fdgCw9lRXTowhu0rKen/CC3rFkHMeHfSW9GHuR8QbPYdVA6d9HY7KxYa7LAkEAwtjTEo/zAVexH/+YgiqL6w89BHlTAmwIEkXpqtAnE86kDDCekYdEfRIdGK1zHDOUddMAhwoJQjJzy/NJreQ8BwJBAMoJ6U3vKZjD8Ex8Jq5yE6nsyt3DmZ73XL5mO6l9sjrYY0kX/+dNKIro+KoyfNGef8bxtcSLUALlsnIsybf0HTUCQQCRDD4cvGJHJpOp4WkTxT6Bjsd6lCKyU9+yUq8/RFNC0HqYxHzWkx7uCFT2sPBXFyK2j4v9+v+ncs13DzZTJ+tzAkBI6UWHtsn839nrAT32M8PEPF/TuDmqebMhFOaTl4anW0Jr8w8iGk2gvQS2cXEPNh4XT9AgcTKDQkhui4RgxK4F\n-----END RSA PRIVATE KEY-----";

    mem = BIO_new_mem_buf(private_key, strlen(private_key));
    if (mem == NULL) 
    { 
        char buffer[120]; 
        ERR_error_string(ERR_get_error(), buffer); 
        fprintf(stderr, "OpenSSL error: %s", buffer);
        exit(0);
    } 

    rsa_private = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL); 
    BIO_free (mem); 
    if (rsa_private == NULL) 
    { 
        char buffer[120]; 
        ERR_error_string(ERR_get_error(), buffer); 
        fprintf(stderr, "OpenSSL error: %s", buffer);
        exit(0);
    } 

    return(0);

}

我正在使用 WinXP-SP2 和 MS-Visual Studio 2008 .. 我在这一行出现错误:rsa_private = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL); 错误:(OpenSSL error: error:0906D064:lib(9):func(109):reason(100)

任何人都可以帮助我,我应该如何阅读这个私有密钥????

谢谢.....

4

2 回答 2

3

变量private_key初始化错误。您需要\n在私钥的每一行之后添加。

于 2013-08-27T12:03:12.143 回答
1

这个对我有用

unsigned char *rsaPrivateKeyChar = (unsigned char *)key.c_str();
// write char array to BIO
BIO *rsaPrivateBIO = BIO_new_mem_buf(rsaPrivateKeyChar, -1);
// create a RSA object from private key char array
RSA *rsaPrivateKey = NULL;
PEM_read_bio_RSAPrivateKey(rsaPrivateBIO, &rsaPrivateKey, NULL, NULL);
return rsaPrivateKey;
于 2019-05-07T16:24:26.637 回答