来源: chatGPT
#include <iostream>
#include <fstream>
#include <openssl/rsa.h>
#include <openssl/pem.h>
bool encryptFile(const char* inputFile, const char* outputFile, RSA* rsaPublicKey) {
FILE* in = fopen(inputFile, "rb");
if (!in) {
std::cerr << "Failed to open input file." << std::endl;
return false;
}
FILE* out = fopen(outputFile, "wb");
if (!out) {
std::cerr << "Failed to open output file." << std::endl;
fclose(in);
return false;
}
const int RSA_KEY_LENGTH = RSA_size(rsaPublicKey);
const int ENCRYPTED_BLOCK_SIZE = RSA_KEY_LENGTH - 42;
const int DECRYPTED_BLOCK_SIZE = RSA_KEY_LENGTH;
unsigned char decryptedData[DECRYPTED_BLOCK_SIZE];
unsigned char encryptedData[ENCRYPTED_BLOCK_SIZE];
int bytesRead;
while ((bytesRead = fread(decryptedData, sizeof(unsigned char), DECRYPTED_BLOCK_SIZE, in)) > 0) {
int encryptedSize = RSA_public_encrypt(bytesRead, decryptedData, encryptedData, rsaPublicKey, RSA_PKCS1_PADDING);
if (encryptedSize == -1) {
std::cerr << "Failed to encrypt data." << std::endl;
fclose(in);
fclose(out);
return false;
}
fwrite(encryptedData, sizeof(unsigned char), encryptedSize, out);
}
fclose(in);
fclose(out);
return true;
}
bool decryptFile(const char* inputFile, const char* outputFile, RSA* rsaPrivateKey) {
FILE* in = fopen(inputFile, "rb");
if (!in) {
std::cerr << "Failed to open input file." << std::endl;
return false;
}
FILE* out = fopen(outputFile, "wb");
if (!out) {
std::cerr << "Failed to open output file." << std::endl;
fclose(in);
return false;
}
const int RSA_KEY_LENGTH = RSA_size(rsaPrivateKey);
const int ENCRYPTED_BLOCK_SIZE = RSA_KEY_LENGTH;
const int DECRYPTED_BLOCK_SIZE = RSA_KEY_LENGTH - 42;
unsigned char decryptedData[DECRYPTED_BLOCK_SIZE];
unsigned char encryptedData[ENCRYPTED_BLOCK_SIZE];
int bytesRead;
while ((bytesRead = fread(encryptedData, sizeof(unsigned char), ENCRYPTED_BLOCK_SIZE, in)) > 0) {
int decryptedSize = RSA_private_decrypt(bytesRead, encryptedData, decryptedData, rsaPrivateKey, RSA_PKCS1_PADDING);
if (decryptedSize == -1) {
std::cerr << "Failed to decrypt data." << std::endl;
fclose(in);
fclose(out);
return false;
}
fwrite(decryptedData, sizeof(unsigned char), decryptedSize, out);
}
fclose(in);
fclose(out);
return true;
}
int main() {
const char* publicKeyFile = "public_key.pem";
const char* privateKeyFile = "private_key.pem";
const char* inputFile = "input.txt";
const char* encryptedFile = "encrypted.bin";
const char* decryptedFile = "decrypted.txt";
// 读取公钥
FILE* publicKeyFP = fopen(publicKeyFile, "rb");
if (!publicKeyFP) {
std::cerr << "Failed to open public key file." << std::endl;
return 1;
}
RSA* rsaPublicKey = PEM_read_RSA_PUBKEY(publicKeyFP, nullptr, nullptr, nullptr);
fclose(publicKeyFP);
if (!rsaPublicKey) {
std::cerr << "Failed to load public key." << std::endl;
return 1;
}
// 读取私钥
FILE* privateKeyFP = fopen(privateKeyFile, "rb");
if (!privateKeyFP) {
std::cerr << "Failed to open private key file." << std::endl;
RSA_free(rsaPublicKey);
return 1;
}
RSA* rsaPrivateKey = PEM_read_RSAPrivateKey(privateKeyFP, nullptr, nullptr, nullptr);
fclose(privateKeyFP);
if (!rsaPrivateKey) {
std::cerr << "Failed to load private key." << std::endl;
RSA_free(rsaPublicKey);
return 1;
}
// 加密文件
if (encryptFile(inputFile, encryptedFile, rsaPublicKey)) {
std::cout << "File encrypted successfully." << std::endl;
} else {
std::cerr << "Failed to encrypt file." << std::endl;
}
// 解密文件
if (decryptFile(encryptedFile, decryptedFile, rsaPrivateKey)) {
std::cout << "File decrypted successfully." << std::endl;
} else {
std::cerr << "Failed to decrypt file." << std::endl;
}
// 释放内存
RSA_free(rsaPublicKey);
RSA_free(rsaPrivateKey);
return 0;
}
编译
g++ -o rsa_example rsa_example.cpp -lssl -lcrypto