Playfair密碼C語言源代碼_第1頁
Playfair密碼C語言源代碼_第2頁
Playfair密碼C語言源代碼_第3頁
Playfair密碼C語言源代碼_第4頁
Playfair密碼C語言源代碼_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、/PLAYFAIR.H頭文件#ifndef _PLAY_FAIR_H_#define _PLAY_FAIR_H_void cst_playfair_table (char *in_key);void prnt_playfair_table ();int playfair_encrypt (char *plain_txt, char *cipher_txt);int playfair_decrypt (char *cipher_txt, char *plain_txt);#endif/PLAYFAIR.C源文件#include playfair.h#include #include #inclu

2、de char playfair_table55;void get_x_y (char c, int *x, int *y)int i, j;for (i = 0; i 5; +i)for (j = 0; j 5; +j)if (playfair_tableij = c)*x = i; *y = j;return;char get_char_x_y (int x, int y)if (x 0)x += 5;if (y 0)y += 5;return playfair_tablex % 5y % 5;void cst_playfair_table (char *in_key)int i = 0,

3、 j, k;int char_count = 0, left_ch_index = 0;char ch, left_chs26 = 0, key26 = 0;int key_len;strcpy (key, in_key);key_len = strlen (key);while (1)if (keyi = )for (j = i; j key_len - 1; +j)keyj = keyj + 1;key_len-;if (i = key_len)break;i+;for (i = 0; i 1)for (k = j; k key_len -1; +k)keyk = keyk + 1;key

4、_len-;elsej+;elsej+;if (j = key_len)break;if (char_count = 0)left_chsleft_ch_index+ = ch;for (i = 0; i left_ch_index; +i)keykey_len+ = left_chsi;for (i = 0; i 26; +i)if (keyi = z)for (j = i; j 25; +j)keyj = keyj + 1;for (i = 0; i 5; +i)for (j = 0; j 5; +j)playfair_tableij = key5 * i + j;void prnt_pl

5、ayfair_table ()int i, j;printf (Playfair Table:n);for (i = 0; i 5; +i)printf (t);for (j = 0; j 5; +j)printf (%c , playfair_tableij);puts(n);int playfair_encrypt (char *plain_txt, char *cipher_txt)int length = strlen (plain_txt);int i = 0, j;int ax, ay, bx, by;char pch1, pch2, cch1, cch2;int ch1_uppe

6、r = 0, ch2_upper = 0;for (i = 0; i length; +i)if (plain_txti = z)plain_txti = x;if (length %2)plain_txtlength = x;length+;for (i = 0; i = A & pch1 = 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_

7、y (bx + 1, by);else if (ax = bx)cch1 = get_char_x_y (ax, ay + 1);cch2 = get_char_x_y (bx, by + 1);elsecch1 = get_char_x_y (ax, by);cch2 = get_char_x_y (bx, ay);cipher_txti = cch1 - 32 * ch1_upper;cipher_txti + 1 = cch2 - 32 *ch2_upper;cipher_txtlength = 0;return length;int playfair_decrypt (char *ci

8、pher_txt, char *plain_txt)int length = strlen (plain_txt);int i = 0, j;int ax, ay, bx, by;char pch1, pch2, cch1, cch2;int ch1_upper = 0, ch2_upper = 0;for (i = 0; i = A & cch1 = 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 (a

9、x - 1, ay);pch2 = get_char_x_y (bx - 1, by);else if (ax = bx)pch1 = get_char_x_y (ax, ay - 1);pch2 = get_char_x_y (bx, by - 1);elsepch1 = get_char_x_y (ax, by);pch2 = get_char_x_y (bx, ay);plain_txti = pch1 - 32 * ch1_upper;plain_txti + 1 = pch2 - 32 *ch2_upper;plain_txtlength = 0;return length;/MAI

10、N.C文件#include #include #include #include playfair.hint main ()char plain_txt1000, cipher_txt1000;char key26 = 0;printf (Please input your key (string): );scanf (%s, key);cst_playfair_table (key);printf (Keyword Table:n);prnt_playfair_table ();printf (Please input plain text: );scanf (%s, plain_txt);playfair_encrypt (plain_tx

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論