利用openssl进行rsa加解密的例子

OpenSSL介绍

OpenSSL安装

https://blog.csdn.net/zhizhengguan/article/details/112846817

OpenSSL实例

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

#pragma comment(lib, "libssl.lib")
#pragma comment(lib, "libcrypto.lib")

extern "C" {
#include <openssl/applink.c>
}

// 加密函数
int encrypt_rsa(const char* public_key_path, const unsigned char* plain_text, int plain_text_len, unsigned char** cipher_text, int* cipher_text_len) {
    FILE* public_key_file = fopen(public_key_path, "rb");
    if (!public_key_file) {
        perror("Failed to open public key file");
        return 0;
    }

    RSA* rsa = PEM_read_RSA_PUBKEY(public_key_file, NULL, NULL, NULL);
    if (!rsa) {
        ERR_print_errors_fp(stderr);
        fclose(public_key_file);
        return 0;
    }
    fclose(public_key_file);

    *cipher_text = (unsigned char*)malloc(RSA_size(rsa));
    *cipher_text_len = RSA_public_encrypt(plain_text_len, plain_text, *cipher_text, rsa, RSA_PKCS1_PADDING);

    RSA_free(rsa);

    if (*cipher_text_len == -1) {
        ERR_print_errors_fp(stderr);
        return 0;
    }

    return 1;
}

// 解密函数
int decrypt_rsa(const char* private_key_path, const unsigned char* cipher_text, int cipher_text_len, unsigned char** decrypted_text, int* decrypted_text_len) {
    FILE* private_key_file = fopen(private_key_path, "rb");
    if (!private_key_file) {
        perror("Failed to open private key file");
        return 0;
    }

    RSA* rsa = PEM_read_RSAPrivateKey(private_key_file, NULL, NULL, NULL);
    if (!rsa) {
        ERR_print_errors_fp(stderr);
        fclose(private_key_file);
        return 0;
    }
    fclose(private_key_file);

    *decrypted_text = (unsigned char*)malloc(RSA_size(rsa));
    *decrypted_text_len = RSA_private_decrypt(cipher_text_len, cipher_text, *decrypted_text, rsa, RSA_PKCS1_PADDING);

    RSA_free(rsa);

    if (*decrypted_text_len == -1) {
        ERR_print_errors_fp(stderr);
        return 0;
    }

    return 1;
}

int main() {
    const char* public_key_path = "public_key.pem";
    const char* private_key_path = "private_key.pem";

    const unsigned char* plain_text = (const unsigned char*)"Hello, RSA encryption!";
    int plain_text_len = strlen((const char*)plain_text);

    unsigned char* cipher_text = NULL;
    int cipher_text_len = 0;

    unsigned char* decrypted_text = NULL;
    int decrypted_text_len = 0;

    // 加密
    if (encrypt_rsa(public_key_path, plain_text, plain_text_len, &cipher_text, &cipher_text_len)) {
        printf("Encryption successful.\n");
        printf("Cipher Text: ");
        for (int i = 0; i < cipher_text_len; i++) {
            printf("%02x", cipher_text[i]);
        }
        printf("\n");

        // 解密
        if (decrypt_rsa(private_key_path, cipher_text, cipher_text_len, &decrypted_text, &decrypted_text_len)) {
            printf("Decryption successful.\n");
            printf("Decrypted Text: %s\n", decrypted_text);
            free(decrypted_text);
        }
        else {
            printf("Decryption failed.\n");
        }

        free(cipher_text);
    }
    else {
        printf("Encryption failed.\n");
    }

    return 0;
}

利用OpenSSL生成公钥与私钥

openssl genpkey -algorithm RSA -out private_key.pem
openssl rsa -pubout -in private_key.pem -out public_key.pem

运行结果

Encryption successful.
Cipher Text: 566c0f9f34fb1deaf00b4d51b209bab05e009dc08ef94bc2b21b07bce49cbf5103d3e6fc69d9168124f89d05e234d165ef5d2896c2f4e8016bf2a49b539544437f3b6eac6919adae5fa17cd8bc9e45ee7a01fab8cd4fa35a280b780aed6fce2bb4ed98c891801d25e31c2b1737533d448864cd9309962eb168f938bc76f4e76c171cccf45914475446a69c02f9378e0edea4c717bb123a096e6f3ee8e2ae195a7b51cc9197ec1b5ac82963660dce47a3c18067b9bc082f160ec5acce7d373a39d218bd59d097d5d90f0b9ec74094d570426d876d2f6403f8ed2d145227c6c4f4519b13ce087fdf63322612a85697fb26c047cd565c09d2304bde822b99d0e689
Decryption successful.
Decrypted Text: Hello, RSA encryption!
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OpenSSL是一个开源的加密库,它提供了RSA加密、解密、签名和验签的功能。 对于RSA加密和解密,我们可以使用OpenSSL提供的命令行工具或者API来实现。 使用命令行工具,我们可以通过以下命令进行RSA加密: openssl rsautl -encrypt -in <input file> -out <output file> -inkey <public key file> -pubin 其中,<input file>是要加密的文件,<output file>是加密后的文件,<public key file>是存储公钥的文件,-pubin参数表示输入的是公钥。 使用命令行工具,我们可以通过以下命令进行RSA解密: openssl rsautl -decrypt -in <input file> -out <output file> -inkey <private key file> 其中,<input file>是要解密的文件,<output file>是解密后的文件,<private key file>是存储私钥的文件。 对于RSA签名和验签,我们可以使用以下命令进行签名: openssl rsautl -sign -in <input file> -out <output file> -inkey <private key file> 其中,<input file>是要签名的文件,<output file>是签名后的文件,<private key file>是存储私钥的文件。 使用以下命令进行验签: openssl rsautl -verify -in <input file> -out <output file> -inkey <public key file> -pubin 其中,<input file>是要验签的文件,<output file>是验签后的文件,<public key file>是存储公钥的文件,-pubin参数表示输入的是公钥。 使用OpenSSL的API进行RSA加密、解密、签名和验签的操作也是类似的,我们可以通过调用相应的函数来实现。需要注意的是,API的使用需要在代码中显式引入OpenSSL的头文件和链接OpenSSL的库文件。 总之,OpenSSL提供了便捷的工具和API来实现RSA加密、解密、签名和验签的功能,无论是命令行工具还是API,都可以选择合适的方式进行操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值