使用字符数组公钥的OpenSSL进行RSA加密

时间:2018-09-13 20:04:50

标签: c++ encryption openssl public-key-encryption

我有一个像这样存储在变量中的公钥:

static const char publicKey[] =
"-----BEGIN PUBLIC KEY-----\n\
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCKFctVrhfF3m2Kes0FBL/JFeO\
cmNg9eJz8k/hQy1kadD+XFUpluRqa//Uxp2s9W2qE0EoUCu59ugcf/p7lGuL99Uo\
SGmQEynkBvZct+/M40L0E0rZ4BVgzLOJmIbXMp0J4PnPcb6VLZvxazGcmSfjauC7\
F3yWYqUbZd/HCBtawwIDAQAB\n\
-----END PUBLIC KEY-----";

我想使用PKCS#1 v1.5填充(RSA_PKCS1_PADDING)进行加密,但是我不知道如何从内存而不是文件中加载密钥:

void init()
{
    RSA* rsa = RSA_new();

    //what now?
    //rsa = PEM_read_RSA_PUBKEY(file, &rsa, NULL, NULL); //requires a file
}

void encrypt(unsigned char* data, int length)
{
    //can input buffer and output buffer be the same?
    RSA_public_encrypt(length, data, data, rsa, RSA_PKCS1_PADDING);
}

还需要调用任何清理代码吗?

0 个答案:

没有答案