From 79bb71732b568d6901b522aed9cb86885d7e8f12 Mon Sep 17 00:00:00 2001 From: mnjx <111888944+mnjx@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:44:42 +0100 Subject: [PATCH] Added cipher programs --- caesar-cipher-bruteforce.c | 60 ++++++++++++++++++ caesar-cipher.c | 39 ++++++++++++ polyalphabetic-cipher.c | 127 +++++++++++++++++++++++++++++++++++++ vernam-cipher.c | 46 ++++++++++++++ 4 files changed, 272 insertions(+) create mode 100644 caesar-cipher-bruteforce.c create mode 100644 caesar-cipher.c create mode 100644 polyalphabetic-cipher.c create mode 100644 vernam-cipher.c diff --git a/caesar-cipher-bruteforce.c b/caesar-cipher-bruteforce.c new file mode 100644 index 0000000..325f1fe --- /dev/null +++ b/caesar-cipher-bruteforce.c @@ -0,0 +1,60 @@ +#include +#include +#include + +void encode(char message[]) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + + for (int k = 1; k < 26; k++) { + for (int i = 0; i < strlen(message); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == message[i]) { + message[i] = alphabet[j+k]; + break; + } + } + } + printf("\nEncoded with shift %d: %s\n", k, message); + } +} + +void decode(char message[]) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + + for (int k = 1; k < 26; k++) { + for (int i = 0; i < strlen(message); i++) { + for (int j = 26; j < 52; j++) { + if (alphabet[j] == message[i]) { + message[i] = alphabet[j-k]; + break; + } + } + } + printf("\nDecoded with shift %d: %s\n", k, message); + } +} + +int main() { + char ar[1000]; + printf("Enter the message\n"); + scanf("%[^\n]", &ar); + + int option = -1; + printf("Encode or decode? 0 or 1\n"); + scanf("%d", &option); + + while (option == 0 || option == 1) { + switch (option) { + case 0: + encode(ar); + option = -1; + break; + case 1: + decode(ar); + option = -1; + break; + default: + printf("Please enter a valid number.\n"); + } + } +} \ No newline at end of file diff --git a/caesar-cipher.c b/caesar-cipher.c new file mode 100644 index 0000000..462fbc7 --- /dev/null +++ b/caesar-cipher.c @@ -0,0 +1,39 @@ +#include +#include +#include + +void encode(char message[], int shift) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + + for (int i = 0; i < strlen(message); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == message[i]) { + message[i] = alphabet[j+shift]; + break; + } + } + } +} + +void decode(char message[], int shift) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + + for (int i = 0; i < strlen(message); i++) { + for (int j = 26; j < 52; j++) { + if (alphabet[j] == message[i]) { + message[i] = alphabet[j-shift]; + break; + } + } + } +} + +int main() { + char ar[1000]; + scanf("%[^\n]", &ar); + encode(ar, 25); + printf("Encoded: %s\n", ar); + + decode(ar, 25); + printf("\nDecoded: %s\n", ar); +} \ No newline at end of file diff --git a/polyalphabetic-cipher.c b/polyalphabetic-cipher.c new file mode 100644 index 0000000..706af87 --- /dev/null +++ b/polyalphabetic-cipher.c @@ -0,0 +1,127 @@ +#include +#include +#include + +void encode(char message[], char code_char[]) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + int code[strlen(code_char)]; + + for (int i = 0; i < strlen(code_char); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == code_char[i] && code_char[i] != ' ' && code_char[i] != ',' && code_char[i] != '.') code[i] = j+1; + } + } + + // for (int i = 0; i < strlen(code_char); i++) printf("%d ", code[i]); printf("\n"); + + for (int i = 0, k = 0; i < strlen(message); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == message[i]) { + if (k >= strlen(code_char)) k = 0; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j+code[k]]; + // printf("%c\n", message[i]); + k++; + } else { + k++; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j+code[k]]; + // printf("%c\n", message[i]); + k++; + } else { + k++; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j+code[k]]; + // printf("%c\n", message[i]); + k++; + } + } + } + break; + } + } + } + + printf("Encoded with repeating shifts "); + for (int i = 0; i < strlen(code_char); i++) { + if (1 <= code[i] && code[i] <= 26) printf("%d ", code[i]); + } + printf(": %s\n", message); +} + +void decode(char message[], char code_char[]) { + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + int code[strlen(code_char)]; + + for (int i = 0; i < strlen(code_char); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == code_char[i] && code_char[i] != ' ' && code_char[i] != ',' && code_char[i] != '.') code[i] = j+1; + } + } + + // for (int i = 0; i < strlen(code_char); i++) printf("%d ", code[i]); printf("\n"); + + for (int i = 0, k = 0; i < strlen(message); i++) { + for (int j = 26; j < 52; j++) { + if (alphabet[j] == message[i]) { + if (k >= strlen(code_char)) k = 0; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j-code[k]]; + // printf("%c\n", message[i]); + k++; + } else { + k++; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j-code[k]]; + // printf("%c\n", message[i]); + k++; + } else { + k++; + if (1 <= code[k] && code[k] <= 26) { + // printf("%c = ", message[i]); + message[i] = alphabet[j-code[k]]; + // printf("%c\n", message[i]); + k++; + } + } + } + break; + } + } + } + + printf("Encoded with repeating shifts "); + for (int i = 0; i < strlen(code_char); i++) { + if (1 <= code[i] && code[i] <= 26) printf("%d ", code[i]); + } + printf(": %s\n", message); +} + +int main() { + char ar[1000]; + char code[1000]; + char option_char; + printf("Enter the message\n"); + scanf("%[^\n]", &ar); + + int option = -1; + printf("Encode or decode? 0 or 1 and the code word/sentence\n"); + + scanf("%d %[^\n]", &option, &code); + + switch (option) { + case 0: + encode(ar, code); + option = -1; + break; + case 1: + decode(ar, code); + option = -1; + break; + } +} \ No newline at end of file diff --git a/vernam-cipher.c b/vernam-cipher.c new file mode 100644 index 0000000..f83e215 --- /dev/null +++ b/vernam-cipher.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +void encode(char message[]) { + char origin[strlen(message)]; + for (int i = 0; i < strlen(message); i++) { + origin[i] = message[i]; + } + char alphabet[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + int shifts[strlen(message)]; + for (int i = 0; i < strlen(message); i++) { + shifts[i] = 0; + } + int k; + + for (int i = 0; i < strlen(message); i++) { + for (int j = 0; j < 26; j++) { + if (alphabet[j] == message[i]) { + k = rand() % 27 + 1; + shifts[i] = k; + message[i] = alphabet[j+k]; + break; + } + } + } + printf("Encoded: %s\n", message); + // for (int i = 0; i < strlen(message); i++) { + // printf("%d %c; ", shifts[i], origin[i]); + // } + // printf("\n"); +} + +// void decode(char message[]); (uplne to nejde...) + +int main() { + srand(time(NULL)); + + char ar[1000]; + + printf("Enter the message\n"); + scanf("%[^\n]", &ar); + + encode(ar); +} \ No newline at end of file