如何从包含 OpenSSL 中的公钥的字符数组中获取 RSA* 对象
How do I get the RSA* object from a char array that contains the public key in OpenSSL?
我试图加密和解密消息,同时将私钥和公钥存储在字符向量上。我已经尝试了d2i_PublicKey(...)并在EVP_set1_RSA(...)中使用EVP_PKEY对象。我也不知道 EVP_set1_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] = ' ';
pub_key[pub_len] = ' ';
// ---------
char msg[RSA_KEY_LENGTH/8] = "HOLA, ESPERO QUE ME ENCRIPTES";
char *encrypt = NULL; // Encrypted message
char *decrypt = NULL; // Decrypted message
printf("encrypting: %sn", 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: %sn", err);
}
return 0;
}
我在其他堆栈溢出帖子中找到了解决此问题的方法,即在这里:使用 OpenSSL 从内存中读取公钥/私钥
你要找的答案是他的最后一行代码@SquareRootOfTwentyThree
,将公钥提取到名为 pub 的 BIO 变量后:
PEM_write_bio_RSAPublicKey(pub, keypair);
创建一个 RSA 变量并将 pub 放入其中:
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: %sn", 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: %sn", err);
}
如果要通过网络传输"pub"变量,使用它来加密消息,然后将加密的数据发送回原始计算机以对其进行解密,这可能会有所帮助。
如果你真的想使用 char 变量,正如你在问题中所说,你当然可以使用 memcpy
将原始内存复制到 char 变量(来自 BIO 变量)中,但不要忘记在末尾添加"\0",在这里,这篇文章应该会有所帮助: 将公钥和私钥与 RSA 密钥对变量分开
相关文章:
- 如何在 nlohmann 的 json 库中获取数组长度?
- 从用户那里获取数组大小的值
- 如何在VS2019中获取数组的任意对齐?
- 我如何获取数组的大小,以便我可以从函数声明所述数组
- 根据字符获取数组的索引
- 尝试递归获取数组元素的总和
- 用于获取数组长度的指针数学
- Socket.io客户端C++,从Socket.on获取数组
- C++ nlohmann JSON 获取数组名称
- 练习循环和使用循环 1 次以及如何获取数组的最大值
- 如何在函数参数中通过引用获取数组
- 如何获取数组中字符串元素的字符数?[C++]
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 无法获取数组内数组的总和
- 尝试获取数组中的最小元素时崩溃
- 获取数组类型的无效错误
- 如何从用户获取数组输入
- 使用位集获取数组项的条件总和,而无需使用循环
- 如何以模式获取数组中的数据?
- 如何获取数组中每个数字的平方根 (C++)