0% found this document useful (0 votes)
73 views24 pages

Index: S.No - Program Date Remarks

The document outlines 7 programs that implement various encryption/decryption techniques: 1. Caesar cipher 2. Rail fence cipher 3. Vernam cipher 4. Modified Caesar cipher 5. Diffie-Hellman key exchange 6. Hill cipher 7. A program using switch statements to implement the above techniques For each program, the code to encrypt and decrypt text is provided, along with sample outputs. The final program demonstrates implementing the techniques through a menu driven switch statement.

Uploaded by

Ayush Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views24 pages

Index: S.No - Program Date Remarks

The document outlines 7 programs that implement various encryption/decryption techniques: 1. Caesar cipher 2. Rail fence cipher 3. Vernam cipher 4. Modified Caesar cipher 5. Diffie-Hellman key exchange 6. Hill cipher 7. A program using switch statements to implement the above techniques For each program, the code to encrypt and decrypt text is provided, along with sample outputs. The final program demonstrates implementing the techniques through a menu driven switch statement.

Uploaded by

Ayush Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

INDEX

S.No
.

Program

Date

1.

Write a program to encrypt and 10/09/16


decrypt text using Caesar Cipher.

2.

Write a program to encrypt and 01/10/16


decrypt text using Rail Fence Cipher.

3.

Write a program to encrypt and 08/10/16


decrypt text using Vernam Cipher.

4.

Write a program to encrypt and 22/10/16


decrypt text using Modified Caesar
Cipher.

5.

Write a program to implement Diffie- 27/10/16


Hellman Key Exchange Algorithm.

6.

Write a program to encrypt and 8/11/16


decrypt text using Hill Cipher.

7.

Write a program, using Switch Case 16/11/16


Statement, to implement various
encryption/decryption techniques.

Remarks

Program No. 1
Write a program to encrypt and decrypt text using Caesar
Cipher.
Program Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char str[30];
int i;
cout<<"||||||||||| CAESAR CIPHER |||||||||||";
cout<<"\n\nEnter Plain Text: ";
gets(str);
i=0;
while(str[i]!='\0') //loop for encryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+3)%26+65;
i++;
}
cout<<"\nCipher Text is: ";
puts(str);
i=0;
while(str[i]!='\0') //loop for decryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+26-3)%26+65;
i++;
}
cout<<"\nDecrypted Text is: ";
puts(str);
getch();
}

Output:

Program No. 2
Write a program to encrypt and decrypt text using Rail Fence
Cipher.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char p[50],c[50];
int n,i,j,k,t;
cout<<"||||||||| RAIL FENCE CIPHER |||||||||";
cout<<"\n\nEnter plain text:\n";
gets(p);
n=strlen(p);
i=0;
while(i<n) //loop to remove spaces
{
if(p[i]==' ')
{
for(int j=i;j<n;j++)
{
p[j]=p[j+1];
}
n--;
continue;
}
i++;
}
i=0;
j=0;
cout<<"\nPlain text after removing spaces is:\n";
puts(p);
cout<<"\nZig-zag sequence is:\n";
while(i<n) //loop to generate 1st row
{
c[j]=p[i];
cout<<c[j]<<" ";
j++;
i=i+2;
}
cout<<"\n";
i=1;

while(i<n) //loop to generate 2nd row


{
c[j]=p[i];
cout<<" "<<c[j];
j++;
i=i+2;
}
c[n]='\0';
cout<<"\n\nCipher Text is:\n";
puts(c);
cout<<"\nDecrypted Text is:\n";
t=0;
j=0;
k=(n+1)/2;
i=0;
while(i<n) //loop for decryption
{
if(t%2==0)
{
p[i]=c[j];
j++;
}
else
{
p[i]=c[k];
k++;
}
i++;
t++;
}
p[n]='\0';
puts(p);
getch();
}

Output:

Program No. 3
Write a program to encrypt and decrypt text using Vernam
Cipher.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char pt[30],ct[30],pad[30];
int l,i;
cout<<"||||||||||| VERNAM CIPHER |||||||||||";
cout<<"\n\nEnter plain text:\n";
gets(pt);
l=strlen(pt);
i=0;
while(i<l) //loop to remove spaces
{
if(pt[i]==' ')
{
for(int j=i;j<l;j++)
{
pt[j]=pt[j+1];
}
l--;
continue;
}
i++;
}
cout<<"\nPlain text after removing spaces is:\n";
puts(pt);
cout<<"\nEnter One Time Pad:\n";
gets(pad);
i=0;
while(i<l) //loop for encryption
{
pt[i]=pt[i]-65;
pad[i]=pad[i]-65;
ct[i]=pt[i]+pad[i];
ct[i]=ct[i]%26;
ct[i]=ct[i]+65;
i++;
}
ct[i]='\0';

cout<<"\nCipher Text is:\n";


puts(ct);
cout<<"\nDecrypted Text is:\n";
i=0;
while(i<l) //loop for decryption
{
ct[i]=ct[i]-65;
pt[i]=ct[i]+26-pad[i];
pt[i]=pt[i]%26;
pt[i]=pt[i]+65;
i++;
}
pt[i]='\0';
puts(pt);
getch();
}

Output:

Program No. 4
Write a program to encrypt and decrypt text using Modified
Caesar Cipher.
Program Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char str[30];
int i,k;
cout<<"|||| MODIFIED CAESAR CIPHER ||||";
cout<<"\n\nEnter Plain Text: ";
gets(str);
cout<<"Enter Key: ";
cin>>k;
i=0;
while(str[i]!='\0') //loop for encryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+k)%26+65
;
i++;
}
cout<<"\nCipher Text is: ";
puts(str);
i=0;
while(str[i]!='\0') //loop for decryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+26-k)%26+65;
i++;
}
cout<<"\nDecrypted Text is: ";
puts(str);
getch();
}

Output:

Program No. 5
Write a program to implement Diffie-Hellman Key Exchange
Algorithm.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
long int g,n,x,y,A,B,K1,K2,t;
cout<<"|||||| DIFFIE-HELLMAN ALGORITHM ||||||";
cout<<"\n\nEnter two large prime numbers: ";
cin>>g;
cin>>n;
cout<<"\nEnter number selected by sender: ";
cin>>x;
cout<<"Enter number selected by receiver: ";
cin>>y;
t=pow(g,x);
A=t%n;
cout<<"\nNumber generated by sender is: "<<A;
t=pow(g,y);
B=t%n;
cout<<"\nNumber generated by receiver is: "<<B;
t=pow(B,x);
K1=t%n;
t=pow(A,y);
K2=t%n;
cout<<"\n\nKey K1= "<<K1;
cout<<"\nKey K2= "<<K2;
getch();
}

Output:

Program No. 6
Write a program to encrypt and decrypt text using Hill Cipher.
Program Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char pt[30],ct[30];
int m[30][30],temp,i,j,k,l;
cout<<"||||||||||| HILL CIPHER |||||||||||";
cout<<"\n\nEnter plain text: ";
gets(pt);
l=strlen(pt);
i=0;
while(i<l) //loop to remove spaces
{
if(pt[i]==' ')
{
for(int j=i;j<l;j++)
{
pt[j]=pt[j+1];
}
l--;
continue;
}
i++;
}
cout<<"\nPlain text after removing spaces is:\n";
puts(pt);
cout<<"\nPlain text single column matrix is:\n";
i=0;
while(i<l)
{
pt[i]=pt[i]-65;
cout<<int(pt[i])<<"\n";
i++;
}
cout<<"\nEnter a "<<l<<"x"<<l<<" matrix:\n";
for(i=0;i<l;i++) //loop to get values of matrix
{
for(j=0;j<l;j++)
{
cin>>m[i][j];

}
}
k=0;
for(i=0;i<l;i++) //loop for encryption
{
temp=0;
for(j=0;j<l;j++)
{
temp=temp+pt[j]*m[i][j];
if(temp>=26)
temp=temp%26;
}
ct[k]=temp;
ct[k]=ct[k]+65;
k++;
}
ct[k]='\0';
cout<<"\nCipher text single column matrix is:\n";
for(i=0;i<l;i++)
{
cout<<int(ct[i])-65<<"\n";
}
cout<<"\nCipher Text is: ";
puts(ct);
getch();
}

Output:

Program No. 7
Write a program, using Switch Case Statement, to implement
various encryption/decryption techniques.
Program Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void caesar();
void rail_fence();
void vernam();
void mod_caesar();
void diffie_hell();
void hill();
void main()
{
clrscr();
int ch;
do
{
cout<<"Available Encryption Techniques are:\n";
cout<<" 1)Caesar Cipher 2)Rail Fence
3)Vernam\n";
cout<<" 4)Modified Caesar 5)Diffie-Hellman 6)Hill Cipher\n";
cout<<" 7)EXIT\n";
cout<<"Enter Choice:";
cin>>ch;
switch(ch)
{
case 1: caesar();
break;
case 2: rail_fence();
break;
case 3: vernam();
break;
case 4: mod_caesar();
break;
case 5: diffie_hell();
break;
case 6: hill();
break;
case 7: goto xyz;
default: cout<<"\nInvalid Choice\n\n";
}
}while(ch!=7);
xyz: cout<<"\nProgram Exiting....";

getch();
}
void caesar()
{
char str[30];
int i;
cout<<"\n |||||||| CAESAR CIPHER ||||||||";
cout<<"\n Enter Plain Text: ";
gets(str);
i=0;
while(str[i]!='\0') //loop for encryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+3)%26+65
;
i++;
}
cout<<" Cipher Text is: ";
puts(str);
i=0;
while(str[i]!='\0') //loop for decryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+26-3)%26+65;
i++;
}
cout<<" Decrypted Text is: ";
puts(str);
cout<<"\n";
}
void rail_fence()
{
char p[50],c[50];
int n,i,j,k,t;
cout<<"\n ||||||||| RAIL FENCE CIPHER |||||||||";
cout<<"\n Enter plain text:\n ";
gets(p);
n=strlen(p);
i=0;
while(i<n) //loop to remove spaces
{
if(p[i]==' ')
{
for(int j=i;j<n;j++)
{

p[j]=p[j+1];
}
n--;
continue;
}
i++;
}
i=0;
j=0;
cout<<" Plain text after removing spaces is:\n
puts(p);
cout<<" Zig-zag sequence is:\n ";
while(i<n) //loop to generate 1st row
{
c[j]=p[i];
cout<<c[j]<<" ";
j++;
i=i+2;
}
cout<<"\n ";
i=1;
while(i<n) //loop to generate 2nd row
{
c[j]=p[i];
cout<<" "<<c[j];
j++;
i=i+2;
}
c[n]='\0';
cout<<"\n Cipher Text is:\n ";
puts(c);
cout<<" Decrypted Text is:\n ";
t=0;
j=0;
k=(n+1)/2;
i=0;
while(i<n) //loop for decryption
{
if(t%2==0)
{
p[i]=c[j];
j++;
}
else
{
p[i]=c[k];
k++;
}
i++;
t++;
}

";

p[n]='\0';
puts(p);
cout<<"\n";
}
void vernam()
{
char pt[30],ct[30],pad[30];
int l,i;
cout<<"\n ||||||||||| VERNAM CIPHER |||||||||||";
cout<<"\n Enter plain text:\n ";
gets(pt);
l=strlen(pt);
i=0;
while(i<l) //loop to remove spaces
{
if(pt[i]==' ')
{
for(int j=i;j<l;j++)
{
pt[j]=pt[j+1];
}
l--;
continue;
}
i++;
}
cout<<" Plain text after removing spaces is:\n
puts(pt);
cout<<" Enter One Time Pad:\n ";
gets(pad);
i=0;
while(i<l) //loop for encryption
{
pt[i]=pt[i]-65;
pad[i]=pad[i]-65;
ct[i]=pt[i]+pad[i];
ct[i]=ct[i]%26;
ct[i]=ct[i]+65;
i++;
}
ct[i]='\0';
cout<<" Cipher Text is:\n ";
puts(ct);
cout<<" Decrypted Text is:\n ";
i=0;
while(i<l) //loop for decryption
{
ct[i]=ct[i]-65;
pt[i]=ct[i]+26-pad[i];

";

pt[i]=pt[i]%26;
pt[i]=pt[i]+65;
i++;
}
pt[i]='\0';
puts(pt);
cout<<"\n";
}
void mod_caesar()
{
char str[30];
int i,k;
cout<<"\n |||| MODIFIED CAESAR CIPHER ||||";
cout<<"\n Enter Plain Text: ";
gets(str);
cout<<" Enter Key: ";
cin>>k;
i=0;
while(str[i]!='\0') //loop for encryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+k)%26+65
;
i++;
}
cout<<" Cipher Text is: ";
puts(str);
i=0;
while(str[i]!='\0') //loop for decryption
{
if(str[i]==' ')
str[i]=' ';
else
str[i]=(str[i]-65+26-k)%26+65;
i++;
}
cout<<" Decrypted Text is: ";
puts(str);
cout<<"\n";
}
void diffie_hell()
{
long int g,n,x,y,A,B,K1,K2,t;
cout<<"\n |||||| DIFFIE-HELLMAN ALGORITHM ||||||";
cout<<"\n Enter two large prime numbers: ";
cin>>g;
cin>>n;
cout<<" Enter number selected by sender: ";

cin>>x;
cout<<" Enter number selected by receiver: ";
cin>>y;
t=pow(g,x);
A=t%n;
cout<<" Number generated by sender is: "<<A;
t=pow(g,y);
B=t%n;
cout<<"\n Number generated by receiver is: "<<B;
t=pow(B,x);
K1=t%n;
t=pow(A,y);
K2=t%n;
cout<<"\n Key K1= "<<K1;
cout<<"\n Key K2= "<<K2;
cout<<"\n\n";
}
void hill()
{
char pt[30],ct[30];
int m[30][30],temp,i,j,k,l;
cout<<"\n ||||||||||| HILL CIPHER |||||||||||";
cout<<"\n Enter plain text: ";
gets(pt);
l=strlen(pt);
i=0;
while(i<l) //loop to remove spaces
{
if(pt[i]==' ')
{
for(int j=i;j<l;j++)
{
pt[j]=pt[j+1];
}
l--;
continue;
}
i++;
}
cout<<" Plain text after removing spaces is:\n ";
puts(pt);
cout<<" Plain text single column matrix is:\n ";
i=0;
while(i<l)
{
pt[i]=pt[i]-65;
cout<<int(pt[i])<<"\n
i++;
}

";

cout<<"Enter a "<<l<<"x"<<l<<" matrix:\n ";


for(i=0;i<l;i++) //loop to get values of matrix
{
for(j=0;j<l;j++)
{
cin>>m[i][j];
}
}
k=0;
for(i=0;i<l;i++) //loop for encryption
{
temp=0;
for(j=0;j<l;j++)
{
temp=temp+pt[j]*m[i][j];
if(temp>=26)
temp=temp%26;
}
ct[k]=temp;
ct[k]=ct[k]+65;
k++;
}
ct[k]='\0';
cout<<" Cipher text single column matrix is:\n
for(i=0;i<l;i++)
{
cout<<int(ct[i])-65<<"\n ";
}
cout<<"Cipher Text is: ";
puts(ct);
cout<<"\n";
}

";

Output:

You might also like