Openssl-3.1.1 RSA加密算法的测试

        最近学习了Openssl中rsa的加密过程,并生成了保存在本地的密钥对,用的是openssl3.1.1版本,在此记录。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <error.h>

void generateKeyFiles()
{
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
    if (ctx == NULL) {
        printf("Error creating EVP_PKEY_CTX.\n");
        return;
    }

    if (EVP_PKEY_keygen_init(ctx) <= 0) {
        printf("Error initializing key generation.\n");
        EVP_PKEY_CTX_free(ctx);
        return;
    }

    if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 1024) <= 0) {
        printf("Error setting key size.\n");
        EVP_PKEY_CTX_free(ctx);
        return;
    }

    EVP_PKEY *pkey = NULL;
    if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
        printf("Error generating RSA key.\n");
        EVP_PKEY_CTX_free(ctx);
        return;
    }

    EVP_PKEY_CTX_free(ctx);

    // 保存公钥到文件
    FILE *pub_file = fopen("public.pem", "w");
    if (pub_file != NULL) {
        BIO *pub_bio = BIO_new_file("public.pem", "w");
        if (pub_bio != NULL) {
            if (PEM_write_bio_PUBKEY(pub_bio, pkey) == 1) {
                BIO_free(pub_bio);
                fclose(pub_file);
            } else {
                printf("Error writing public key to file.\n");
                BIO_free(pub_bio);
                fclose(pub_file);
                remove("public.pem");
            }
        } else {
            printf("Error creating BIO object for public key.\n");
            fclose(pub_file);
            remove("public.pem");
        }
    } else {
        perror("File error");
    }

    // 保存私钥到文件
    FILE *priv_file = fopen("private.pem", "w");
    if (priv_file != NULL) {
        BIO *priv_bio = BIO_new_file("private.pem", "w");
        if (priv_bio != NULL) {
            if (PEM_write_bio_PrivateKey(priv_bio, pkey, NULL, NULL, 0, NULL, NULL) == 1) {
                BIO_free(priv_bio);
                fclose(priv_file);
            } else {
                printf("Error writing private key to file.\n");
                BIO_free(priv_bio);
                fclose(priv_file);
                remove("private.pem");
            }
        } else {
            printf("Error creating BIO object for private key.\n");
            fclose(priv_file);
            remove("private.pem");
        }
    } else {
        perror("File error");
    }

    EVP_PKEY_free(pkey);
}

int main()
{
    generateKeyFiles();

    BIO *pub_bio = BIO_new_file("public.pem", "r");
    if (pub_bio == NULL) {
        perror("Failed to open public key file.");
        return 1;
    }

    EVP_PKEY *pub_key = PEM_read_bio_PUBKEY(pub_bio, NULL, NULL, NULL);
    BIO_free(pub_bio);
    if (pub_key == NULL) {
        printf("Failed to read public key from file.\n");
        return 1;
    }

    BIO *priv_bio = BIO_new_file("private.pem", "r");
    if (priv_bio == NULL) {
        perror("Failed to open private key file.");
        EVP_PKEY_free(pub_key);
        return 1;
    }

    EVP_PKEY *priv_key = PEM_read_bio_PrivateKey(priv_bio, NULL, NULL, NULL);
    BIO_free(priv_bio);
    if (priv_key == NULL) {
        printf("Failed to read private key from file.\n");
        EVP_PKEY_free(pub_key);
        return 1;
    }

    // 待加密的数据
    unsigned char plaintext[] = "Hello, RSA!";
    size_t plaintext_len = sizeof(plaintext) - 1;

    // 加密后的数据缓冲区
    unsigned char ciphertext[256];
    size_t ciphertext_len = 0;

    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pub_key, NULL);
    if (ctx == NULL) {
        printf("Error creating EVP_PKEY_CTX for encryption.\n");
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    if (EVP_PKEY_encrypt_init(ctx) <= 0) {
        printf("Error initializing RSA encryption.\n");
        EVP_PKEY_CTX_free(ctx);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) {
        printf("Error setting RSA padding.\n");
        EVP_PKEY_CTX_free(ctx);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    if (EVP_PKEY_encrypt(ctx, ciphertext, &ciphertext_len, plaintext, plaintext_len) <= 0) {
        printf("RSA encryption failed.\n");
        EVP_PKEY_CTX_free(ctx);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    printf("Ciphertext: ");
    for (size_t i = 0; i < ciphertext_len; i++) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");

    EVP_PKEY_CTX_free(ctx);

    // 解密后的数据缓冲区
    unsigned char *decrypted_text;
    size_t decrypted_text_len = 0;

    EVP_PKEY_CTX *ctx1 = EVP_PKEY_CTX_new(priv_key, NULL);
    if (ctx1 == NULL) {
        printf("Error creating EVP_PKEY_CTX for decryption.\n");
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    if (EVP_PKEY_decrypt_init(ctx1) <= 0) {
        printf("Error initializing RSA decryption.\n");
        EVP_PKEY_CTX_free(ctx1);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }
    //此处先进行解密明文的长度的初始化
    EVP_PKEY_decrypt(ctx1, NULL, &decrypted_text_len, ciphertext, ciphertext_len);
    //按照长度开辟空间
    decrypted_text = malloc(plaintext_len + 1);
    
    //填充
    if (EVP_PKEY_CTX_set_rsa_padding(ctx1, RSA_PKCS1_PADDING) <= 0) {
        printf("Error setting RSA padding.\n");
        EVP_PKEY_CTX_free(ctx1);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    //再进行解密
    if (EVP_PKEY_decrypt(ctx1, decrypted_text, &decrypted_text_len, ciphertext, ciphertext_len) <= 0) {
        printf("RSA decryption failed.\n");
        system("pause");
        EVP_PKEY_CTX_free(ctx1);
        EVP_PKEY_free(pub_key);
        EVP_PKEY_free(priv_key);
        return 1;
    }

    decrypted_text[decrypted_text_len] = '\0';
    printf("Decrypted Text: %s\n", decrypted_text);

    EVP_PKEY_CTX_free(ctx1);
    EVP_PKEY_free(pub_key);
    EVP_PKEY_free(priv_key);
    //system("pause");
    return 0;
}

windows和linux都适用,注意包含头文件和静态库

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenSSL是一个广泛使用的开放源代码工具包,提供加密和解密功能。它支持DES和RSA加密算法,可以用来加密和解密数据。DES是一种对称加密算法,使用相同的密钥进行加密和解密。RSA是一种非对称加密算法,使用公钥进行加密,私钥进行解密。 使用OpenSSL进行DES加密和解密,首先需要生成一个DES密钥,然后使用该密钥进行加密和解密操作。可以使用以下命令生成DES密钥: ``` openssl rand -out des.key 8 ``` 生成的密钥保存在des.key文件中。然后使用该密钥进行加密和解密: ``` openssl enc -des -in plaintext.txt -out encrypted.des -kfile des.key openssl enc -d -des -in encrypted.des -out decrypted.txt -kfile des.key ``` 上述命令分别用指定的DES密钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.des文件中。解密操作则相反,使用相同的DES密钥对加密后的文件进行解密,得到明文文件decrypted.txt。 而要使用RSA算法进行加密和解密,首先需要生成RSA密钥对(公钥和私钥),然后使用公钥进行加密,私钥进行解密。可以使用以下命令生成RSA密钥对: ``` openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -pubout -out public.pem ``` 生成的私钥保存在private.pem文件中,公钥保存在public.pem文件中。然后使用公钥进行加密,私钥进行解密: ``` openssl rsautl -encrypt -in plaintext.txt -out encrypted.rsa -inkey public.pem openssl rsautl -decrypt -in encrypted.rsa -out decrypted.txt -inkey private.pem ``` 上述命令分别使用指定的公钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.rsa文件中。解密操作则相反,使用相同的私钥对加密后的文件进行解密,得到明文文件decrypted.txt。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值