Added cipher programs

This commit is contained in:
mnjx
2023-02-14 20:44:42 +01:00
committed by GitHub
parent ca033aa57d
commit 79bb71732b
4 changed files with 272 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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");
}
}
}
+39
View File
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
}
+127
View File
@@ -0,0 +1,127 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
}
+46
View File
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
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);
}