Playfair密码C语言源代码(共6页).doc
精选优质文档-倾情为你奉上/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 <stdio.h>#include <stdlib.h>#include <string.h>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, 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 < 26; +i)ch = 'a' + i;j = 0;char_count = 0;while (1)if (keyj = ch)char_count+;if (char_count > 1)for (k = j; k < key_len -1; +k)keyk = keyk + 1;key_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_playfair_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_upper = 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 < length; i = i + 2)pch1 = plain_txti;pch2 = plain_txti + 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);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 *cipher_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 < length; i = i + 2)cch1 = cipher_txti;cch2 = cipher_txti + 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);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;/MAIN.C文件#include <stdio.h>#include <stdlib.h>#include <string.h>#include "playfair.h"int 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_txt, cipher_txt);printf ("Cipher is : %sn", cipher_txt);playfair_decrypt (cipher_txt, plain_txt);printf ("After decryption, plain text is :%sn", plain_txt);return 0;专心-专注-专业