代码一: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
代码二:CSDN上的一段代码
- 生成私钥
openssl genrsa -out prikey.pem 1024
- 生成公钥
openssl rsa -pubout -in prikey.pem -out pubkey.pem
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
const char *publicKey = "-----BEGIN PUBLIC KEY-----\n\
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcFzwk3q/VNKjCloHn+ClRF7el\n\
sfsJIVkzCkqK62ufPyThsIEW6UlEIrA6wVY3lwG84wmknHesWnhOntLZNI5QrPgn\n\
1FOL4fVJtaeWq1azoHwejtPD0QNM03CKIuvxV5MV7hh9QXirfb7ac6zMVsQ4vf4w\n\
swiTBdt8l1JFIXj0dQIDAQAB\n\
-----END PUBLIC KEY-----";
const char *privateKey = "-----BEGIN RSA PRIVATE KEY-----\n\
MIICXQIBAAKBgQCcFzwk3q/VNKjCloHn+ClRF7elsfsJIVkzCkqK62ufPyThsIEW\n\
6UlEIrA6wVY3lwG84wmknHesWnhOntLZNI5QrPgn1FOL4fVJtaeWq1azoHwejtPD\n\
0QNM03CKIuvxV5MV7hh9QXirfb7ac6zMVsQ4vf4wswiTBdt8l1JFIXj0dQIDAQAB\n\
AoGAbdhMEwZDZx7vxEB2mvAg41h1fL2LlBlL1IdhffG+gwrEwa6cqaryTGi87Bwy\n\
uFwkdPCxUI5sCXaBQ5KxfvllTHeMZKfOlDy0vwpOP4g0UJoJRp/GaEee4zZVjJgN\n\
WRrjQXobOLSnEJRrjQkKW9gmrrs5kSLu041HpI3IqKetHgECQQDKriYVw6t96sXD\n\
0Wib5eksQB5uG96/S8000TrWpVZ8BgwRei+u1TCplXuLYyTWGv3RIJITrNCAefFJ\n\
uy1gezYxAkEAxSdviW9CmTCBjVEV0sLCeFqMT7dck5BzM44yfekrIY4JHxzBYlOF\n\
J+6/MsKAAwXSmu9m9Pkki+QWWiwsC6ZdhQJBALQAjlpt+PRPeWw7osgyHC/skhoA\n\
Lod+brUlzTesh26GgFlVRtvjAuDZnWthlemA90KvxowqwtIXeq+cyOQTCUECQHIm\n\
QB0JzzyU6h2hOgp6e1Bl1OF+39oMg7bw+OkP7MoTy9I4uvssMmeJgyhnluipIWRV\n\
gn4KFo/scsio1KO2ImUCQQC41CzCihfolvlSCE4Q1YtCYf63iGnpTm95DvmDCskS\n\
vIphhmeN3XFciq7w78LbLJTpn9AB66TtYnRWegSoGcho\n\
-----END RSA PRIVATE KEY-----";
static void inner_encryptFile(const char *srcFilename, RSA *pubKey, const char *enFilename)
{
FILE *fpSrc;
FILE *fpTgt;
size_t readLen;
char *sBuf;
char *dBuf;
size_t sBufLen;
size_t dBufLen;
dBufLen = RSA_size(pubKey);
sBufLen = dBufLen - 11;
fpSrc = fopen(srcFilename, "rb");
fpTgt = fopen(enFilename, "wb");
sBuf = (char *)malloc(sBufLen);
dBuf = (char *)malloc(dBufLen);
while(1)
{
int len;
readLen = fread(sBuf, 1, sBufLen, fpSrc);
if(readLen > 0)
{
len = RSA_public_encrypt(readLen, (unsigned char *)sBuf, (unsigned char *)dBuf, pubKey, RSA_PKCS1_PADDING);
if (len < 0)
{
printf("RSA_public_encrypt failed!%u\n", ERR_get_error());
break;
}
fwrite(dBuf, 1, len, fpTgt);
}
else if(feof(fpSrc) && !ferror(fpSrc))
{
break;
}
else
{
printf("inner_encryptFile fread failed!");
break;
}
}
free(sBuf);
free(dBuf);
fflush(fpTgt);
fclose(fpTgt);
fclose(fpSrc);
}
static void inner_decryptFile(const char *enFilename, RSA *priKey, const char *deFilename)
{
FILE *fpEn;
FILE *fpDe;
size_t readLen;
char *sBuf;
char *dBuf;
size_t sBufLen;
size_t dBufLen;
sBufLen = RSA_size(priKey);
dBufLen = sBufLen - 11;
fpEn = fopen(enFilename, "rb");
fpDe = fopen(deFilename, "wb");
sBuf = (char *)malloc(sBufLen);
dBuf = (char *)malloc(dBufLen);
while(1)
{
int len;
readLen = fread(sBuf, 1, sBufLen, fpEn);
if(readLen > 0)
{
len = RSA_private_decrypt(readLen, (unsigned char *)sBuf, (unsigned char *)dBuf, priKey, RSA_PKCS1_PADDING);
if (len < 0)
{
printf("RSA_private_decrypt failed!%u\n", ERR_get_error());
break;
}
fwrite(dBuf, 1, len, fpDe);
}
else if(feof(fpEn) && !ferror(fpEn))
{
break;
}
else
{
printf("inner_decryptFile fread failed!");
break;
}
}
free(sBuf);
free(dBuf);
fclose(fpEn);
fflush(fpDe);
fclose(fpDe);
}
int main(int argc, char *argv[])
{
BIO *bio = NULL;
RSA *publicRsa = NULL;
RSA *privateRsa = NULL;
if ((bio = BIO_new_mem_buf((void *)publicKey, -1)) == NULL)
{
printf("BIO_new_mem_buf publicKey error\n");
return -1;
}
if ((publicRsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)) == NULL)
{
printf("PEM_read_bio_RSA_PUBKEY error\n");
return -1;
}
BIO_free_all(bio);
if ((bio = BIO_new_mem_buf((void *)privateKey, -1)) == NULL)
{
printf("BIO_new_mem_buf privateKey error\n");
return -1;
}
if ((privateRsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)) == NULL)
{
printf("PEM_read_RSAPrivateKey error\n");
return NULL;
}
BIO_free_all(bio);
inner_encryptFile(argv[1], publicRsa, argv[2]);
inner_decryptFile(argv[2], privateRsa, argv[3]);
RSA_free(publicRsa);
RSA_free(privateRsa);
return 0;
}