Skip to content

Instantly share code, notes, and snippets.

@mhofwell
Last active October 24, 2020 18:15
Show Gist options
  • Save mhofwell/7077477e3bd18f830a1d11317851f157 to your computer and use it in GitHub Desktop.
Save mhofwell/7077477e3bd18f830a1d11317851f157 to your computer and use it in GitHub Desktop.
Shift Cipher String Encryption in C!
// this program is run on the command line. Fist compile the program with clang or C compiler.
// then run ./<program name> <key>
// the key is a numerical value greater or equal to zero and is required.
// The program will then ask you for a plaintext message to encrypt using your key.
// The program returns ciphertext.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
int errorHandle(void);
int numCheck(string str);
int encrypt(int key, string message, int encryptedMessage[], int inputLength);
int encryptAlgo(char c, int key, int count, int cipherArr[]);
string printCipher(int array[], int length);
string getMessage(void);
int main(int argc, string argv[])
{
string key = argv[1];
if (argc == 2)
{
// is the key a valid number greater or equal to 0?
numCheck(key);
// turn string argv to integer
int keyInt = atoi(key);
// get plaintext
string message = getMessage();
// get the length of the message
int inputLength = strlen(message);
// initialize encrypted message array
const int L = inputLength;
int encryptedMessage[L];
// call the encryption function
encrypt(keyInt, message, encryptedMessage, L);
// print the encrypted message
printCipher(encryptedMessage, L);
}
// handle errors if argv != 2
else
{
errorHandle();
}
}
// Utility functions //////////////////////////////////////////////////////////////////////////////////////
// error handler //
int errorHandle(void)
{
printf("Usage: ./caesar key\n");
exit(1);
}
// numCheck: check the key to see if its all digits //
int numCheck(string str)
{
int count = 0;
for (int i = 0; i < strlen(str); i++)
{
if (isdigit(str[i]))
{
count++;
if (count == strlen(str))
{
return 0;
}
}
else
{
errorHandle();
}
}
return 0;
}
// getMessage //
string getMessage(void)
{
string message = get_string("plaintext: ");
return message;
}
// encrypt the message //
int encrypt(int key, string message, int array[], int length)
{
for (int i = 0; i < length; i++)
{
if (message[i] >= 'a' && message[i] <= 'z')
{
encryptAlgo(message[i], key, i, array);
}
else if (message[i] >= 'A' && message[i] <= 'Z')
{
encryptAlgo(message[i], key, i, array);
}
else if (message[i] == ' ')
{
array[i] = message[i];
}
else if (message[i] == '?' || message[i] == '!' || message[i] == '.' || message[i] == ',')
{
array[i] = message[i];
}
}
return 0;
}
// encrypt the letters
int encryptAlgo(char c, int key, int i, int array[])
{
// coerce the char to int
int asciiChar = (int) c;
// create a sum betewen the ascii value of the char and the key
int shift = asciiChar + key;
const int A = 65;
const int Z = 90;
const int a = 97;
const int z = 122;
// check to see if the char is upper case
if (asciiChar < 91)
{
// if the total shift is less than the upper bound of the uppercase ASCII values
if (shift <= Z)
{
// if shift is under the upper bound then save the character
int newAsciiLetter = shift;
array[i] = newAsciiLetter;
}
else
{
// figure out difference from upper bound to char.
int toEnd = Z - asciiChar;
// compute the difference remaining from the key.
int diffFromKey = key - toEnd - 1;
// find the modulo of the difference and the set of letters then add to the beginning of the set to find your char
int newAsciiLetter = ((diffFromKey % 26) + A);
array[i] = newAsciiLetter;
}
}
// if the char isn't uppcase, its lowercase.
else
{
if (shift <= z)
{
int newAsciiLetter = shift;
array[i] = newAsciiLetter;
}
else
{
int toEnd = z - asciiChar;
int diffFromKey = key - toEnd - 1;
if (diffFromKey > 26)
{
int newAsciiLetter = ((diffFromKey % 26) + a);
array[i] = newAsciiLetter;
}
else
{
int newAsciiLetter = (a + diffFromKey);
array[i] = newAsciiLetter;
}
}
}
return 0;
}
// print the encryption cars
string printCipher(int array[], int length)
{
printf("ciphertext: ");
for (int i = 0; i < length; i++)
{
char cipherChar = (char) array[i];
printf("%c", cipherChar);
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment