用C++写一个对文本文件加密的程序和一个解密的程序。急用呀!

2024-11-27 17:54:52
推荐回答(2个)
回答1:

/*abc.cpp*/
#include

int jiami(char *file1, char *file2)
{
FILE *fp1,*fp2;
char ch;

fp1=fopen(file1,"r");
if(!fp1) return 0;
fp2=fopen(file2,"w");
if(!fp2) return 0;
fseek(fp1,0,0);
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
if(ch>='a'&&ch<='c'||ch>='A'&&ch<='C') ch=ch+'x'-'a';
else ch=ch-'d'+'a';
putchar(ch);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return 1;
}
int jiemi(char *file1, char *file2)
{
FILE *fp1,*fp2;
char ch;

fp1=fopen(file1,"r");
if(!fp1) return 0;
fp2=fopen(file2,"w");
if(!fp2) return 0;
fseek(fp1,0,0);
while(!feof(fp1))
{
ch=fgetc(fp1);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
if(ch>='x'&&ch<='z'||ch>='X'&&ch<='Z') ch=ch-'x'+'a';
else ch=ch+'d'-'a';
putchar(ch);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
return 1;
}

int main( )
{ int ok;
printf("Text of JiaMi:\n");
ok=jiami("abc.cpp","abc1.cpp");
if(!ok) printf("Encrypt Failure!\n");
printf("Text of jieMi:\n");
ok=jiemi("abc1.cpp","abc2.cpp");
if(!ok) printf("Decrypt Failure!\n");
return 0;
}
加解密程序都在里面了,在VC6下调试通过.

回答2:

//加密
#include
#include
using namespace std;

int main(){
ifstream ifile("e:\\mima.txt");
ofstream ofile("e:\\mima2.txt");
char temp;
while(ifile.get(temp)){
if(temp=='a'||temp=='b'||temp=='c')
temp+=23;
else if(temp>='d'&&temp<='z')temp-=3;
else if(temp=='A'||temp=='B'||temp=='C')
temp+=23;
else if(temp>='D'&&temp<='Z')temp-=3;
ofile< }
ifile.close();
ofile.close();
}

//解密
#include
#include
using namespace std;

int main(){
ifstream ifile("e:\\mima2.txt");
ofstream ofile("e:\\mima22.txt");
char temp;
while(ifile.get(temp)){
if(temp=='x'||temp=='y'||temp=='z')
temp-=23;
else if(temp>='a'&&temp<='w')temp+=3;
else if(temp=='X'||temp=='Y'||temp=='Z')
temp-=23;
else if(temp>='A'&&temp<='W')temp+=3;
ofile< }
ifile.close();
ofile.close();
}