如何从包含OpenSSL中的公钥的char数组中获取RSA *对象? [英] How do I get the RSA* object from a char array that contains the public key in OpenSSL?

查看:163
本文介绍了如何从包含OpenSSL中的公钥的char数组中获取RSA *对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加密和解密消息,同时存储私钥和公​​钥的字符向量。我试过d2i_PublicKey(...)和使用EVP_PKEY对象在EVP_set1_RSA(...)。我也不知道EVP_set1_RSA(...)中的所有参数是什么。请帮忙。这是我的代码:

Im trying to encrypt and decrypt messages while storing the private and public keys on char vectors. I have tried d2i_PublicKey(...) and using EVP_PKEY objects in EVP_set1_RSA(...). I also do not know what are all the parameters in EVP_set1_RSA(...). Please help. Here is my code:

#include <stdio.h>

//RSA
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/x509.h>

#define RSA_KEY_LENGTH 2048
#define PUB_EXP     3
#define PRINT_KEYS

//RSA


int main()
{
    printf("\ngenerating keys...\n");
    RSA *keypair = RSA_generate_key(RSA_KEY_LENGTH, PUB_EXP, NULL, NULL);


    // ---------

    printf("Converting Keys to char array..\n");

    char   *pri_key = NULL;           // Private key
    char   *pub_key = NULL;           // Public key
    size_t pri_len;            // Length of private key
    size_t pub_len;            // Length of public key

    BIO *pri = BIO_new(BIO_s_mem());
    BIO *pub = BIO_new(BIO_s_mem());

    PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
    PEM_write_bio_RSAPublicKey(pub, keypair);

    pri_len = BIO_pending(pri);
    pub_len = BIO_pending(pub);

    pri_key = (char*)malloc(pri_len + 1);
    pub_key = (char*)malloc(pub_len + 1);

    BIO_read(pri, pri_key, pri_len);
    BIO_read(pub, pub_key, pub_len);

    pri_key[pri_len] = '\0';
    pub_key[pub_len] = '\0';

    // ---------



    char msg[RSA_KEY_LENGTH/8] = "HOLA, ESPERO QUE ME ENCRIPTES";
    char   *encrypt = NULL;    // Encrypted message
    char   *decrypt = NULL;    // Decrypted message

    printf("encrypting: %s\n", msg);



/*
* Here I want to obtain an RSA *PublicKey to use it for the encryption 
*/


    int encrypt_len;
    err = (char*)malloc(130);
    printf("++++\n");
    if((encrypt_len = RSA_public_encrypt(strlen(msg), (unsigned char*)msg, (unsigned char*)encrypt, PublicKey, RSA_PKCS1_OAEP_PADDING)) == -1) {
        printf("err++++\n");
        ERR_load_crypto_strings();
        ERR_error_string(ERR_get_error(), err);
        fprintf(stderr, "Error encrypting message: %s\n", err);

    }

    return 0;
}


推荐答案

在这个问题,其他Stack-Overflow帖子,即在这里:阅读公共/私人密钥从内存与OpenSSL

I've found a solution to this issue among other Stack-Overflow posts and namely here :Reading Public/Private Key from Memory with OpenSSL

您寻找的答案是回答@SquareRootOfTwentyThree是他的最后一行代码,

The answer you waere looking for is answered by @SquareRootOfTwentyThree is his last line of code,

将公钥解压到名为pub的BIO变量中:

After extracting the Public key into a BIO variable called pub:

PEM_write_bio_RSAPublicKey(pub,keypair) code>

PEM_write_bio_RSAPublicKey(pub, keypair);

创建RSA变量并将pub置于其中:

create a RSA variable and put pub inside it:

RSA *keypair2 = NULL; 
PEM_read_bio_RSAPublicKey( pub, &keypair2, NULL, NULL);

完成后,您可以照常使用keypair2成功加密邮件:

After you've done this you can successfully encrypt the message as usual, using keypair2:

加密:

encrypt = (char*)malloc(RSA_size(keypair));
int encrypt_len;
err = (char*)malloc(130);
if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,    keypair2   ,RSA_PKCS1_OAEP_PADDING)) == -1) {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error encrypting message: %s\n", err);
}

您可以照常使用原始密钥对解密,在您的第一次加密时使用

you can then decrypt it as usual, using the original keypair, without having to use it at your first encryption

解密:

decrypt = (char*)malloc(encrypt_len);
if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, keypair, RSA_PKCS1_OAEP_PADDING) == -1) {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error decrypting message: %s\n", err);
}

如果您想通过网络传输pub ,使用它来加密消息,然后将加密的数据发送回原始机器,以解密。

This might help if you want to transfer the "pub" variable over a network, use it to encrypt a message, then send the encrypted data back to the original machine to get it decrypted.

如果你真的想使用char变量,在你的问题中,你当然可以使用 memcpy 将内存作为原始数据复制到一个char变量(从BIO中),但不要忘记添加\ 0结束,这里,这篇文章应该有助于:分离公钥和私钥来自RSA密钥对变量

If you really want to use char variables, as you said in your question, you can of course copy the memory as raw to a char variable (from the BIO one) using memcpy, but don't forget to add the "\0" at the end, here, this post should help: Separating public and private keys from RSA keypair variable

这篇关于如何从包含OpenSSL中的公钥的char数组中获取RSA *对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆