我试图在将私钥和公钥存储在字符向量上的同时加密和解密消息。我已经尝试过 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 …Run Code Online (Sandbox Code Playgroud) 目前我的私钥保存在一个文件private.key中,我使用以下函数加载它:
RSA*r = PEM_read_RSAPrivateKey("private.key",NULL,NULL,NULL);
这很好用,但我对基于文件的格式不满意; 我想在char*变量中以纯二进制形式(即,没有base64或类似形式)保存我的密钥,并从中加载/保存密钥.这样我就有了更大的自由:我可以将密钥直接存储到应用程序中const char key[] { 0x01, 0x02, ... };,通过网络套接字发送等等.
不幸的是,虽然我还没有办法做到这一点.保存和加载我知道的密钥的唯一方法是将其直接读取/保存到文件中.