Last active
August 29, 2015 14:24
-
-
Save SohanChy/37c43ff95df6b4908da1 to your computer and use it in GitHub Desktop.
Encrypt & Decrypt input text to Caesar Cipher. (CS50). Input a numeric key and a text to encrypt it into ceaser ciphered code. Send it to anyone. Those who dont know the key cant read your message, those who have the key can decrypt and read it. :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <ctype.h> | |
void caesarEncrypt(char plaintxt[], int key); | |
void caesarDecrypt(char plaintxt[], int key); | |
int main(void) | |
{ | |
char plaintxt[500]; | |
char encrypted[500]; | |
char option; | |
int key=1; | |
printf("Please input your text: (Maximum 500 characters)\n"); | |
fgets(plaintxt,500,stdin); | |
//only integer digits as input will work | |
printf("Please input your key: (Numbers only please)\n"); | |
scanf("%d",&key); | |
//Option to encrypt or decrypt | |
do{ | |
printf("Enter E to Encrypt & D to Decrypt.\n"); | |
scanf(" %c",&option); | |
} | |
while (option != 'E' && option != 'D' && option != 'e' && option != 'd'); | |
printf("\nYour key is: %d \nYour text length is: %d",key,(int)strlen(plaintxt)-1); | |
//Now I have string plain text[], and key | |
strcpy(encrypted,plaintxt); | |
//Encrypt or Decrypt | |
switch(option) | |
{ | |
case 'e': | |
case 'E': printf("\nYou chose to ENCRYPT\n"); | |
caesarEncrypt(encrypted,key); | |
break; | |
case 'd': | |
case 'D': printf("\nYou chose to DECRYPT\n"); | |
caesarDecrypt(encrypted,key); | |
break; | |
} | |
printf("\nCaeser cipher complete. \n\nYour secret message: "); | |
fputs(encrypted,stdout); | |
printf("\n\n\n"); | |
return 0; | |
} | |
void caesarEncrypt(char plaintxt[], int key) | |
{ | |
int numAlphas = 26; //number of alphabets to wrap around | |
key=key%26; //modulo rounds the key to below 26, plus 1 makes sure key is not 0 | |
int i; | |
for (i=0;i<(int)strlen(plaintxt);i++) //Run loop for length of text | |
{ | |
if(isalpha(plaintxt[i])) //Only encrypt if its alphabetic | |
{ | |
//To keep Uppercase and Lowercase formatting, two separate if else's used. | |
if(isupper(plaintxt[i])) //for Uppercase | |
{ | |
if(plaintxt[i]+key<='Z') //Wrap around Z | |
{ | |
plaintxt[i] = plaintxt[i]+key; | |
} | |
else //If exceeds Z, wrap and go to A | |
{ | |
plaintxt[i] = plaintxt[i]+(key-numAlphas); | |
} | |
} | |
//Exactly same as above, but for lowercase | |
if(islower(plaintxt[i])) | |
{ | |
if(plaintxt[i]+key<='z') | |
{ | |
plaintxt[i] = plaintxt[i]+key; | |
} | |
else | |
{ | |
plaintxt[i] = plaintxt[i]+(key-numAlphas); | |
} | |
} | |
} | |
} | |
} | |
void caesarDecrypt(char plaintxt[], int key) | |
{ | |
int numAlphas = 26; //number of alphabets to wrap around | |
key=key%26; //modulo rounds the key to below 26, plus 1 makes sure key is not 0 | |
int i; | |
for (i=0;i<(int)strlen(plaintxt);i++) //Run loop for length of text | |
{ | |
if(isalpha(plaintxt[i])) //Only encrypt if its alphabetic | |
{ | |
//To keep Uppercase and Lowercase formatting, two separate if else's used. | |
if(isupper(plaintxt[i])) //for Uppercase | |
{ | |
if(plaintxt[i]-key>='A') //Wrap around A | |
{ | |
plaintxt[i] = plaintxt[i]-key; | |
} | |
else //If preceeds A, wrap and go to Z | |
{ | |
plaintxt[i] = plaintxt[i]-(key-numAlphas); | |
} | |
} | |
//Exactly same as above, but for lowercase | |
if(islower(plaintxt[i])) | |
{ | |
if(plaintxt[i]-key>='a') | |
{ | |
plaintxt[i] = plaintxt[i]-key; | |
} | |
else | |
{ | |
plaintxt[i] = plaintxt[i]-(key-numAlphas); | |
} | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment