欢迎来到天天文库
浏览记录
ID:37915286
大小:38.50 KB
页数:6页
时间:2019-06-02
《Playfair密码C语言源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、//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源文件#include"playfair.h"#include2、io.h>#include#includecharplayfair_table[5][5];voidget_x_y(charc,int*x,int*y){inti,j;for(i=0;i<5;++i){for(j=0;j<5;++j){if(playfair_table[i][j]==c){*x=i;*y=j;return;}}}}charget_char_x_y(intx,inty){if(x<0)x+=5;if(y<0)y+=5;returnplayfair_table[x%5][y%5];}voi3、dcst_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;j4、;++i){ch='a'+i;j=0;char_count=0;while(1){if(key[j]==ch){char_count++;if(char_count>1){for(k=j;k5、i];for(i=0;i<26;++i){if(key[i]=='z'){for(j=i;j<25;++j)key[j]=key[j+1];}}for(i=0;i<5;++i){for(j=0;j<5;++j)playfair_table[i][j]=key[5*i+j];}}voidprnt_playfair_table(){inti,j;printf("PlayfairTable:");for(i=0;i<5;++i){printf("t");for(j=0;j<5;++j){printf("%c",playfair_table[i6、][j]);}puts("");}}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;i7、gth++;}for(i=0;i='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=ge8、t_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(b
2、io.h>#include#includecharplayfair_table[5][5];voidget_x_y(charc,int*x,int*y){inti,j;for(i=0;i<5;++i){for(j=0;j<5;++j){if(playfair_table[i][j]==c){*x=i;*y=j;return;}}}}charget_char_x_y(intx,inty){if(x<0)x+=5;if(y<0)y+=5;returnplayfair_table[x%5][y%5];}voi
3、dcst_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;j4、;++i){ch='a'+i;j=0;char_count=0;while(1){if(key[j]==ch){char_count++;if(char_count>1){for(k=j;k5、i];for(i=0;i<26;++i){if(key[i]=='z'){for(j=i;j<25;++j)key[j]=key[j+1];}}for(i=0;i<5;++i){for(j=0;j<5;++j)playfair_table[i][j]=key[5*i+j];}}voidprnt_playfair_table(){inti,j;printf("PlayfairTable:");for(i=0;i<5;++i){printf("t");for(j=0;j<5;++j){printf("%c",playfair_table[i6、][j]);}puts("");}}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;i7、gth++;}for(i=0;i='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=ge8、t_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(b
4、;++i){ch='a'+i;j=0;char_count=0;while(1){if(key[j]==ch){char_count++;if(char_count>1){for(k=j;k5、i];for(i=0;i<26;++i){if(key[i]=='z'){for(j=i;j<25;++j)key[j]=key[j+1];}}for(i=0;i<5;++i){for(j=0;j<5;++j)playfair_table[i][j]=key[5*i+j];}}voidprnt_playfair_table(){inti,j;printf("PlayfairTable:");for(i=0;i<5;++i){printf("t");for(j=0;j<5;++j){printf("%c",playfair_table[i6、][j]);}puts("");}}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;i7、gth++;}for(i=0;i='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=ge8、t_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(b
5、i];for(i=0;i<26;++i){if(key[i]=='z'){for(j=i;j<25;++j)key[j]=key[j+1];}}for(i=0;i<5;++i){for(j=0;j<5;++j)playfair_table[i][j]=key[5*i+j];}}voidprnt_playfair_table(){inti,j;printf("PlayfairTable:");for(i=0;i<5;++i){printf("t");for(j=0;j<5;++j){printf("%c",playfair_table[i
6、][j]);}puts("");}}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;i7、gth++;}for(i=0;i='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=ge8、t_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(b
7、gth++;}for(i=0;i='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=ge
8、t_char_x_y(bx+1,by);}elseif(ax==bx){cch1=get_char_x_y(ax,ay+1);cch2=get_char_x_y(b
此文档下载收益归作者所有