XOR 仅部分解密文件
XOR only partially decrypts file
我正在学习 C,而且进展缓慢,因为我在自学。我正在尝试创建一个读取文件的小函数,XOR 逐字节对其进行加密,然后将其吐出。好像可以加密,但是解密的时候只有一半清晰,剩下的都是乱码。
int encrypt_file(void)
{
//declare variables used
FILE *src_file_ptr;
FILE *dst_file_ptr;
char infilename[96];
char key[50];
char outfilename[96];
char temp_store[50];
int plen;
printf("Please enter the name of the file to encrypt: ");
scanf("%s", infilename);
printf("Please enter the name of the output file: ");
scanf("%s", outfilename);
printf("Please enter the key to encrypt the file: ");
scanf("%s", key);
plen = strlen(key);
//test for existence of input file
src_file_ptr = fopen(infilename, "r");
if (src_file_ptr == NULL)
{
printf("File does not exist!");
return 1;
}
dst_file_ptr = fopen(outfilename, "a");
//read and process file 50 bytes at a time
while(fgets(temp_store, 50, src_file_ptr) != NULL)
{
int i;
//XOR n bytes with key
for ( i = 0; i < strlen(temp_store); i++)
{
temp_store[i] = temp_store[i] ^ key[i%plen];
fprintf(dst_file_ptr,"%c",temp_store[i]);
}
}
fclose(dst_file_ptr);
fclose(src_file_ptr);
return 0;
}
int main(void)
{
encrypt_file();
return 0;
}
我在这里对堆栈溢出进行了广泛的研究,我发现的所有内容都是用于 XOR 加密/解密设置值字符串的。我试图通过读取文件并向用户询问密码(密钥)来使其动态工作。任何帮助将不胜感激。
所以在头撞到墙上更加痛苦之后,在这里的人的帮助下,我找到了解决方案。
int encrypt_file(void)
{
//declare variables used
FILE *src_file_ptr;
FILE *dst_file_ptr;
char infilename[96];
char key[50];
char outfilename[96];
char temp_store[50];
int plen;
printf("Please enter the name of the input file: ");
scanf("%s", infilename);
printf("Please enter the name of the output file: ");
scanf("%s", outfilename);
printf("Please enter the key to perform encrypt / decrypt: ");
scanf("%s", key);
plen = strlen(key);
//test for existence of input file
src_file_ptr = fopen(infilename, "rb");
if (src_file_ptr == NULL)
{
printf("File does not exist!");
return 1;
}
dst_file_ptr = fopen(outfilename, "wb");
//create and initialize iterator
int i = 0;
do
{
//variable for new byte
char nb;
// check for end of file and exit if found
if (feof(src_file_ptr))
break;
//check where iterator is at. If same length as password, reset it.
if (i == plen)
{
i = 0;}
//read single character from file, in binary mode
char c = fgetc(src_file_ptr);
// combine file character with password character using XOR
nb = c ^ key[i];
//write new character to file as byte
fputc(nb, dst_file_ptr);
//increment to next letter in password
i++;
} while(1);
fclose(dst_file_ptr);
fclose(src_file_ptr);
return 0;
}
int main(void)
{
encrypt_file();
return 0;
}
这对文本文件非常有效,还没有尝试过二进制文件,但到目前为止我只需要纯文本。
我正在学习 C,而且进展缓慢,因为我在自学。我正在尝试创建一个读取文件的小函数,XOR 逐字节对其进行加密,然后将其吐出。好像可以加密,但是解密的时候只有一半清晰,剩下的都是乱码。
int encrypt_file(void)
{
//declare variables used
FILE *src_file_ptr;
FILE *dst_file_ptr;
char infilename[96];
char key[50];
char outfilename[96];
char temp_store[50];
int plen;
printf("Please enter the name of the file to encrypt: ");
scanf("%s", infilename);
printf("Please enter the name of the output file: ");
scanf("%s", outfilename);
printf("Please enter the key to encrypt the file: ");
scanf("%s", key);
plen = strlen(key);
//test for existence of input file
src_file_ptr = fopen(infilename, "r");
if (src_file_ptr == NULL)
{
printf("File does not exist!");
return 1;
}
dst_file_ptr = fopen(outfilename, "a");
//read and process file 50 bytes at a time
while(fgets(temp_store, 50, src_file_ptr) != NULL)
{
int i;
//XOR n bytes with key
for ( i = 0; i < strlen(temp_store); i++)
{
temp_store[i] = temp_store[i] ^ key[i%plen];
fprintf(dst_file_ptr,"%c",temp_store[i]);
}
}
fclose(dst_file_ptr);
fclose(src_file_ptr);
return 0;
}
int main(void)
{
encrypt_file();
return 0;
}
我在这里对堆栈溢出进行了广泛的研究,我发现的所有内容都是用于 XOR 加密/解密设置值字符串的。我试图通过读取文件并向用户询问密码(密钥)来使其动态工作。任何帮助将不胜感激。
所以在头撞到墙上更加痛苦之后,在这里的人的帮助下,我找到了解决方案。
int encrypt_file(void)
{
//declare variables used
FILE *src_file_ptr;
FILE *dst_file_ptr;
char infilename[96];
char key[50];
char outfilename[96];
char temp_store[50];
int plen;
printf("Please enter the name of the input file: ");
scanf("%s", infilename);
printf("Please enter the name of the output file: ");
scanf("%s", outfilename);
printf("Please enter the key to perform encrypt / decrypt: ");
scanf("%s", key);
plen = strlen(key);
//test for existence of input file
src_file_ptr = fopen(infilename, "rb");
if (src_file_ptr == NULL)
{
printf("File does not exist!");
return 1;
}
dst_file_ptr = fopen(outfilename, "wb");
//create and initialize iterator
int i = 0;
do
{
//variable for new byte
char nb;
// check for end of file and exit if found
if (feof(src_file_ptr))
break;
//check where iterator is at. If same length as password, reset it.
if (i == plen)
{
i = 0;}
//read single character from file, in binary mode
char c = fgetc(src_file_ptr);
// combine file character with password character using XOR
nb = c ^ key[i];
//write new character to file as byte
fputc(nb, dst_file_ptr);
//increment to next letter in password
i++;
} while(1);
fclose(dst_file_ptr);
fclose(src_file_ptr);
return 0;
}
int main(void)
{
encrypt_file();
return 0;
}
这对文本文件非常有效,还没有尝试过二进制文件,但到目前为止我只需要纯文本。