自己复制的

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值