文件加密程序:将待加密文件以二进制打开,读入32个字节,分别与口令异或,在将4、5、6三个字分别与(0x四个a)异或,即完成加密,执行第二次就解密。
一点都不会,希望提供思路
参考:https://blog.csdn.net/qq_72111942/article/details/126477652
只不过你需要把0xaaaaaa插入到你的口令的456中
整了点花活,但是程序没问题,一样用。。。
//插入排序
void sort(int* x, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 1+i; j < n; j++)
{
if (x[i] < x[j])
{
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
printf("%d ", x[i]);
}
}
int main()
{
int n = 0;
scanf("%d", &n);
int *x = (int*)malloc(n*sizeof(int));
int i = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &x[i]);
}
int num = 0;
scanf("%d", &num);
x = (int*)realloc(x, (n+1)*sizeof(int));
x[i] = num;
sort(x, n+1);
return 0;
}
运行结果:
下面是使用C语言来实现以上步骤的示例代码:
#include <stdio.h>
void encryptFile(const char* inputFile, const char* outputFile, const char* password) {
FILE* input = fopen(inputFile, "rb");
FILE* output = fopen(outputFile, "wb");
if (input == NULL || output == NULL) {
printf("无法打开文件\n");
return;
}
unsigned char buffer[32];
fread(buffer, sizeof(unsigned char), 32, input);
// 与口令进行异或操作
for (int i = 0; i < sizeof(buffer); i++) {
buffer[i] ^= password[i % sizeof(password)];
}
// 将第4、5、6个字节与0xaaaa进行异或操作
buffer[3] ^= 0xaa;
buffer[4] ^= 0xaa;
buffer[5] ^= 0xaa;
fwrite(buffer, sizeof(unsigned char), 32, output);
fclose(input);
fclose(output);
printf("加密完成\n");
}
void decryptFile(const char* inputFile, const char* outputFile, const char* password) {
FILE* input = fopen(inputFile, "rb");
FILE* output = fopen(outputFile, "wb");
if (input == NULL || output == NULL) {
printf("无法打开文件\n");
return;
}
unsigned char buffer[32];
fread(buffer, sizeof(unsigned char), 32, input);
// 将第4、5、6个字节与0xaaaa进行异或操作
buffer[3] ^= 0xaa;
buffer[4] ^= 0xaa;
buffer[5] ^= 0xaa;
// 与口令进行异或操作
for (int i = 0; i < sizeof(buffer); i++) {
buffer[i] ^= password[i % sizeof(password)];
}
fwrite(buffer, sizeof(unsigned char), 32, output);
fclose(input);
fclose(output);
printf("解密完成\n");
}
int main() {
const char* inputFile = "./input.txt";
const char* encryptedFile = "./encrypted.txt";
const char* decryptedFile = "./decrypted.txt";
const char* password = "password";
encryptFile(inputFile, encryptedFile, password);
decryptFile(encryptedFile, decryptedFile, password);
return 0;
}
以上代码中,encryptFile函数用于加密文件,decryptFile函数用于解密文件。在main函数中,调用encryptFile函数将input.txt文件加密,并保存到encrypted.txt文件中;然后调用decryptFile函数将encrypted.txt文件解密,并保存到decrypted.txt文件中。
请注意,以上代码中的口令password是一个字符串,如果口令长度不足32个字节,则会重复使用口令的字符来进行异或操作。