码迷,mamicode.com
首页 > 其他好文 > 详细

openssl RSA 内存读取密钥

时间:2016-10-08 16:32:15      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

主要注意一下密钥的格式

#include <openssl/pem.h>
#include <openssl/err.h>


bool CEncipher::CreatePubKey()
{
    BIO* bp = NULL;
    string strPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmm9GRVbi5+myHYztzOyWvMdo1\
                          4b0fwRF3va9A8bd19oW9ZUCpALVIC4d4I1Zvkfcgnpvswf/DOHD9umT+pevmwICF                          /mU8LNe/MT8kGh0IAIHdJVrXw/2TEWCVEgFlFfloWzCNmzOcx4BH8yIw50RjG1ED                          5Fl5QKFrirgcU11IhQIDAQAB";
    int nPublicKeyLen = strPublicKey.size();
    for(int i = 64; i < nPublicKeyLen; i+=64)
    {
        if(strPublicKey[i] != \n)
        {
            strPublicKey.insert(i, "\n");
        }
        i++;
    }
    strPublicKey.insert(0, "-----BEGIN PUBLIC KEY-----\n");
    strPublicKey.append("\n-----END PUBLIC KEY-----\n");

    char *chPublicKey = const_cast<char *>(strPublicKey.c_str());
    if ((bp = BIO_new_mem_buf(chPublicKey, -1)) == NULL)
    {     
        printf("BIO_new_mem_buf failed!\n");
        return false;
    }

    pPubKey_ = PEM_read_bio_RSA_PUBKEY(bp, NULL, NULL, NULL);

    if (NULL == pPubKey_)
    {
        ERR_load_crypto_strings();
        char errBuf[512];
        ERR_error_string_n(ERR_get_error(), errBuf, sizeof(errBuf));
        printf("load public key failed[%s]\n", errBuf);
        return false;
    }
    BIO_free_all(bp);

    return true;
}

 

openssl RSA 内存读取密钥

标签:

原文地址:http://www.cnblogs.com/john-h/p/5939019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!