August 18, 2018

Caesar Cipher in C (Encryption & Decryption)

Caesar Cipher Image

Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet.

C Program:

#include <stdio.h>
#include <string.h> // for strlen() function
#include <ctype.h> // for isupper() function
void encrypt(char text[], int s)
{
int i;
for (i=0; i<strlen(text); i++)
{
if (isupper(text[i]))
text[i]= (text[i]+s-65)%26 +65;
else
text[i]= (text[i]+s-97)%26 +97;
}
}
void dencrypt(char text[], int s)
{
int i;
for (i=0; i<strlen(text); i++)
{
if (isupper(text[i]))
text[i]= 65+ ((text[i]-s-65)%26+26)%26;
else
text[i]= 97+ ((text[i]-s-97)%26+26)%26;
}
}
int main()
{
char text[100];
int s,i;
printf("Enter String: ");
scanf("%s",&text);
printf("Enter Key: ");
scanf("%d",&s);
printf("1.Encryption\n2.Decryption\nEnter choice: ");
scanf("%d",&i);
switch(i)
{
case 1:
encrypt(text, s);
printf("Cipher Text: %s",text);
break;
case 2:
dencrypt(text, s);
printf("Plain Text: %s",text);
break;
default:
printf("Invalid input");
break;
}
return 0;
}
view raw Caesar.c hosted with ❤ by GitHub


post written by: Author

Related Posts

0 comments:

Note: Only a member of this blog may post a comment.