c语言凯撒加密函数实现 c语言凯撒密码编程简单
C语言的凯撒加密
/*
札达网站建设公司创新互联,札达网站设计制作,有大型网站制作公司丰富经验。已为札达千余家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的札达做网站的公司定做!
和楼上的相比,或许 看上去很烦
ch[i] +=5;
if (ch[i] 'Z')
{
ch[i] -= 26;
}
可以改成和 楼上的 方法
等价于 ch[i] = 'A' + (ch[i] - 'A' + 5) % 26;
*/
# include stdio.h
# include stdlib.h //用到了system(); 不写 ,可以用 getchar();
#define strwidth 117 //定义长度
int main(void)
{
char ch[strwidth];
int i ;
printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格
for (i = 0; i strwidth ;i++ )
{
if (ch[i] = 'a' ch[i] = 'z' ) //判断是否小写字母
{
ch[i] +=5;
if (ch[i] 'z') //不解释,我想这样,理解可能会方便点吧
{
ch[i] -= 26;
}
}
else if ( ch[i] = 'A' ch[i] = 'Z') //判断是否大写字母
{
ch[i] +=5;
if (ch[i] 'Z')
{
ch[i] -= 26;
}
}
}
printf("加密后为:%s\n" , ch); //输出数据
system("pause");
return 0;
}
/*
或者 这样
*/
# include stdio.h
# include stdlib.h //用到了system(); 不写 ,可以用 getchar();
#define strwidth 117 //定义长度
int main(void)
{
char ch[strwidth];
int i ;
printf("请输入密码:");
gets(ch); //输入数据,用gets(); 保留了空格
for (i = 0; i strwidth ;i++ )
{
if (ch[i] = 'a' ch[i] = 'z' || ch[i] = 'A' ch[i] = 'Z' ) //判断是否是字母
{
ch[i] +=5;
if ( ch[i]'Z' ch[i] = 'Z' + 5 || ch[i] 'z' )
{
ch[i] -= 26;
}
}
}
printf("加密后为:%s\n" , ch); //输出数据
system("pause");
return 0;
}
凯撒密码的算法c语言的怎么实现啊?
凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。他的原理很简单,说到底就是字母于字母之间的替换。下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。
#include stdio.h
main()
{
char M[100];
char C[100];
int K=3,i;
printf("请输入明文M(注意不要输入空白串)\n");
gets(M);
for(i=0;M[i]!='\0';i++)
C[i]=(M[i]-'a'+K)%26+'a';
C[i]='\0';
printf("结果是:\n%s\n",C);
}
求凯撒加密法(C语言)
#includestdio.h
#includeconio.h char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch=Ach=Z)
{
return (A+(ch-A+n)%26);
}
while(ch=ach=z)
{
return (a+(ch-a+n)%26);
}
return ch;
}void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n===============================================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("===============================================================================\n");
printf("Please select a item:");
return;
}void logo()/*显示版权信息*/
{
printf("\nZhensoft Encryption [Version:1.0.0]");
printf("\nCopyright (C) 2004 Zhensoft Corp.\n");
printf("\n \n");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();logo();
sleep(3);/*等待3秒*/menu();
ch0=getch();while(ch0!=4)
{
if(ch0==1)
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要加密的文件名*/ if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
} printf("Please input the key:");
scanf("%d",n);/*输入加密密码*/ printf("Please input the outfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
} while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
} printf("\nEncrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
} if(ch0==2)
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
} printf("Please input the key:");
scanf("%d",n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n; printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
} while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nDecrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
} if(ch0==3)
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/ if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
} printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/ if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
} for(i=1;i=25;i++)/*暴力破解过程,在察看信息正确后,可以按Q或者q退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("===============================================================================\n");
printf("The outfile is:\n");
printf("===============================================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n===============================================================================\n");
printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
printf("Press Q to quit and other key to continue......\n");
printf("===============================================================================\n");
ch1=getch();
if(ch1==q||ch1==Q)/*按Q或者q时退出*/
{
clrscr();
logo();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
} printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
logo();
printf("\nGood Bye!\n");
sleep(3);
}
文章标题:c语言凯撒加密函数实现 c语言凯撒密码编程简单
当前网址:http://abwzjs.com/article/hjdiip.html