Playfair密码C语言源代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//PLAYFAIR.H头文件#ifndef_PLAY_FAIR_H_#define_PLAY_FAIR_H_voidcst_playfair_table(char*in_key);voidprnt_playfair_table();intplayfair_encrypt(char*plain_txt,char*cipher_txt);intplayfair_decrypt(char*cipher_txt,char*plain_txt);#endif//PLAYFAIR.C源文件#includeplayfair.h#includestdio.h#includestdlib.h#includestring.hcharplayfair_table[5][5];voidget_x_y(charc,int*x,int*y){inti,j;for(i=0;i5;++i){for(j=0;j5;++j){if(playfair_table[i][j]==c){*x=i;*y=j;return;}}}}charget_char_x_y(intx,inty){if(x0)x+=5;if(y0)y+=5;returnplayfair_table[x%5][y%5];}voidcst_playfair_table(char*in_key){inti=0,j,k;intchar_count=0,left_ch_index=0;charch,left_chs[26]={0},key[26]={0};intkey_len;strcpy(key,in_key);key_len=strlen(key);while(1){if(key[i]==''){for(j=i;jkey_len-1;++j)key[j]=key[j+1];key_len--;}if(i==key_len)break;i++;}for(i=0;i26;++i){ch='a'+i;j=0;char_count=0;while(1){if(key[j]==ch){char_count++;if(char_count1){for(k=j;kkey_len-1;++k)key[k]=key[k+1];key_len--;}elsej++;}elsej++;if(j==key_len)break;}if(char_count==0)left_chs[left_ch_index++]=ch;}for(i=0;ileft_ch_index;++i)key[key_len++]=left_chs[i];for(i=0;i26;++i){if(key[i]=='z'){for(j=i;j25;++j)key[j]=key[j+1];}}for(i=0;i5;++i){for(j=0;j5;++j)playfair_table[i][j]=key[5*i+j];}}voidprnt_playfair_table(){inti,j;printf(PlayfairTable:\n);for(i=0;i5;++i){printf(\t);for(j=0;j5;++j){printf(%c,playfair_table[i][j]);}puts(\n);}}intplayfair_encrypt(char*plain_txt,char*cipher_txt){intlength=strlen(plain_txt);inti=0,j;intax,ay,bx,by;charpch1,pch2,cch1,cch2;intch1_upper=0,ch2_upper=0;for(i=0;ilength;++i){if(plain_txt[i]=='z')plain_txt[i]=‘x’;}if(length%2){plain_txt[length]='x';length++;}for(i=0;ilength;i=i+2){pch1=plain_txt[i];pch2=plain_txt[i+1];ch1_upper=ch2_upper=0;if(pch1='A'&&pch1='Z'){ch1_upper=1;pch1+=32;}if(pch2='A'&&pch2='Z'){ch2_upper=1;pch2+=32;}get_x_y(pch1,&ax,&ay);get_x_y(pch2,&bx,&by);if(ay==by){cch1=get_char_x_y(ax+1,ay);cch2=get_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(bx,by+1);}else{cch1=get_char_x_y(ax,by);cch2=get_char_x_y(bx,ay);}cipher_txt[i]=cch1-32*ch1_upper;cipher_txt[i+1]=cch2-32*ch2_upper;}cipher_txt[length]='\0';returnlength;}intplayfair_decrypt(char*cipher_txt,char*plain_txt){intlength=strlen(plain_txt);inti=0,j;intax,ay,bx,by;charpch1,pch2,cch1,cch2;intch1_upper=0,ch2_upper=0;for(i=0;ilength;i=i+2){cch1=cipher_txt[i];cch2=cipher_txt[i+1];ch1_upper=ch2_upper=0;if(cch1='A'&&cch1='Z'){ch1_upper=1;cch1+=32;}if(cch2='A'&&cch2='Z'){ch2_upper=1;cch2+=32;}get_x_y(cch1,&ax,&ay);get_x_y(cch2,&bx,&by);if(ay==by){pch1=get_char_x_y(ax-1,ay);pch2=get_char_x_y(bx-1,by);}elseif(ax==bx){pch1=get_char_x_y(ax,ay-1);pch2=get_char_x_y(bx,by-1);}else{pch1=get_char_x_y(ax,by);pch2=get_char_x_y(bx,ay);}plain_txt[i]=pch1-32*ch1_upper;plain_txt[i+1]=pch2-32*ch2_upper;}plain_txt[length]='\0';returnlength;}//MAIN.C文件#includestdio.h#includestdlib.h#includestring.h#includeplayfair.hintmain(){charplain_txt[1000],cipher_txt[1000];charkey[26]={0};printf(Pleaseinputyourkey(string):);scanf(%s,key);cst_playfair_table(key);printf(KeywordTable:\n);prnt_playfair_table();printf(Pleaseinputplaintext:);scanf(%s,plain_txt);playfair_encrypt(plain_txt,cipher_txt);printf(Cipheris:%s\n,cipher_txt);playfair_decrypt(cipher_txt,plain_txt);printf(Afterdecryption,plaintextis:%s\n,plain_txt);return0;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功