Techknow Study

Encrypt and decrypt {substitution cipher}

4:03:00 PM vikas 0 Comments Category :



Encrypt and decrypt a plain text using substitution cipher(normal method)


In cryptography, a Caesar cipher, also known as a 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 plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals.Later this method was advanced with the key being taken greater than 3 and this method is called normal substitution method.

METHOD OF ENCRYPTION-:  The method of encyrption is exactly the same way as we have done in experiment no:1, but the only difference is that the key is user input (instead of 3). According to the key the cipher is formed and then decrypted at the receiver end.


PROGRAM in C++-:


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
void main()
{
int ch=1,n=0,key;
char str[20];
clrscr();
printf("enter key to be used\n");
scanf("%d",&key);
key=key%26;
do
{
printf("enter 1 for encryption\n");
printf("enter 2 for decryption\n");
printf("enter 0 for exit\n");
scanf("%d",&ch);
if(ch==1)
{
n=0;
printf("enter text to be encrypted");
fflush(stdin);
scanf("%s",&str);
while(str[n]!='\0')
{
if(str[n]>=65&&str[n]<=90)
{
if((str[n]+32+key)>122)
{
str[n]=str[n]+32+key-122+96;
}
else
{
str[n]=str[n]+32+key;
}
n++;
}
puts(str);
}
if(ch==2)
{
n=0;
printf("enter text to be decrypted");  }
fflush(stdin);
scanf("%s",&str);
while(str[n]!='\0')
{
if(str[n]>=97&&str[n]<=122)
{
if((str[n]-(32+key))<65)
 {
str[n]=str[n]-(32+key)+26;
}
else
{
str[n]=str[n]-(32+key);
}
}
n++;
}
puts(str);
}
}
while(ch!=0);
getch();
}

RELATED POSTS

0 comments