谁帮我看看这段加密解密的代码哪里错了,解密的时候发现数据丢失!
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
int prin_size1()
{
FILE *p = fopen("F:\\cs.txt", "rb");
int size = 0;
int ch;
if (p != NULL)
{
while ((ch = getc(p)) != EOF)
{
size++;
}
}
fclose(p);
return size;
}
void mainasd()//分段加密
{
FILE *pf = fopen("F:\\cs.txt", "rb");
FILE *pfw = fopen("F:\\cs(加密).txt", "wb");
char str[100] = { 0 };
if (pf == NULL || pfw == NULL)
{
printf("文件打开失败\n");
}
else
{
int size = 0;
int length = 0;
printf("请输入加密的密码:");
scanf("%s", str);
length = strlen(str);
size = prin_size1();
printf("size=%d\n", size);
if (size%length == 0)
{
for (int i = 0; i < size / length; i++)
{
for (int j = 0; j < length; j++)
{
fputc(fgetc(pf) ^ str[j], pfw);
}
}
}
else
{
for (int i = 0; i < size / length - 1; i++)
{
for (int j = 0; j < length; j++)
{
fputc(fgetc(pf) ^ str[j], pfw);
}
}
for (int i = 0; i < size%length; i++)
{
fputc(fgetc(pf) ^ str[i], pfw);
}
}
}
fclose(pf);
fclose(pfw);
system("pause");
}
void main()//分段解密
{
FILE *pf = fopen("F:\\cs(加密).txt", "rb");
FILE *pfw = fopen("F:\\cs(解密).txt", "wb");
char str[100] = { 0 };
if (pf == NULL || pfw == NULL)
{
printf("文件打开失败\n");
}
else
{
int size = 0;
int length = 0;
printf("请输入解密的密码:");
scanf("%s", str);
length = strlen(str);
size = prin_size1();
if (size%length == 0)
{
for (int i = 0; i < size / length; i++)
{
for (int j = 0; j < length; j++)
{
fputc(fgetc(pf) ^ str[j], pfw);
}
}
}
else
{
for (int i = 0; i < size / length - 1; i++)
{
for (int j = 0; j < length; j++)
{
fputc(fgetc(pf) ^ str[j], pfw);
}
}
for (int i = 0; i < size%length; i++)
{
fputc(fgetc(pf) ^ str[i], pfw);
}
}
}
fclose(pf);
fclose(pfw);
system("pause");
}