供参考 译码只需要一句即可
感兴趣自己分析下
#include stdio.h
int main()
{
char s[100];
int k;
int i;
while(~scanf("%s%d", s, k))
{
k%=26;
for(i = 0; s[i]; i ++)
s[i] = (s[i] - 'A' + 26 - (k+i + 1)%26)%26 + 'A';
printf("%s\n", s);
}
return 0;
}
#includestdio.h
#includestring.h
int main()
{
int i;int number;
char a[100];
scanf("%s",a);
number=strlen(a);
for(i=0;inumber;i++){
a[i]=a[i]+4;
}
for(i=0;inumber;i++){
printf("%c",a[i]);
}
return 0;
}
#includestdio.h
#includestring.h
void main ()
{
char str[100];
char str1[100];
printf("输入字符串:");
scanf("%s",str);
int len;
len=strlen(str);
for(int i=0;i<len;i++)
{
str1[i]=(str[i]-97+3)%26+97;
}
str1[len]='\0';
printf ("密文为:%s\n",str1);
}
凯撒密码就是简单的加上一个数,'a'+3='d';'z'+3='c' 假设原文全是小写字母,那么 char plain[N]={...}; //明文 char cipher[N]={};//密文 int key=3; int i=0,temp; for(i=0;iN;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i]=temp+'a'; } else cipher[i]=plain[i]; } 这样就完成了加密,密文数组里面就是对原文加密后的密文,key是密钥。
(2)kaiser加密算法
具体程序:
#includestdio.h
#includeconio.h
char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch='A'ch='Z')
{
return ('A'+(ch-'A'+n)%26);
}
while(ch='a'ch='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;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
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();
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();
printf("\nGood Bye!\n");
sleep(3);
}
.
希望能够帮助你 ^_^ 也希望能够选为最佳答案!
写的一般般,希望对LZ有所帮助
#include stdio.h
#include string.h
int main()
{
char str[201];//存放字符
char tmp[11];//临时变量
int i;//循环变量
int len;//存放消息长度
scanf("%s",tmp);//这里输入START,开始
getchar();//接收回车
while(strcmp(tmp,"ENDOFINPUT"))
{
gets(str);//由于输入中有空格,所以用gets输入
getchar();//接收回车
len = strlen(str);
for(i=0;ilen;i++)
{
if(str[i]='A'str[i]='Z')
{
str[i] -= 5 ;
if(str[i] 65)
{
str[i] +=26;
}
}
}
scanf("%s",tmp);//这里输入END,结束
printf("%s\n",str);//处理完就直接输出结果
scanf("%s",tmp);//输入START表示继续,输入ENDOFINPUT则表示最后一个数据集
getchar();//接收回车
}
return 0;
}