#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
int main() {
EVP_PKEY *pkey;
EVP_PKEY_CTX *ctx;
RSA *rsa;
FILE *fp;
int bits = 2048;
unsigned long e = RSA_F4;
unsigned char plaintext[256] = "Hello, world!";
unsigned char ciphertext[256];
unsigned char decrypted[256];
// 生成RSA密钥对
pkey = EVP_PKEY_new();
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
EVP_PKEY_keygen_init(ctx);
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits);
EVP_PKEY_keygen(ctx, &pkey);
// 加密明文
ctx = EVP_PKEY_CTX_new(pkey, NULL);
EVP_PKEY_encrypt_init(ctx);
EVP_PKEY_encrypt(ctx, ciphertext, sizeof(ciphertext), plaintext, sizeof(plaintext));
// 解密密文
ctx = EVP_PKEY_CTX_new(pkey, NULL);
EVP_PKEY_decrypt_init(ctx);
EVP_PKEY_decrypt(ctx, decrypted, sizeof(decrypted), ciphertext, sizeof(ciphertext));
// 打印解密后的明文
printf("Decrypted plaintext: %s\n", decrypted);
// 释放资源
EVP_PKEY_free(pkey);
return 0;
}