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:
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> // 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; | |
} |
0 comments:
Note: Only a member of this blog may post a comment.