Experimental polybius square encryption
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
polybius-square/decrypt_file.c

516 lines
11 KiB

/* decrypt_file.c
This function is part of the Polysqu program. It decrypts message input
from the user and writes it to a file.
Copyright (C) 2015, 2016, 2017 Scott C. MacCallum
scm@ma.sdf.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void decrypt_file(void)
{
int choice;
int index;
int element_number = 1;
int array_number[5];
int element_letter = 97;
int array_letter[25];
int counter;
char file_name[256];
FILE *pointer_file;
char *pointer_name = file_name;
char *pointer_mode = "w";
char letter;
printf("\n");
printf("File Name: ");
scanf("%255s", file_name);
printf("\n");
printf("WARNING, if file exists, it will be overwritten.\n");
printf("\n");
printf("Do you want to proceed?\n");
printf("\n");
printf("1. Yes\n");
printf("2. No\n");
printf("\n");
printf("Number: ");
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
printf("\n");
printf("Good bye!\n");
printf("\n");
exit(0);
default:
printf("\n");
printf("Error! Bad input.\n");
printf("\n");
exit(1);
}
pointer_file = fopen(pointer_name, pointer_mode);
fclose(pointer_file);
pointer_mode = "a";
printf("\n");
printf("Your message will be encrypted using this\n");
printf("Polybius Square.\n");
/* At index zero, initialize the elements in the array of numbers
with values one through five */
for (index = 0; element_number < 6; index++)
array_number[index] = element_number++;
printf("\n");
printf(" ");
/* At index zero, print the values of the elements in the array of
numbers */
for (index = 0; index < 5; index++)
printf("%d ", array_number[index]);
printf("\n");
/* At index zero, print the value of the element in the array of
numbers */
printf("%d ", array_number[0]);
/* At index zero, initialize the elements in the array of letters
with ASCII values ninety-seven through one hundred twenty-one */
for (index = 0; element_letter < 122; index++)
array_letter[index] = element_letter++;
/* At index zero, print the five ASCII values of the elements in
the array of letters */
for (index = 0; index < 5; index++)
printf("%c ", array_letter[index]);
printf("\n");
/* At index one, print the ASCII value of the element in the
array of numbers */
printf("%d ", array_number[1]);
/* At index five, print the five ASCII values of the elements in
the array of letters */
for (index = 5; index < 10; index++)
printf("%c ", array_letter[index]);
printf("\n");
/* At index two, print the ASCII value of the element in the
array of numbers */
printf("%d ", array_number[2]);
/* At index ten, print the five ASCII values of the elements in
the array of letters */
for (index = 10; index < 15; index++)
printf("%c ", array_letter[index]);
printf("\n");
/* At index three, print the ASCII value of the element in the
number array */
printf("%d ", array_number[3]);
/* At index fifteen, print the five ASCII values of the elements
in the letter array */
for (index = 15; index < 20; index++)
printf("%c ", array_letter[index]);
printf("\n");
/* At index four, print the ASCII value of the element in the
number array */
printf("%d ", array_number[4]);
/* At index twenty, print the five ASCII values of the elements
in the letter array */
for (index = 20; index < 25; index++)
printf("%c ", array_letter[index]);
printf("\n");
printf("\n");
printf("Limit your message to 75 letters and end it with\n");
printf("an exclamation point.\n");
printf("\n");
printf("Message: ");
for (counter = 0; counter < 77; counter++) {
scanf("%c", &letter);
letter = tolower(letter);
/* a */
if (letter == array_letter[0]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* b */
if (letter == array_letter[1]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* c */
if (letter == array_letter[2]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* d */
if (letter == array_letter[3]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* e */
if (letter == array_letter[4]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* f */
if (letter == array_letter[5]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* g */
if (letter == array_letter[6]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* h */
if (letter == array_letter[7]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* i */
if (letter == array_letter[8]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* j */
if (letter == array_letter[9]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* k */
if (letter == array_letter[10]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* l */
if (letter == array_letter[11]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* m */
if (letter == array_letter[12]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* n */
if (letter == array_letter[13]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* o */
if (letter == array_letter[14]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* p */
if (letter == array_letter[15]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* q */
if (letter == array_letter[16]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* r */
if (letter == array_letter[17]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* s */
if (letter == array_letter[18]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* t */
if (letter == array_letter[19]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* u */
if (letter == array_letter[20]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, "%d", array_number[0]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* v */
if (letter == array_letter[21]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, "%d", array_number[1]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* w */
if (letter == array_letter[22]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, "%d", array_number[2]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* x */
if (letter == array_letter[23]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, "%d", array_number[3]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* y */
if (letter == array_letter[24]) {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, "%d", array_number[4]);
fprintf(pointer_file, " ");
fclose(pointer_file);
}
/* ! */
if (letter == '!') {
pointer_file = fopen(pointer_name, pointer_mode);
fprintf(pointer_file, "\n");
fclose(pointer_file);
printf("\n");
printf("Encrypted message written to file.");
printf("\n");
printf("\n");
exit(0);
}
if (counter == 76) {
printf("\n");
printf("Error! Bad Input.");
printf("\n");
printf("\n");
exit(1);
}
}
}