fdf
dfsfsunknown
c_cpp
4 years ago
4.3 kB
5
Indexable
#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define ALPHA "ABCDEFGHIJKLMNOPQRSTUVXYZ" void playfair_table(char key[], char table[5][5]) { int r = 0; int s = 0; int count; for (size_t j = 0; j < strlen(key); j++) { table[r][s] = key[j]; s++; if (s == 5) { s = 0; r++; } } for (size_t i = 0; i < 26; i++) { count = 0; for (size_t j = 0; j < strlen(key); j++) if (ALPHA[i] == key[j]) count++; if (count == 1) continue; else { table[r][s] = ALPHA[i]; s++; if (s == 5) { s = 0; r++; } } } } void find_pos(char table[5][5], char posc, int pos[]) { for (size_t i = 0; i < 5; i++) { for (size_t j = 0; j < 5; j++) { if (posc == table[i][j]) { pos[0] = i; pos[1] = j; } } } } void sipher(char table[5][5], char encrypt[], int pos1[], int pos2[]) { if (pos1[0] == pos2[0]) { if (pos1[1] == 4) encrypt[0] = table[pos1[0]][0]; else encrypt[0] = table[pos1[0]][pos1[1] + 1]; if (pos2[1] == 4) encrypt[1] = table[pos2[0]][0]; else encrypt[1] = table[pos2[0]][pos2[1] + 1]; } else if (pos1[1] == pos2[1]) { if (pos1[0] == 4) encrypt[0] = table[0][pos1[1]]; else encrypt[0] = table[pos1[0] + 1][pos1[1]]; if (pos2[0] == 4) encrypt[1] = table[0][pos2[1]]; else encrypt[1] = table[pos2[0] + 1][pos2[1]]; } else { encrypt[0] = table[pos1[0]][pos2[1]]; encrypt[1] = table[pos2[0]][pos1[1]]; } } char *playfair_encrypt(const char *key, const char *text) { if (key == NULL || text == NULL || text[0] == '\0' || key[0] == '\0') return NULL; // PREMENNE char chan_text[strlen(text) + 50]; char chan_key[strlen(key) + 50]; size_t text_in = 0; size_t key_in = 0; int count; char table[5][5]; int pos_a[2]; int pos_b[2]; char encrypt[2]; // uprava textu for (size_t i = 0; i < strlen(text); i++, text_in++) { if (isalpha(text[i]) == 0) if (text[i] != ' ') return NULL; if (text[i] == ' ') { text_in--; continue; } chan_text[text_in] = toupper(text[i]); if (chan_text[text_in] == 'W') chan_text[text_in] = 'V'; if ((chan_text[text_in] == chan_text[text_in - 1]) && chan_text[text_in] != 'X') { chan_text[text_in] = 'X'; text_in++; chan_text[text_in] = chan_text[text_in - 2]; } } if (text_in % 2 != 0) { chan_text[text_in] = 'X'; text_in++; } chan_text[text_in] = '\0'; //uprava key for (size_t i = 0; i < strlen(key); i++, key_in++) { count = 0; if (isalpha(key[i]) == 0) if (key[i] != ' ') return NULL; if (key[i] == ' ') { key_in--; continue; } if (i > 1) for (size_t j = i - 1; j > 0; j--) if (key[i] == key[j]) count++; if (count == 1) { key_in--; continue; } chan_key[key_in] = toupper(key[i]); if (chan_key[key_in] == 'W') chan_key[key_in] = 'V'; } chan_key[key_in] = '\0'; //vygenerovanie tabulky s klucovym slovom playfair_table(chan_key, table); // magic int fin_len = strlen(chan_text) + strlen(chan_text) / 2; char *playfair = calloc(fin_len, sizeof(char)); for (size_t i = 0, j = 0; i < strlen(chan_text); i++, j++) { find_pos(table, chan_text[i], pos_a); i++; find_pos(table, chan_text[i], pos_b); sipher(table, encrypt, pos_a, pos_b); playfair[j] = encrypt[0]; j++; playfair[j] = encrypt[1]; j++; if (j != fin_len) playfair[j] = ' '; } playfair[fin_len] = '\0'; return playfair; }
Editor is loading...