用C++写一个对文本文件加密的程序和一个解密的程序。急用呀!
用C++写一个对文本文件加密的程序和一个解密的程序。密码规则是:对于小写字母,a换成x,b换成y,c换成z,d换成a,e换成b,...;对于大写字母,A换成X,B换成Y,...
用C++写一个对文本文件加密的程序和一个解密的程序。密码规则是:对于小写字母,a换成x,b换成y,c换成z,d换成a,e换成b,...;对于大写字母,A换成X,B换成Y,C换成Z,D换成A,E换成B,...;其他字符不变。
展开
2个回答
展开全部
/*abc.cpp*/
#include <stdio.h>
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下调试通过.
#include <stdio.h>
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下调试通过.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//加密
#include<iostream>
#include<fstream>
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<<temp;
}
ifile.close();
ofile.close();
}
//解密
#include<iostream>
#include<fstream>
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<<temp;
}
ifile.close();
ofile.close();
}
#include<iostream>
#include<fstream>
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<<temp;
}
ifile.close();
ofile.close();
}
//解密
#include<iostream>
#include<fstream>
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<<temp;
}
ifile.close();
ofile.close();
}
参考资料: 自己的大脑
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询