A. 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;
}
B. 凯撒密码 c语言
这个是算法
程序太多,给个算法你看下
C. 凯撒密码,C语言,求救!
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
int len = 0;
char ch;
char buf[256] = {0};
char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};
printf("Encode or Decode: ");
scanf("%c",&ch);
printf("please input your string: ");
fflush(stdin);
gets(buf);
len = strlen(buf);
switch (ch)
{
case 'e':
case 'E':
for (i=0;i<len;i++)
{
buf[i] = enc[buf[i] - 'a'];
}
break;
case 'd':
case 'D':
for (i=0;i<len;i++)
{
buf[i] = nor[i];
}
break;
default:
printf("wrong input!\n");
}
printf("<%s>\n",buf);
return 0;
}
D. C语言编程问题,凯撒密码
#include<stdio.h>
chars[]="(BTBU)isakeystate-,Sciences,Engineering,Law,Economics,History,PhilosophyandManagement.";
voidfun(char*s,intn)
{
inti=0;
while(s[i]!=0)
{
if(s[i]<='z'&&s[i]>='a')
{
s[i]+=n;
if(s[i]>'z')s[i]-=24;
}
elseif(s[i]<='Z'&&s[i]>='A')
{
s[i]+=n;
if(s[i]>'Z')s[i]-=24;
}
i++;
}
}
voidmain()
{
fun(s,3);
puts(s);
}
E. 凯撒密码的问题C语言
(2)kaiser加密算法
具体程序:
#include<stdio.h>
#include<conio.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);
}
.
希望能够帮助你 ^_^ 也希望能够选为最佳答案!
F. 请问一下这道C语言编程题怎么做
按照题目要求编写的用凯撒密码加密的C语言程序如下
#include<stdio.h>
int main(){
char s[80];
int offset,i;
fgets(s,80,stdin);
scanf("%d",&offset);
for(i=0;s[i]!='