C 异或加密失败
回答:C 异或加密失败,c,encryption,xor,C,Encryption,Xor,回答: int encrypt_file(const char *filename, const char *key) { int i = 0; size_t key_len = strlen(key); size_t key_count = 0; size_t num_bytes = 0; int *data = NULL; int byte = 0; FILE *file;
int encrypt_file(const char *filename, const char *key)
{
int i = 0;
size_t key_len = strlen(key);
size_t key_count = 0;
size_t num_bytes = 0;
int *data = NULL;
int byte = 0;
FILE *file;
fopen_s(&file, filename, "r");
if ( file != NULL )
{
// get file size / number of bytes
fseek(file, 0L, SEEK_END);
num_bytes = ftell(file);
fseek(file, 0L, SEEK_SET);
// allocate enough memory for the data
data = (int*)malloc(sizeof(int) *num_bytes);
// stores the data from the file in the array
while ( (data[i++] = fgetc(file)) != EOF );
// encrypt the data
for ( i = 0; i < num_bytes; i++ ) {
data[i] = data[i]^key[key_count++];
if ( key_count == key_len ) {
key_count = 0;
}
}
fclose(file);
fopen_s(&file, filename, "w");
// write the data from the array to the same file
for ( i = 0; i < num_bytes; i++ ) {
fputc(data[i], file);
}
fclose(file);
return 0;
}
else
{
return 1;
}
}
我必须创建一个单独的解密函数,使用“rb”打开加密文件,然后在加密函数中使用“wb”将加密数据写入文件
我的Xor加密有问题。当加密文件中的数据时,加密工作正常,但当我尝试解密时,加密失败。问题是
fgetc
函数只读取第一行和第二行,无法解密第二行的50%
示例:
正常:
int encrypt_file(const char *filename, const char *key)
{
int i = 0;
size_t key_len = strlen(key);
size_t key_count = 0;
size_t num_bytes = 0;
int *data = NULL;
int byte = 0;
FILE *file;
fopen_s(&file, filename, "r");
if ( file != NULL )
{
// get file size / number of bytes
fseek(file, 0L, SEEK_END);
num_bytes = ftell(file);
fseek(file, 0L, SEEK_SET);
// allocate enough memory for the data
data = (int*)malloc(sizeof(int) *num_bytes);
// stores the data from the file in the array
while ( (data[i++] = fgetc(file)) != EOF );
// encrypt the data
for ( i = 0; i < num_bytes; i++ ) {
data[i] = data[i]^key[key_count++];
if ( key_count == key_len ) {
key_count = 0;
}
}
fclose(file);
fopen_s(&file, filename, "w");
// write the data from the array to the same file
for ( i = 0; i < num_bytes; i++ ) {
fputc(data[i], file);
}
fclose(file);
return 0;
}
else
{
return 1;
}
}
加密的:
a¦_ÖÞ`×ûù‡ûÛ(‹Pñ»FŒ§U®7!¼ªãŸ<çϱ\Î8ðs6Öã`GÒFAªÓV/Ç1t
我用一个断点检查了代码,发现问题是fgetc
在第二行之后停止读取文件,但我不知道为什么。也许我的算法有问题
代码:
int encrypt_file(const char *filename, const char *key)
{
int i = 0;
size_t key_len = strlen(key);
size_t key_count = 0;
size_t num_bytes = 0;
int *data = NULL;
int byte = 0;
FILE *file;
fopen_s(&file, filename, "r");
if ( file != NULL )
{
// get file size / number of bytes
fseek(file, 0L, SEEK_END);
num_bytes = ftell(file);
fseek(file, 0L, SEEK_SET);
// allocate enough memory for the data
data = (int*)malloc(sizeof(int) *num_bytes);
// stores the data from the file in the array
while ( (data[i++] = fgetc(file)) != EOF );
// encrypt the data
for ( i = 0; i < num_bytes; i++ ) {
data[i] = data[i]^key[key_count++];
if ( key_count == key_len ) {
key_count = 0;
}
}
fclose(file);
fopen_s(&file, filename, "w");
// write the data from the array to the same file
for ( i = 0; i < num_bytes; i++ ) {
fputc(data[i], file);
}
fclose(file);
return 0;
}
else
{
return 1;
}
}
int-encrypt_文件(常量字符*文件名,常量字符*密钥)
{
int i=0;
尺寸(键)长度=strlen(键);
大小键计数=0;
大小\u t数量\u字节=0;
int*data=NULL;
int字节=0;
文件*文件;
fopen_s(&file,文件名,“r”);
如果(文件!=NULL)
{
//获取文件大小/字节数
fseek(文件,0L,SEEK\u结束);
num_bytes=ftell(文件);
fseek(文件,0L,搜索集);
//为数据分配足够的内存
数据=(int*)malloc(sizeof(int)*num_字节);
//将文件中的数据存储在阵列中
而((数据[i++]=fgetc(文件))!=EOF);
//加密数据
对于(i=0;i
由于加密数据不再是文本,因此在打开I/O加密文件时应使用“wb”
和“rb”
。由于加密数据不再是文本,因此在打开I/O加密文件时应使用“wb”
和“rb”
。应使用“wb”
而不是在打开输出文件时使用“w”,因为它不再是文本。@timra使用“wb”是正确的。此外,请确保使用“rb”打开解密文件。打开输出文件时,应使用“wb”
而不是“w”
,因为它不再是文本。@timra使用“wb”是正确的。另外,请确保您的解密文件是使用“rb”打开的。仅供个人参考,是什么导致了此故障?ASCII字符的表示形式是否更改为EOL或EOF字符?是的,加密数据中可能存在任何值,包括EOF
,如果文件以文本模式打开,则会混淆fgetc()
。仅供个人参考,究竟是什么导致了此故障?ASCII字符的表示形式是否更改为EOL或EOF字符?是的,加密数据中可能存在任何值,包括EOF
,如果文件以文本模式打开,则会混淆fgetc()
。