我正在尝试构建一个需要以下内容的P2P应用程序,使用OpenSSL中的RSA:
-Encryption
-Decryption
-Generating Keys (done)
-Saving and loading keys (done)
-Saving the PUBLIC key as bytes so it can be sent over the sockets
-Loading keys from the above format
我选择使用EVP函数,无论是什么意思。然而,我非常难找到需要使用的函数以及它们的顺序和原型。OpenSSL的官方文档似乎并不存在。
有谁知道我需要使用哪些函数以及它们的顺序和原型?任何现成的示例代码也会很好。
提前感谢,twitchliquid64。
PS:这是我目前的进展。
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/engine.h>
#include <openssl/rand.h>
RSA* Generate_KeyPair(void)
{
char rand_buff[16];
EVP_PKEY *pkey = NULL;
RSA* r;
char* pass = "passgdfgf";//for now
int bits = 512; // 512, 1024, 2048, 4096
unsigned long exp = RSA_F4; // RSA_3
OpenSSL_add_all_algorithms();
RAND_seed(rand_buff, 16); //On linux: RAND_load_file("/dev/urandom", 1024);
r = RSA_generate_key(bits,exp,NULL,NULL);
if (RSA_check_key(r)!=1);;; //Check key - error out
//Create EVP to save to file.
pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, r);
//Save private key
FILE* fp = fopen("private.key", "w");
PEM_write_PrivateKey(fp,pkey,EVP_aes_256_cbc(),NULL,0,NULL,pass)
fclose(fp);
//Save public key
fp = fopen("public.key", "w");
PEM_write_PUBKEY(fp, pkey);
fclose(fp);
return r;
}
EVP_PKEY* ReadPrivKey_FromFile(char* filename, char* pass)
{
FILE* fp = fopen(filename, "r");
EVP_PKEY* key = NULL;
PEM_read_PrivateKey(fp, &key, NULL, pass);
fclose(fp);
return key;
}
EVP_PKEY* ReadPubKey_FromFile(char* filename)
{
FILE* fp = fopen(filename, "r");
EVP_PKEY* key = NULL;
PEM_read_PUBKEY(fp, &key, NULL, NULL);
fclose(fp);
return key;
}
apps/rsa.c
中。它可能有助于确定使用命令行工具执行每个功能所需的 OpenSSL 命令行,并找出代码实际执行的操作(通过检查代码),以便使您的代码实现相同的操作。 - David Schwartz