Problem Set 2 (Substitution) Cipher Program

The program encrypts a message by replacing every letter with another letter.

To do this the program uses a key given by the user at the command line, in this case, a mapping of each of the letters of the alphabet to the letter it should correspond to when the program gets encrypted. To “decrypt” the message, the receiver of the message would need to know the key, so that they can reverse the process.

/*
 * File Name: substitution.c
 * Programer: Julio Ornelas
 * Date: 2022/12/29
 * The program encrypts a message by replacing every letter with another letter.
 * To do this the program uses a key given by the user at the command line
 * in this case, a mapping of each of the letters of the alphabet to the letter it should
 * correspond to when the program gets encrypted.
 * To “decrypt” the message, the receiver of the message would need to know the key,
 * so that they can reverse the process.
 */

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

//function prototypes
bool is26char(string isString26char);
string textCipher(string textToCipher, string keyToCipherText);

int main(int argc, string argv[]) //command line arguments argc = agument count and argv = argument vector (array of strings)
{
    //declarations
    string userText; //Variable that will store text inputed by user at prompt
    string cipherKey; //Variable that will store the cipher key inputed at the command line


    //get user input, calculation and output
    if (argc != 2) //if argument count is not equal to 2 quit program and return to command line, instructions are given
    {
        printf("Usage: ./substitution key\n");
        return 1;
    }
    else
    {
        //continue with program
        if (is26char(argv[1]) == false)
        {
            printf("Usage: ./substitution key\n"); //if user does not input only numbers as second argument quit program and return to command line, instructions are given again
            return 1;
        }
        else
        {
            //continue with program
            cipherKey = argv[1];//cipher key given by the user must be 26 characters long
            userText = get_string("plaintext: "); //request for text input to be cipher by program
            textCipher(userText, cipherKey);//function to cipher text and output result
        }
    }
}//int main(int argc, string argv[])

//Function takes in a string and returns true if the string is a number and false if it not a number
bool is26char(string isString26char)
{
    int userKeyCount;
    string userText = isString26char;
    userKeyCount = strlen(isString26char);

    if (userKeyCount == 26)
    {
        for (int i = 0; i < userKeyCount; i++)
        {
            if (userText[i] < 65 || userText[i] > 122)
            {
                return false;
            }
            else if (userText[i] > 90 && userText[i] < 97)
            {
                return false;
            }
        }
        // here is where the code to check for duplicate characters go did not feel like writing more loops
        // -------- begining of code missing ------------
        /*
        for (int i = 0; i < userKeyCount; i++)
        {
            char checkChar;
            for (int j = 0; j < userKeyCount; i++)
            {

            }
        }
        */
        // --------- end of code missing -------------
        return true;
    }
    return false;
}//bool is26char(string isString26char)

//This function takes two parameters a string to be cipher and a string to be used as a key to cipher the text
string textCipher(string textToCipher, string keyToCipherText)
{
    //declarations
    string userText; //Variable that will store the text to be cipher
    string cipherKey; //Varialbe the will hold the Key to be use to cipher the user text
    int value; //ASCII value of each character looped from userText
    int characterLength; //How long a string is

    //user input
    userText = textToCipher;
    cipherKey = keyToCipherText;
    characterLength = strlen(userText);

    //calculation and output
    printf("ciphertext: ");
    for (int i = 0; i < characterLength; i++)
    {
        if (userText[i] >= 65 && userText[i] <= 90) //boundaries of uppercase caracters in a ASCII table
        {
            value = userText[i] - 65;
            printf("%c", toupper(cipherKey[value]));
        }
        else if (userText[i] >= 97 && userText[i] <= 122) //boundaries of lowercase caracters in a ASCII table
        {
            value = userText[i] - 97;
            printf("%c", tolower(cipherKey[value]));
        }
        else
        {
            //every other character in the ASCII table
            printf("%c", userText[i]);
        }
    }//for (int i = 0; i < characterLength; i++)
    printf("\n");
    return 0;
}//string textCipher(string textToCipher, int keyToCipherText)

/*
Check one character at the time aginst the whole string, this can be done with a loop
:( handles duplicate characters in uppercase key
    timed out while waiting for program to exit
:( handles duplicate characters in lowercase key
    timed out while waiting for program to exit
:( handles multiple duplicate characters in key
    timed out while waiting for program to exit
*/

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *