Aim:
To write a C Program to Implement the Simple Substitution Technique Named Caesar Cipher.
Algorithm:
- The Caesar Cipher technique is one of the earliest and simplest method of encryption technique.
- It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example with a shift of 1,
- A would be replaced by B, B would become C, and so on. The method is apparently named after Julius Caesar, who apparently used it to communicate with his officials.
- Thus to cipher a given text we need an integer value, known as shift which indicates the number of position each letter of the text has been moved down.
- The encryption can be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme,
- A = 0, B = 1,…, Z = 25. Encryption of a letter by a shift n can be described mathematically.
- End
Program:
#include<stdio.h> #include<stdlib.h> char encrpt[100]; char decrpt[100]; int shfit; void encrypt(char* plain) { printf("ENTER THE KEY VALUE : "); scanf("%d",&shfit); int i=0; while(*(plain+i)!='\0') { if(plain[i]>='a'&&plain[i]<='z') { int ch=plain[i]; encrpt[i]=encrpt[i]+(((ch-97+shfit)%26)+97); } else if(plain[i]>='A'&&plain[i]<='Z') { int ch=plain[i]; encrpt[i]=encrpt[i]+(((ch-65+shfit)%26)+65); } i++; } } void crp() { int i=0; while(*(encrpt+i)!='\0') { if(encrpt[i]>='a'&&encrpt[i]<='z') { int ch=encrpt[i]; decrpt[i]=decrpt[i]+((ch-97-shfit)%26)+97; } else if(encrpt[i]>='A'&&encrpt[i]<='Z') { int ch=encrpt[i]; decrpt[i]=decrpt[i]+((ch-65-shfit)%26)+65; } i++; } } int main() { char plain[100]; printf("ENTER PLAIN TEXT :\n"); scanf ("%s",plain) ; encrypt(plain); printf("\nENCRYPTED MESSAGE :%s\n",encrpt); crp(); printf("DECRYPTED MESSAGE :%s",decrpt); }
Execution:
Input: ant 4 Output: ENTER PLAIN TEXT : ENTER THE KEY VALUE : ENCRYPTED MESSAGE :erx DECRYPTED MESSAGE :ant
Result:
Thus implementation of the Caesar Cipher program was executed Successfully.