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]!='