在C语言中,异或加密是一种基于位运算的轻量级文件加密方式,其核心原理是利用异或运算的可逆性:同一个数值与同一个密钥异或两次,会得到原始数值。这种特性使得我们可以用同一个密钥对文件进行加密和解密,实现逻辑简单且运算效率高,适合对普通文本、二进制文件做基础的安全保护。

异或加密的基本原理
异或运算的符号是^,运算规则是:两个二进制位相同结果为0,不同结果为1。例如数值5(二进制0101)和密钥3(二进制0011)异或,得到结果6(二进制0110);再用6和密钥3异或,就会得到原始的5。这个特性是异或加密可逆的核心基础。
对于文件加密来说,我们只需要将文件中的每个字节依次和预设的密钥进行异或运算,生成的新字节序列就是加密后的文件内容。解密时,用同样的密钥对加密后的文件字节再次异或,就能还原出原始文件。
完整实现步骤
1. 定义密钥和辅助函数
首先我们需要定义一个简单的密钥,这里用单个字符作为密钥,也可以扩展为多字节密钥。同时需要封装文件读取和写入的基础逻辑。
#include <stdio.h> #include <stdlib.h> // 定义加密密钥,可自定义修改 #define ENCRYPT_KEY 'K' // 函数声明 int encrypt_file(const char *source_path, const char *target_path);
2. 实现文件异或加密核心逻辑
核心逻辑分为三步:打开源文件和目标文件,循环读取源文件的每个字节,将字节与密钥异或后写入目标文件,最后关闭文件句柄。
int encrypt_file(const char *source_path, const char *target_path) {
FILE *source_file = fopen(source_path, "rb");
if (source_file == NULL) {
printf("无法打开源文件: %sn", source_path);
return -1;
}
FILE *target_file = fopen(target_path, "wb");
if (target_file == NULL) {
printf("无法创建目标文件: %sn", target_path);
fclose(source_file);
return -1;
}
int byte;
// 循环读取每个字节直到文件结束
while ((byte = fgetc(source_file)) != EOF) {
// 字节与密钥异或后写入目标文件
fputc(byte ^ ENCRYPT_KEY, target_file);
}
fclose(source_file);
fclose(target_file);
return 0;
}
3. 主函数调用示例
在主函数中传入源文件路径和目标文件路径,即可完成加密操作,同样的代码也可以用于解密,只需要将加密后的文件作为源文件,输出原始文件即可。
int main() {
const char *source_file_path = "test.txt";
const char *encrypted_file_path = "test_encrypted.txt";
const char *decrypted_file_path = "test_decrypted.txt";
// 加密文件
printf("开始加密文件...n");
if (encrypt_file(source_file_path, encrypted_file_path) == 0) {
printf("文件加密完成,结果保存在: %sn", encrypted_file_path);
}
// 解密文件(用同样的密钥对加密后的文件再次异或)
printf("开始解密文件...n");
if (encrypt_file(encrypted_file_path, decrypted_file_path) == 0) {
printf("文件解密完成,结果保存在: %sn", decrypted_file_path);
}
return 0;
}
算法注意事项
- 单字节密钥的安全性较低,容易被暴力破解,实际使用中建议扩展为多字节密钥,循环使用密钥字节对文件进行加密。
- 该算法属于对称加密,加密和解密使用同一个密钥,需要妥善保管密钥,避免泄露。
- 异或加密适合对普通文件做基础混淆,不适合保护高敏感数据,高安全场景建议使用更成熟的加密算法。
- 处理大文件时,当前逻辑是逐字节读取,也可以扩展为缓冲区读取,提升大文件的处理效率。
多字节密钥扩展示例
如果需要提升安全性,可以使用多字节密钥,循环取密钥的每个字节与文件字节异或,实现逻辑如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 多字节密钥
const char ENCRYPT_KEY[] = "MySecretKey";
int key_len = 0;
// 初始化密钥长度
void init_key_len() {
key_len = strlen(ENCRYPT_KEY);
}
int encrypt_file_multi_key(const char *source_path, const char *target_path) {
FILE *source_file = fopen(source_path, "rb");
if (source_file == NULL) {
printf("无法打开源文件: %sn", source_path);
return -1;
}
FILE *target_file = fopen(target_path, "wb");
if (target_file == NULL) {
printf("无法创建目标文件: %sn", target_path);
fclose(source_file);
return -1;
}
int byte;
int key_index = 0;
while ((byte = fgetc(source_file)) != EOF) {
// 循环使用多字节密钥的每个字节
fputc(byte ^ ENCRYPT_KEY[key_index % key_len], target_file);
key_index++;
}
fclose(source_file);
fclose(target_file);
return 0;
}
int main() {
init_key_len();
const char *source_file_path = "test.txt";
const char *encrypted_file_path = "test_multi_encrypted.txt";
const char *decrypted_file_path = "test_multi_decrypted.txt";
printf("开始多密钥加密文件...n");
if (encrypt_file_multi_key(source_file_path, encrypted_file_path) == 0) {
printf("多密钥加密完成,结果保存在: %sn", encrypted_file_path);
}
printf("开始多密钥解密文件...n");
if (encrypt_file_multi_key(encrypted_file_path, decrypted_file_path) == 0) {
printf("多密钥解密完成,结果保存在: %sn", decrypted_file_path);
}
return 0;
}
C语言文件加密异或加密XOR_algorithm修改时间:2026-06-26 02:30:21