Wednesday, 15 August 2018

Implement Ceasar cipher encryption-decryption in c.


Introduction:
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. The method is apparently named after Julius Caesar, who apparently used it to communicate with his officials.
Formula:
Ciphertext(C): E(k,p) = (p+k)mod26
Plaintext(p): D(k,C) = (C-k)mod26
Where k takes a value in the range 1 to 25.

Let us assign a numerical equivalent to each letter:
a
b
c
d
E
F
g
h
i
J
k
l
m
0
1
2
3
4
5
6
7
8
9
10
11
12

n
o
P
q
R
S
T
u
v
W
x
y
z
13
14
15
16
17
18
19
20
21
22
23
24
25

Example:
Key=3
Plain text: hello how are you
Cipher text: KHOOR KRZ DUH BRX


C code:

//CEASER-CIPHER
#include<stdio.h>
#include<conio.h>
void main()
{
            char pt[20]={'\0'},ct[20]={'\0'},rt[20]={'\0'},st[20]={'\0'};
            int key,i;
            clrscr();
            printf("\n enter the plain text:");
            gets(pt);
            printf("\n enter teh key value in numeric (0 to 25):");
            scanf("\n %d",&key);

            //converting plain text into cipher text (encryption)
            for(i=0;i<strlen(pt);i++)
            {
                        ct[i]=((((pt[i]-'a')+key)%26)+'a');
            }
            printf("\n cipher text is: %s",ct);

            //converting cipher text into plain text (decryption)
            for(i=0;i<strlen(ct);i++)
            {
            if(ct[i]<pt[i])
            {
                        rt[i]=(26+((ct[i]-97)-key)+97);
            }
            else
            {
                        rt[i]=((((ct[i]-97)-key)%26)+97);
            }
            }
            printf("\n \n plain text is: %s",rt);
            getch();
}
 

Output:
 

THANK YOU!!!!
 

No comments:

Post a Comment