RSA加解密,c语言实现
·
加密代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
int main() {
// 读取公钥
FILE *publicKeyFile = fopen("public_key.pem", "r");
if (publicKeyFile == NULL) {
printf("无法打开公钥文件\n");
return -1;
}
RSA *publicKey = PEM_read_RSA_PUBKEY(publicKeyFile, NULL, NULL, NULL);
fclose(publicKeyFile);
if (publicKey == NULL) {
printf("无法读取公钥\n");
return -1;
}
// 读取待加密的文件内容
FILE *inputFile = fopen("test.txt", "rb");
if (inputFile == NULL) {
printf("无法打开输入文件\n");
return -1;
}
fseek(inputFile, 0, SEEK_END);
long fileSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
unsigned char *fileContent = malloc(fileSize);
fread(fileContent, 1, fileSize, inputFile);
fclose(inputFile);
// 计算加密后的数据长度
int rsaSize = RSA_size(publicKey);
unsigned char *encryptedData = malloc(rsaSize);
// 进行RSA加密
int result = RSA_public_encrypt(fileSize, fileContent, encryptedData, publicKey, RSA_PKCS1_PADDING);
if (result == -1) {
printf("加密失败: %s\n", ERR_error_string(ERR_get_error(), NULL));
return -1;
}
// 将加密后的数据写入输出文件
FILE *outputFile = fopen("encrypted_data.bin", "wb");
if (outputFile == NULL) {
printf("无法打开输出文件\n");
return -1;
}
fwrite(encryptedData, 1, rsaSize, outputFile);
fclose(outputFile);
// 释放内存和关闭密钥
free(fileContent);
free(encryptedData);
RSA_free(publicKey);
printf("加密成功\n");
return 0;
}
解密代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
int main() {
// 读取私钥
FILE *privateKeyFile = fopen("private_key.pem", "r");
if (privateKeyFile == NULL) {
printf("无法打开私钥文件\n");
return -1;
}
RSA *privateKey = PEM_read_RSAPrivateKey(privateKeyFile, NULL, NULL, NULL);
fclose(privateKeyFile);
if (privateKey == NULL) {
printf("无法读取私钥\n");
return -1;
}
// 读取加密后的数据
FILE *encryptedDataFile = fopen("encrypted_data.bin", "rb");
if (encryptedDataFile == NULL) {
printf("无法打开加密数据文件\n");
return -1;
}
fseek(encryptedDataFile, 0, SEEK_END);
long fileSize = ftell(encryptedDataFile);
fseek(encryptedDataFile, 0, SEEK_SET);
unsigned char *encryptedData = malloc(fileSize);
fread(encryptedData, 1, fileSize, encryptedDataFile);
fclose(encryptedDataFile);
// 计算解密后的数据长度
int rsaSize = RSA_size(privateKey);
unsigned char *decryptedData = malloc(rsaSize);
// 进行RSA解密
int result = RSA_private_decrypt(fileSize, encryptedData, decryptedData, privateKey, RSA_PKCS1_PADDING);
if (result == -1) {
printf("解密失败: %s\n", ERR_error_string(ERR_get_error(), NULL));
return -1;
}
// 将解密后的数据写入输出文件
FILE *outputFile = fopen("decrypted_data.txt", "wb");
if (outputFile == NULL) {
printf("无法打开输出文件\n");
return -1;
}
fwrite(decryptedData, 1, rsaSize, outputFile);
fclose(outputFile);
// 释放内存和关闭密钥
free(encryptedData);
free(decryptedData);
RSA_free(privateKey);
printf("解密成功\n");
return 0;
}
gcc编译:
gcc -o out 加密代码 -lssl -lcrypto
更多推荐


所有评论(0)