Skip to content

Instantly share code, notes, and snippets.

@jameslyons
Last active August 29, 2015 13:55
Show Gist options
  • Save jameslyons/8703946 to your computer and use it in GitHub Desktop.
Save jameslyons/8703946 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[]){
char plaintext[] = "DEFEND THE EAST WALL OF THE CASTLE";
int L = strlen(plaintext);
char *ciphertext = malloc(sizeof(char)*(L+1));
int i, key = 3;
//*****encipher******
for(i=0;i<L;i++){
if(isalpha(plaintext[i])) ciphertext[i] = (toupper(plaintext[i]) - 'A' + key)%26 + 'A';
else ciphertext[i] = plaintext[i];
}
ciphertext[i] = 0;
char *plaintext2 = malloc(sizeof(char)*(L+1));
//*****decipher******
for(i=0;i<L;i++){
if(isalpha(plaintext[i])) plaintext2[i] = (toupper(ciphertext[i]) - 'A' + 26 - key)%26 + 'A';
else plaintext2[i] = ciphertext[i];
}
plaintext2[i] = 0;
printf("%s\n%s\n%s\n",plaintext,ciphertext,plaintext2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment