c語言凱撒加密函數(shù)實(shí)現(xiàn) c語言凱撒密碼編程簡單

C語言的凱撒加密

/*

札達(dá)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),札達(dá)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為札達(dá)千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的札達(dá)做網(wǎng)站的公司定做!

和樓上的相比,或許 看上去很煩

ch[i] +=5;

if (ch[i] 'Z')

{

ch[i] -= 26;

}

可以改成和 樓上的 方法

等價(jià)于 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); //輸入數(shù)據(jù),用gets(); 保留了空格

for (i = 0; i strwidth ;i++ )

{

if (ch[i] = 'a' ch[i] = 'z' ) //判斷是否小寫字母

{

ch[i] +=5;

if (ch[i] 'z') //不解釋,我想這樣,理解可能會(huì)方便點(diǎn)吧

{

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); //輸出數(shù)據(jù)

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); //輸入數(shù)據(jù),用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); //輸出數(shù)據(jù)

system("pause");

return 0;

}

凱撒密碼的算法c語言的怎么實(shí)現(xiàn)???

凱撒密碼是一種非常古老的加密方法,相傳當(dāng)年凱撒大地行軍打仗時(shí)為了保證自己的命令不被敵軍知道,就使用這種特殊的方法進(jìn)行通信,以確保信息傳遞的安全。他的原理很簡單,說到底就是字母于字母之間的替換。下面讓我們看一個(gè)簡單的例子:“baidu”用凱撒密碼法加密后字符串變?yōu)椤癳dlgx”,它的原理是什么呢?把“baidu”中的每一個(gè)字母按字母表順序向后移3位,所得的結(jié)果就是剛才我們所看到的密文。

#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("結(jié)果是:\n%s\n",C);

}

求凱撒加密法(C語言)

#includestdio.h

#includeconio.h char encrypt(char ch,int n)/*加密函數(shù),把字符向右循環(huá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.暴力破解,密碼只能是數(shù)字*/

{

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()/*顯示版權(quán)信息*/

{

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);/*輸入解密密碼(可以為加密時(shí)候的密碼)*/ 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);/*顯示當(dāng)前破解所用密碼*/

printf("Press Q to quit and other key to continue......\n");

printf("===============================================================================\n");

ch1=getch();

if(ch1==q||ch1==Q)/*按Q或者q時(shí)退出*/

{

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);

}

新聞標(biāo)題:c語言凱撒加密函數(shù)實(shí)現(xiàn) c語言凱撒密碼編程簡單
文章源于:http://muchs.cn/article8/hjdiip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站改版、用戶體驗(yàn)、定制網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司