#include #include #include typedef struct tagKeywordTable { char p; char c; }KeywordTable; void cst_keyword_table (char *key); void prnt_keyword_table (); int keyword_encrypt (char *plain_txt, char *cipher_txt); int keyword_decrypt (char *cipher_txt, char *plain_txt); KeywordTable keyword_table[26]; void cst_keyword_table (char *in_key) { int i = 0, j, k; int char_count = 0, left_ch_index = 0; char ch, left_chs[26] = {0}, key[26] = {0}; int key_len; strcpy (key, in_key); key_len = strlen (key); while (1) { if (key[i] == ‘ ‘) { for (j = i; j < key_len - 1; ++j) key[j] = key[j + 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 (key[j] == ch) { char_count++; if (char_count > 1) { for (k = j; k < key_len -1; ++k) key[k] = key[k + 1]; key_len--; } else j++; } else j++; if (j == key_len) break; } if (char_count == 0) left_chs[left_ch_index++] = ch; } for (i = 0; i < left_ch_index; ++i) key[key_len++] = left_chs[i]; for (i = 0; i < 26; ++i) { keyword_table[i].p = ‘a‘ + i; keyword_table[i].c = key[i]; } } void prnt_keyword_table () { int i; printf ("Plain:\t"); for (i = 0; i < 26; ++i) printf ("%c ", keyword_table[i].p); printf ("\nCipher:\t"); for (i = 0; i < 26; ++i) printf ("%c ", keyword_table[i].c); puts("\n"); } int keyword_encrypt (char *plain_txt, char *cipher_txt) { int length = strlen (plain_txt); int i; for (i = 0; i < length; ++i) { if (plain_txt[i] >= ‘a‘ && plain_txt[i] <= ‘z‘) { cipher_txt[i] = keyword_table[plain_txt[i] - ‘a‘].c; } else if (plain_txt[i] >= ‘A‘ && plain_txt[i] <= ‘Z‘) { cipher_txt[i] = keyword_table[plain_txt[i] - ‘A‘].c - 32; } else cipher_txt[i] = plain_txt[i]; } cipher_txt[length] = ‘\0‘; return length; } int main () { char plain_txt[1000], cipher_txt[1000]; char key[26] = {0}; printf ("请输入你的密钥: "); scanf ("%s", key); cst_keyword_table (key); printf ("标准字头密码体制的密表为:\n"); prnt_keyword_table (); printf ("现在输入明文: "); scanf ("%s", plain_txt); keyword_encrypt (plain_txt, cipher_txt); printf ("加密后的密文为 : %s\n", cipher_txt); return 0; } **************************************************************************************** 解密 #include #include #include typedef struct tagKeywordTable { char p; char c; }KeywordTable; void cst_keyword_table (char *key); void prnt_keyword_table (); int keyword_encrypt (char *plain_txt, char *cipher_txt); int keyword_decrypt (char *cipher_txt, char *plain_txt); KeywordTable keyword_table[26]; void cst_keyword_table (char *in_key) { int i = 0, j, k; int char_count = 0, left_ch_index = 0; char ch, left_chs[26] = {0}, key[26] = {0}; int key_len; strcpy (key, in_key); key_len = strlen (key); while (1) { if (key[i] == ‘ ‘) { for (j = i; j < key_len - 1; ++j) key[j] = key[j + 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 (key[j] == ch) { char_count++; if (char_count > 1) { for (k = j; k < key_len -1; ++k) key[k] = key[k + 1]; key_len--; } else j++; } else j++; if (j == key_len) break; } if (char_count == 0) left_chs[left_ch_index++] = ch; } for (i = 0; i < left_ch_index; ++i) key[key_len++] = left_chs[i]; for (i = 0; i < 26; ++i) { keyword_table[i].p = ‘a‘ + i; keyword_table[i].c = key[i]; } } void prnt_keyword_table () { int i; printf ("Plain:\t"); for (i = 0; i < 26; ++i) printf ("%c ", keyword_table[i].p); printf ("\nCipher:\t"); for (i = 0; i < 26; ++i) printf ("%c ", keyword_table[i].c); puts("\n"); } int keyword_decrypt (char *cipher_txt, char *plain_txt) { int length = strlen (cipher_txt); int i, j; for (i = 0; i < length; ++i) { if (cipher_txt[i] >= ‘a‘ && cipher_txt[i] <= ‘z‘) { for (j = 0; j < 26; ++j) { if (keyword_table[j].c == cipher_txt[i]) { plain_txt[i] = keyword_table[j].p; break; } } } else if (cipher_txt[i] >= ‘A‘ && cipher_txt[i] <= ‘Z‘) { for (j = 0; j < 26; ++j) { if (keyword_table[j].c == cipher_txt[i] + 32) { plain_txt[i] = keyword_table[j].p - 32; break; } } } else plain_txt[i] = cipher_txt[i]; } plain_txt[length] = ‘\0‘; return length; } int main () { char plain_txt[1000], cipher_txt[1000]; char key[26] = {0}; printf ("请输入你的密钥: "); scanf ("%s", key); cst_keyword_table (key); printf ("标准字头密码体制的密表为:\n"); prnt_keyword_table (); printf ("输入原密文: "); scanf ("%s", cipher_txt); keyword_decrypt (cipher_txt, plain_txt); printf ("则原明文为:%s\n", plain_txt); return 0; } ****************************************************************************************
时间: 2024-11-05 11:35:47