高分求用C++写一道完整的加密程序

2024-12-14 08:05:58
推荐回答(2个)
回答1:

#include
#include
#include
#include

void encrypt(char *in, char *pwd, char *out)
{
FILE *infile, *outfile;
char ch;
int i=0, PwdLen=0;
if ((infile=fopen(in, "rb"))==NULL)
{
printf("无法打开 %s\n", in);
getch();
exit(1);
}
if ((outfile=fopen(out, "wb"))==NULL)
{
printf("无法打开或创建 %s\n", out);
getch();
exit(1);
}
while (pwd[PwdLen++]); //这里用来应付密码为空的情况
PwdLen--;
ch=fgetc(infile);
while (!feof(infile))
{
fputc(ch^pwd[i>=PwdLen?i=0:i++], outfile);
ch=fgetc(infile);
}
fclose(infile);
fclose(outfile);
}

int main(int argc, char *argv[])
{
char in[256];
char out[256];
char pwd[256];
if (argc!=4)
{
printf("要加/解密的文件路径和文件名:\n");
gets(in);
printf("密码:\n");
gets(pwd);
printf("输出路径和文件名:\n");
gets(out);
encrypt(in, pwd, out);
printf("\n\n任务完成!\n\n");
getch();
}
else encrypt(argv[1], argv[2], argv[3]);
return 0;

回答2:

osa