前一篇文章: http://blog.csdn.net/qq372848728/article/details/78687876

int rsa_key_encrypt(EVP_PKEY *key, const unsigned char *orig_data, size_t orig_data_len,
    unsigned char *enc_data, size_t &enc_data_len)
    EVP_PKEY_CTX *ctx = NULL;
    OpenSSL_add_all_ciphers();
    ctx = EVP_PKEY_CTX_new(key, NULL);
    if (NULL == ctx)
        printf("ras_pubkey_encryptfailed to open ctx.\n");
        EVP_PKEY_free(key);
        return -1;
    if (EVP_PKEY_encrypt_init(ctx) <= 0)
        printf("ras_pubkey_encryptfailed to EVP_PKEY_encrypt_init.\n");
        EVP_PKEY_free(key);
        return -1;
    int ret = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
    if (!ret) {
        printf("set padding error!");
        return 0;
    //EVP_CIPHER_CTX_set_padding(ctx, EVP_CIPH_NO_PADDING);
    if (EVP_PKEY_encrypt(ctx,
        enc_data,
        &enc_data_len,
        orig_data,
        orig_data_len) <= 0)
        printf("ras_pubkey_encryptfailed to EVP_PKEY_encrypt.\n");
        EVP_PKEY_CTX_free(ctx);
        EVP_PKEY_free(key);
        return -1;
    EVP_PKEY_CTX_free(ctx);
    EVP_PKEY_free(key);
    return 0;
// 使用密钥解密,这种封装格式只适用公钥加密,私钥解密,这里key必须是私钥  
int rsa_key_decrypt(EVP_PKEY *key, const unsigned char *enc_data, size_t enc_data_len,
    unsigned char *orig_data, size_t &orig_data_len)
    EVP_PKEY_CTX *ctx = NULL;
    OpenSSL_add_all_ciphers();
    ctx = EVP_PKEY_CTX_new(key, NULL);
    if (NULL == ctx)
        printf("ras_prikey_decryptfailed to open ctx.\n");
        EVP_PKEY_free(key);
        return -1;
    if (EVP_PKEY_decrypt_init(ctx) <= 0)
        printf("ras_prikey_decryptfailed to EVP_PKEY_decrypt_init.\n");
        EVP_PKEY_free(key);
        return -1;
    int ret = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING);
    if (ret = EVP_PKEY_decrypt(ctx,
        orig_data,
        &orig_data_len,
        enc_data,
        enc_data_len) <= 0)
        printf("ras_prikey_decryptfailed to EVP_PKEY_decrypt.\n");
        EVP_PKEY_CTX_free(ctx);
        EVP_PKEY_free(key);
        return -1;
    EVP_PKEY_CTX_free(ctx);
    EVP_PKEY_free(key);
    return 0;
EVP_PKEY *generatePubEVPKEY(char * keyChar) {
    int ret, flen, bio_len;
    BIO *bio = NULL;
    RSA *r = NULL;
    if ((bio = BIO_new_mem_buf((void *)keyChar, strlen(keyChar))) == NULL)       //从字符串读取RSA公钥
        //LOGE("BIO_new_mem_buf failed!\n");
    PEM_read_bio_RSA_PUBKEY(bio, &r, 0, 0);
    flen = RSA_size(r);
    EVP_PKEY *evp_key = EVP_PKEY_new();//新建一个EVP_PKEY变量
    if (evp_key == NULL)
        RSA_free(r);
        return NULL;
    if (EVP_PKEY_set1_RSA(evp_key, r) != 1)  //保存RSA结构体到EVP_PKEY结构体
        RSA_free(r);
        EVP_PKEY_free(evp_key);
        return NULL;
    RSA_free(r);
    return evp_key;
extern EVP_PKEY *generatePriEVPKEY(char *keyChar) {
    int ret, flen, bio_len;
    BIO *bio = NULL;
    RSA *r = NULL;
    if ((bio = BIO_new_mem_buf((void *)keyChar, strlen(keyChar))) == NULL)       //从字符串读取RSA公钥
        //LOGE("BIO_new_mem_buf failed!\n");
    PEM_read_bio_RSAPrivateKey(bio, &r, 0, 0);
    flen = RSA_size(r);
    EVP_PKEY *evp_key = EVP_PKEY_new();//新建一个EVP_PKEY变量
    if (evp_key == NULL)
        RSA_free(r);
        return NULL;
    if (EVP_PKEY_set1_RSA(evp_key, r) != 1)  //保存RSA结构体到EVP_PKEY结构体
        RSA_free(r);
        EVP_PKEY_free(evp_key);
        return NULL;
    RSA_free(r);
    return evp_key;

EVP_PKEY *pub_key = generatePubEVPKEY(pubkey1024);//新建一个EVP_PKEY变量
EVP_PKEY *pri_key = generatePriEVPKEY(prikey1024);
调用rsa_key_decrypt/rsa_key_encrypt两个函数即可加解密

前一篇文章:http://blog.csdn.net/qq372848728/article/details/78687876int rsa_key_encrypt(EVP_PKEY *key, const unsigned char *orig_data, size_t orig_data_len, unsigned char *enc_data, size_t &enc_data_len
1.该程序是基于OpenSSL的使用纯C语言实现RSA加解密的,在Linux环境下开发完成,建议在Linux环境下使用(在Windows环境下需要自行修改); 2.该程序具有生成RSA密钥对、RSA公钥加密和RSA私钥解密的功能,支持手动输入密钥; 3.该程序支持不限长度的明文加密,支持256~4096长度的密钥,支持Crypto++密码库生成的RSA密钥对(公钥:X.509,私钥:PKCS#8); 4.该程序包中提供有可供二次开发的接口源文件和可在Linux下使用的.so动态链接库文件; 5.该程序包中提供了相应功能的测试小程序。
RSA 加密解密简单案列 Openssl 1.1.1 C语言实现一、直接复制公钥私钥到代码里代码gcc编译运行结果二、读取pem文件后加密解密代码编译 一、直接复制公钥私钥到代码里 main.c #include "crypto.h" int main(int argc, char* argv[]) char* cleartext = "123abc"; if (sizeof(cleartext) > 256) { printf("cleartext too len
int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, const unsigned char *from, int flen); int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, const unsigned char *from, int flen, int num); int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, const unsigned char *from, int flen); int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, const unsigned char *from, int flen, int num); https://blog.csdn.net/github_35454460/article/details/51862470 https://developer.aliyun.com/article/693527 之前文章有提过使用openssl库能够很方便的实现rsa的加密和解密,但是在实际使用过程中发现,无法解密客户方发送过来的密文,原因是客户使用的填充方式是OAEPWithSHA-256,两边填充方式不一致导致。 但是这里会有一个问题,ope...