c语言加密解密算法
答案:1 悬赏:60 手机版
解决时间 2021-03-07 17:10
- 提问者网友:抽煙菂渘情少年
- 2021-03-06 18:05
c语言加密解密算法
最佳答案
- 五星知识达人网友:夜余生
- 2021-03-06 19:04
这里使用的是按位加密,按ASCII码进行加密的算法自己写个,很容易的。
#include
#include
#include
#include
void dofile(char *in_fname,char *pwd,char *out_fname);
void usage(char *name);
void main(int argc,char *argv[])
{
char in_fname[30];
char out_fname[30];
char pwd[10];
if(argc!=4)
{
usage(argv[0]);
printf("\nIn-fname:\n");
gets(in_fname);
while(*in_fname==NULL)
{
printf("\nIn-fname:\n");
gets(in_fname);
}
printf("Password 6-8:\n");
gets(pwd);
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password 6-8:\n");
gets(pwd);
}
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
else
{
strcpy(in_fname,argv[1]);
strcpy(pwd,argv[2]);
strcpy(out_fname,argv[3]);
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password faied!\n");
printf("Password 6-8:\n");
gets(pwd);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
}
void dofile(char *in_fname,char *pwd,char *out_file)
{
FILE *fp1,*fp2;
register char ch;
int j=0;
int j0=strlen(pwd);
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot open in-file.\n");
exit(1);
}
fp2=fopen(out_file,"wb");
if(fp2==NULL)
{
printf("cannot open or create out-file.\n");
exit(1);
}
while(j0>=0)
{
ch=fgetc(fp1);
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2);
ch=fgetc(fp1);
}
j0--;
}
fclose(fp1);
fclose(fp2);
}
void usage(char *name)
{
printf("\t=======================File encryption======================\n");
printf("\tusage: %s In-fname password out_fname\n",name);
printf("\tExample: %s file1.txt 12345678 file2.txt\n",name);
}
#include
#include
#include
#include
void dofile(char *in_fname,char *pwd,char *out_fname);
void usage(char *name);
void main(int argc,char *argv[])
{
char in_fname[30];
char out_fname[30];
char pwd[10];
if(argc!=4)
{
usage(argv[0]);
printf("\nIn-fname:\n");
gets(in_fname);
while(*in_fname==NULL)
{
printf("\nIn-fname:\n");
gets(in_fname);
}
printf("Password 6-8:\n");
gets(pwd);
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password 6-8:\n");
gets(pwd);
}
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
else
{
strcpy(in_fname,argv[1]);
strcpy(pwd,argv[2]);
strcpy(out_fname,argv[3]);
while(*pwd==NULL || strlen(pwd)>8 || strlen(pwd)<6)
{
printf("Password faied!\n");
printf("Password 6-8:\n");
gets(pwd);
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n");
printf("Out-file:\n");
gets(out_fname);
while(*in_fname==NULL)
{
printf("Out-file:\n");
gets(out_fname);
}
}
dofile(in_fname,pwd,out_fname);
printf("加密成功,解密请再次运行程序\n");
}
}
void dofile(char *in_fname,char *pwd,char *out_file)
{
FILE *fp1,*fp2;
register char ch;
int j=0;
int j0=strlen(pwd);
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot open in-file.\n");
exit(1);
}
fp2=fopen(out_file,"wb");
if(fp2==NULL)
{
printf("cannot open or create out-file.\n");
exit(1);
}
while(j0>=0)
{
ch=fgetc(fp1);
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2);
ch=fgetc(fp1);
}
j0--;
}
fclose(fp1);
fclose(fp2);
}
void usage(char *name)
{
printf("\t=======================File encryption======================\n");
printf("\tusage: %s In-fname password out_fname\n",name);
printf("\tExample: %s file1.txt 12345678 file2.txt\n",name);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯