Caesar Cipher Encryption  & Decryption in  C  and  C ++  code : #include<stdio.h> #include<string.h> #include<co...

Caesar Cipher Encryption & Decryption in C and C++

Caesar Cipher Encryption & Decryption in C and C++

 code :

#include<stdio.h>
#include<string.h>
#include<conio.h>
void encript(int p,int key){
int me = (p+key)%26;
//printf("%d",me);
switch(me){
case(0):
{
printf("a");
break;
}
case(1):
{
printf("b");
break;
}
case(2 ):
{
printf("c");
break;
}
case(3):
{
printf("d");
break;
}
case(4 ):
{
printf("e");
break;
}
case(5):
{
printf("f");
break;
}
case(6 ):
{
printf("g");
break;
}
case(7):
{
printf("h");
break;
}
case(8 ):
{
printf("i");
break;
}
case(9 ):
{
printf("j");
break;
}
case(10 ):
{
printf("k");
break;
}
case(11):
{
printf("l");
break;
}
case(12 ):
{
printf("m");
break;
}
case(13 ):
{
printf("n");
break;
}
case(14 ):
{
printf("o");
break;
}
case(15 ):
{
printf("p");
break;
}
case(16 ):
{
printf("q");
break;
}
case(17 ):
{
printf("r");
break;
}
case(18 ):
{
printf("s");
break;
}
case(19):
{
printf("t");
break;
}
case(20 ):
{
printf("u");
break;
}
case(21 ):
{
printf("v");
break;
}
case(22 ):
{
printf("w");
break;
}
case(23 ):
{
printf("x");
break;
}
case(24 ):
{
printf("y");
break;
}
case(25 ):
{
printf("z");
break;
}
default:
{
printf(" ");
}
}
}
void dencript(int p,int key){
int me = (p-key)%26;
//printf("%d",me);
switch(me){
case(0):
{
printf("a");
break;
}
case(1):
{
printf("b");
break;
}
case(2 ):
{
printf("c");
break;
}
case(3):
{
printf("d");
break;
}
case(4 ):
{
printf("e");
break;
}
case(5):
{
printf("f");
break;
}
case(6 ):
{
printf("g");
break;
}
case(7):
{
printf("h");
break;
}
case(8 ):
{
printf("i");
break;
}
case(9 ):
{
printf("j");
break;
}
case(10 ):
{
printf("k");
break;
}
case(11):
{
printf("l");
break;
}
case(12 ):
{
printf("m");
break;
}
case(13 ):
{
printf("n");
break;
}
case(14 ):
{
printf("o");
break;
}
case(15 ):
{
printf("p");
break;
}
case(16 ):
{
printf("q");
break;
}
case(17 ):
{
printf("r");
break;
}
case(18 ):
{
printf("s");
break;
}
case(19):
{
printf("t");
break;
}
case(20 ):
{
printf("u");
break;
}
case(21 ):
{
printf("v");
break;
}
case(22 ):
{
printf("w");
break;
}
case(23 ):
{
printf("x");
break;
}
case(24 ):
{
printf("y");
break;
}
case(25 ):
{
printf("z");
break;
}
default:
{
printf(" ");
}
}
}
int main(){
char enterString[25];
int i,choice;
printf("Enter a string:\t");
gets(enterString);
printf("Enter your choice: 1.encrypy 2.decrypt \t");
scanf("%d",&choice);
int length = strlen(enterString);
//printf("lenght is %d",length);
switch(choice)

case 1:
{
int key;
printf("Enter a key(Number):\t");
scanf("%d",&key);
for(i=0;i<length;i++)
{
char ch = enterString[i];
switch(ch){
case('a' ):
{
encript(0,key);
break;
}
case('b'):
{
encript(1,key);
break;
}
case('c' ):
{
encript(2,key);
break;
}
case('d' ):
{
encript(3,key);
break;
}
case('e' ):
{
encript(4,key);
break;
}
case('f' ):
{
encript(5,key);
break;
}
case('g' ):
{
encript(6,key);
break;
}
case('h' ):
{
encript(7,key);
break;
}
case('i' ):
{
encript(8,key);
break;
}
case('j' ):
{
encript(9,key);
break;
}
case('k' ):
{
encript(10,key);
break;
}
case('l' ):
{
encript(11,key);
break;
}
case('m' ):
{
encript(12,key);
break;
}
case('n' ):
{
encript(13,key);
break;
}
case('o' ):
{
encript(14,key);
break;
}
case('p' ):
{
encript(15,key);
break;
}
case('q' ):
{
encript(16,key);
break;
}
case('r' ):
{
encript(17,key);
break;
}
case('s' ):
{
encript(18,key);
break;
}
case('t'):
{
encript(19,key);
break;
}
case('u' ):
{
encript(20,key);
break;
}
case('v' ):
{
encript(21,key);
break;
}
case('w' ):
{
encript(22,key);
break;
}
case('x' ):
{
encript(23,key);
break;
}
case('y' ):
{
encript(24,key);
break;
}
case('z' ):
{
encript(25,key);
break;
}
default:
{
encript(-1,key);
}
}

} //block complete
break;
}
case 2:
{
int key;
printf("Enter a key(Number):\t");
scanf("%d",&key);
for(i=0;i<length;i++)
{
char ch = enterString[i];
switch(ch){
case('a' ):
{
dencript(0,key);
break;
}
case('b'):
{
dencript(1,key);
break;
}
case('c' ):
{
dencript(2,key);
break;
}
case('d' ):
{
dencript(3,key);
break;
}
case('e' ):
{
dencript(4,key);
break;
}
case('f' ):
{
dencript(5,key);
break;
}
case('g' ):
{
dencript(6,key);
break;
}
case('h' ):
{
dencript(7,key);
break;
}
case('i' ):
{
dencript(8,key);
break;
}
case('j' ):
{
dencript(9,key);
break;
}
case('k' ):
{
dencript(10,key);
break;
}
case('l' ):
{
dencript(11,key);
break;
}
case('m' ):
{
dencript(12,key);
break;
}
case('n' ):
{
dencript(13,key);
break;
}
case('o' ):
{
dencript(14,key);
break;
}
case('p' ):
{
dencript(15,key);
break;
}
case('q' ):
{
dencript(16,key);
break;
}
case('r' ):
{
dencript(17,key);
break;
}
case('s' ):
{
dencript(18,key);
break;
}
case('t'):
{
dencript(19,key);
break;
}
case('u' ):
{
dencript(20,key);
break;
}
case('v' ):
{
dencript(21,key);
break;
}
case('w' ):
{
dencript(22,key);
break;
}
case('x' ):
{
dencript(23,key);
break;
}
case('y' ):
{
dencript(24,key);
break;
}
case('z' ):
{
dencript(25,key);
break;
}
default:
{
dencript(-1,key);
}
}
} break;
}
default:
printf("\nEnter valid input");
break;

}
getch();
}

Caesar Cipher Encryption & Decryption in C and C++
Caesar Cipher Encryption & Decryption in C and C++

Caesar Cipher Encryption & Decryption in C and C++
Caesar Cipher Encryption & Decryption in C and C++

Caesar Cipher Encryption & Decryption in C and C++
Caesar Cipher Encryption & Decryption in C and C++

Caesar Cipher Encryption & Decryption in C and C++
Caesar Cipher Encryption & Decryption in C and C++


0 comments: