2013-11-15 54 views
3

我試圖加密和解密消息,同時將私鑰和公鑰存儲在char向量上。我已經嘗試了d2i_PublicKey(...)並在EVP_set1_RSA(...)中使用EVP_PKEY對象。我也不知道EVP_set1_RSA(...)中的所有參數是什麼。請幫忙。這裏是我的代碼:如何從包含OpenSSL中的公鑰的char數組中獲取RSA *對象?

#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; 
} 
+0

[這應該對你有幫助](https://shanetully.com/2012/04/simple-public-key-encryption-with-rsa- and-openssl /) –

+0

@GabrielL。在該頁面中,他們使用他們已經創建的對象RSA *密鑰對來加密和解密消息。我想要的只是使用公鑰對消息進行加密,並僅使用私鑰對其進行解密。 – ghyur7

回答

1

我已經找到了解決其他棧溢出職位,即在此之間的這個問題:Reading Public/Private Key from Memory with OpenSSL

你waere尋找被@SquareRootOfTwentyThree回答的答案是他最後一次

PEM_write_bio_RSAPublicKey(pub, keypair);

:行代碼,

提取公鑰到名爲酒館BIO變量之後3210

裏面創建它RSA變量,把酒吧:

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

你這樣做之後,你可以成功地加密的消息像往常一樣,用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); 
} 

您可以像往常一樣使用原始密鑰對解密,而無需在第一次加密時使用它

解密:

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」變量,使用它來加密消息,然後將加密數據發送回原始機器以解密,這可能會有所幫助。

如果你真的想要使用char變量,就像你在你的問題中說的那樣,你當然可以使用memcpy作爲raw將內存複製到char變量(從BIO之一),但是不要忘記添加「\ 0」在最後,在這裏,這個帖子應該有所幫助:Separating public and private keys from RSA keypair variable

相關問題