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!