# Cryptography in C

Lacking a Computer Science background, I decided to take Harvard's CS50 through EDX. It's a self-paced course complete with lectures, short videos on topics like arrays and algorithms, and programming homework.

The course starts of with learning C, a language of which I had no prior knowledge. Some of the syntax is similar to JavaScript and been interested to dive deeper into certain programming concepts that I encounter in my job as a web developer.

### The Problems

#### Caesar Cipher

A very simple encryption technique that encrypts a word or message by 'rotating' each letter by k positions.
The basic formula is:
ci=(pi + k) % 26
Note: The code to Caesar's cipher is very similar to the next (Vigenere's) so I'll only be posting the code to the Vigenere's cipher.

#### Vigenere's Cipher

This cipher is a bit more secure than Caesar but still very easy to crack. It encrypts messages using a sequence of keys (or a keyword). This program takes k, the keyword, and uses it to encrypt p the message (plaintext). The characters of k are used as different values to encrypt the characters of p.

This can be expressed as:
ci=(pi + kj) % 26
The program also has to have certain validation for the inputs that it takes. It has to take a single command line argument that only consists of alphabetic characters. Case has to be preserved as well. Code below:

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

string vigenere(string k, string p);

int main (int argc, string argv[])
{
//error out if one CL arg wasn't entered
if(argc != 2 )
{
printf("Error! Wrong number of command line arguments\n");
return 1;
} else
{
//k is the cipher keyword. CL argument
string k = argv[1];
int n = strlen(k);
//check to makes sure everything in string is a letter
for(int i = 0;  i < n; i++)
{
if(!isalpha(k[i]))
{
printf("non alphabetic char/value entered");
return 1;
}
}

//get the plaintext word to encrypt and passs it to the function
string p = GetString();
vigenere(k, p);
printf("\n");

}

return 0;
}
string vigenere(string k, string p)
{
int pLength = strlen(p);
int kLength = strlen(k);
int myChar;
//keyword index
int kIndex = 0;
//convert keyword to lowercase
for(int d = 0; d < kLength; d++) {
k[d] = tolower(k[d]);
}
for(int i = 0; i < pLength; i++)
{
//loop through plaintext word and grab a new char each time to save to myChar
myChar = p[i];

if(isalpha(myChar))
{
//encrypt it!!!:
int startOfIndex;
char charEncrypted;
//set ASCII boundaries
if(isupper(myChar))
{
startOfIndex = 65;
} else {
startOfIndex = 97;
}
//find alphabet index of keyword char and use k's length as the modulus then subtract ASCII start
// of lower letters since we have converted p to all lowercase!!
int vigenereNum = k[kIndex % kLength] - 97;
//vigenereNum is the alphabetic index of this char of k
vigenereNum = (((myChar - startOfIndex) + vigenereNum) % 26);
// now we convert back to ASCII
charEncrypted = startOfIndex + vigenereNum;
printf("%c", charEncrypted);
//incrementing k's index each time to cycle through the keyword so that a
//different char is used for encryption each time
kIndex++;

} else {
printf("%c", myChar);

}

}

return 0;
}
``````