0% found this document useful (0 votes)
10 views

Lab Manualof IS

The document describes the implementation of various classical encryption techniques like Caesar cipher, Rail Fence cipher and Playfair cipher in C++. It provides code snippets to encrypt and decrypt text using these ciphers. The document also contains sample outputs of the implemented encryption and decryption algorithms.

Uploaded by

parmpatel302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab Manualof IS

The document describes the implementation of various classical encryption techniques like Caesar cipher, Rail Fence cipher and Playfair cipher in C++. It provides code snippets to encrypt and decrypt text using these ciphers. The document also contains sample outputs of the implemented encryption and decryption algorithms.

Uploaded by

parmpatel302
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Apollo Institute of Engineering and

Technology

Lab Manual
B.E Semester: - VII
Subject: - Information Security (3170720)
CERTIFICATE

This is to certify that Mr./Ms.___________________________


Enrollment No.______________________ Semester_________
Branch_______________________ has satisfactorily completed his/her
term work in Course of “Information Security” with subject code
3170720 for the academic term 2022-2023.

Staff In-charge Date of Submission Head of Department


INDEX
Page
Sr. No. Practical Aim Date Sign
No.
1. Implement Caesar cipher Encryption - Decryption.
2. Implement Monoalphabetic cipher encryption.
3. Implement Playfair cipher encryption-decryption.

4. Implement Polyalphabetic cipher encryption-


decryption.
5. Implement Hill cipher encryption-decryption.
6. To implement Simple DES or AES.
7. Implement Diffie-Hellman Key exchange Method.
8. Implement RSA encryption-decryption algorithm.

9. Write a program to generate SHA-1 hash.

10. Implement a digital signature algorithm.

11. Perform various encryption-decryption techniques


with cryptool.
12. Study and use the Wireshark for the various network
protocols.
PRACTICAL 1
AIM: Implement Caesar cipher encryption-decryption

ENCRYPTION:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
int k;
cout<<"Enter the Key:";
cin>>k;
cout<<"Enter the String:";
cin>>a;

for(int i=0;i<a.length();i++)
{
a[i]=a[i]+k;
if(a[i]>'z')
a[i]=a[i]-26;
}
cout<<a;
return 0;

OUTPUT:

DECRYPTION:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
int k;
cout<<"Enter the Key:";
cin>>k;
cout<<"Enter the String:";
cin>>a;

for(int i=0;i<a.length();i++)
{
a[i]=a[i]-k;
if(a[i]<'a')
a[i]=a[i]+26;
}
cout<<a;
return 0;

OUTPUT:
PRACTICAL 2
AIM: Implement Rail Fence cipher encryption-decryption

ENCRYPTION:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string a;
vector<char> ans;
int k;
cout<<"Enter the Key:";
cin>>k;
cout<<"Enter the String:";
cin>>a;
int n=(k-1)*2;
if(k==1)
cout<<a;
else
{
for(int i=0;i<k;i++)
{

int dif1=n-i*2;
int dif2=i*2;
//cout<<dif1<<" "<<dif2;
if(dif1==0 || dif2==0)
{
int temp=i;
while(temp<a.length())
{
ans.push_back(a[temp]);
temp=temp+dif1+dif2;
}
}
else
{
int temp=i,flag=0;

while(temp<a.length())
{
if(flag==0)
{
ans.push_back(a[temp]);
temp=temp+dif1;
flag=1;
}
else
{
ans.push_back(a[temp]);
temp=temp+dif2;
flag=0;
}
}
}
}
cout<<"Encode String is:";
for(auto &c : ans)
{
cout<<c;
}
}
return 0;
}

OUTPUT:

DECRYPTION:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i,j,k,len,rails,count,code[100][1000];
char str[1000];
printf("Enter a Secret Message\n");
gets(str);
len=strlen(str);
printf("Enter number of rails\n");
scanf("%d",&rails);
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
code[i][j]=0;
}
}
count=0;
j=0;
while(j<len)
{
if(count%2==0)
{
for(i=0;i<rails;i++)
{
//strcpy(code[i][j],str[j]);
code[i][j]=-1;
j++;
}
}
else
{

for(i=rails-2;i>0;i--)
{
code[i][j]=-1;
j++;
}
}
count++;
}
k=0;
for(i=0;i<rails;i++)
{
for(j=0;j<len;j++)
{
if(code[i][j]!=0)
{
code[i][j]=(int)str[k];
k++;
}
}
}
count=0;
j=0;
while(j<len)
{
if(count%2==0)
{
for(i=0;i<rails;i++)
{
printf("%c",code[i][j]);
j++;
}
}
else
{
for(i=rails-2;i>0;i--)
{
printf("%c",code[i][j]);
j++;
}
}
count++;
}
printf("\n");
}

OUTPUT:
PRACTICAL 3
AIM: Implement Playfair cipher encryption-decryption.

ENCRYPTION:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cout<<"enter the key:";
cin>>a;
int vis[26]={0},j=0,k=0;
char mat[5][5];
for(int i=0;i<a.length();i++)
{
if(k==5)
{
k=0;
j++;
}
int temp=a[i]-'a';
if(vis[temp]==0)
{
mat[j][k]=a[i];
k++;
vis[temp]=1;
}
}
for(int i=0;i<25;i++)
{
if(k==5)
{
k=0;
j++;
}
if(vis[i]==0)
{
mat[j][k]=(char)'a'+i;
k++;
vis[i]=1;
}
}
char t1=mat[j][k-1];
char t2=(char)t1+1;
//cout<<t1<<t2;
mat[4][4]=(char)'0';
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
string b;
cout<<"Enter the string you want to encode:";
cin>>b;
for(int i=0;i<b.length();i++)
{
if(b[i]==t1 || b[i]==t2)
b[i]='0';
if(i+1<b.length() && b[i]==b[i+1] && i%2==0)
{
b.insert(i+1,"x");
}
}
if(b.length()%2==1)
b.insert(b.length(),"x");
//cout<<b;
char ans[b.length()];
for(int k=0;k<b.length();k=k+2)
{
int ti,tj,t1i,t1j;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(mat[i][j]==b[k])
{
ti=i;
tj=j;
}
if(mat[i][j]==b[k+1])
{
t1i=i;
t1j=j;
}
}
}
if(ti==t1i)
{
ans[k]=mat[ti][(tj+1)%5];
ans[k+1]=mat[ti][(t1j+1)%5];
}
else if(tj==t1j)
{
ans[k]=mat[(ti+1)%5][tj];
ans[k+1]=mat[(t1i+1)%5][tj];
}
else
{
ans[k]=mat[ti][t1j];
ans[k+1]=mat[t1i][tj];
}
}
cout<<"Encode String is:";
for(int i=0;i<b.length();i++)
cout<<ans[i];
return 0;
}

OUTPUT:

DECRYPTION:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string a;
cout<<"enter the key:";
cin>>a;
int vis[26]={0},j=0,k=0;
char mat[5][5];
for(int i=0;i<a.length();i++)
{
if(k==5)
{
k=0;
j++;
}
int temp=a[i]-'a';
if(vis[temp]==0)
{
mat[j][k]=a[i];
k++;
vis[temp]=1;
}
}
for(int i=0;i<25;i++)
{
if(k==5)
{
k=0;
j++;
}
if(vis[i]==0)
{
mat[j][k]=(char)'a'+i;
k++;
vis[i]=1;
}
}
char t1=mat[j][k-1];
char t2=(char)t1+1;
//cout<<t1<<t2;
mat[4][4]=(char)'0';
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
string b;
cout<<"Enter the string you want to decode:";
cin>>b;
char ans[b.length()];
for(int k=0;k<b.length();k=k+2)
{
int ti,tj,t1i,t1j;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(mat[i][j]==b[k])
{
ti=i;
tj=j;
}
if(mat[i][j]==b[k+1])
{
t1i=i;
t1j=j;
}
}
}
if(ti==t1i)
{
ans[k]=mat[ti][(tj+4)%5];
ans[k+1]=mat[ti][(t1j+4)%5];
}
else if(tj==t1j)
{
ans[k]=mat[(ti+4)%5][tj];
ans[k+1]=mat[(t1i+4)%5][tj];
}
else
{
ans[k]=mat[ti][t1j];
ans[k+1]=mat[t1i][tj];
}
}
cout<<"Decode String is:";
for(int i=0;i<b.length();i++)
cout<<ans[i];
return 0;
}

OUTPUT:
PRACTICAL 4
AIM: Implement Polyalphabetic cipher encryption-decryption

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void take_input(char *text){
int i=0;
char c;
while(1){
c=getch();
if(c==13){
text[i]='\0';
break; }
else if(c==8){
putch('\b');
putch(' ');
putch('\b');
i--; }
else if(c!=32){
if((c>=65 && c<=90) || (c>=97 && c<=122)){
printf("%c",c);
text[i]=c;
i++; }
}
}
}
void encrypt(){
int i=0,j=0,pt_len,key_len,temp;
char c,pt[200],ct[200],key[200];
printf("Enter text to encrypt:\n");
take_input(&pt);
printf("\nEnter key to encrypt:\n");
take_input(&key);
pt_len=strlen(pt);
key_len=strlen(key);
for(i=0;i<pt_len;i++){
if(isupper(pt[i]))
temp=pt[i]-65;
else
temp=pt[i]-97;
if(isupper(key[j]))
temp=temp+(key[j]-65);
else
temp=temp+(key[j]-97);
ct[i]=(temp%26)+97;
j=(j+1)%key_len; }
ct[i]='\0';
printf("\nEncrypted message:\t%s\n",ct);
}
void decrypt(){
int i=0,j=0,ct_len,key_len,temp;
char c,pt[200],ct[200],key[200];
printf("Enter encrypted text:\n");
take_input(&ct);
printf("\nEnter key to decrypt:\n");
take_input(&key);
ct_len=strlen(ct);
key_len=strlen(key);
for(i=0;i<ct_len;i++){
if(isupper(ct[i]))
temp=ct[i]-65;
else
temp=ct[i]-97;
if(isupper(key[j]))
temp=temp-(key[j]-65);
else
temp=temp-(key[j]-97);
pt[i]=((temp+26)%26)+97;
j=(j+1)%key_len; }
pt[i]='\0';
printf("\nDecrypted message:\t%s\n",pt);
}
void main(){
int choice;
while(1){
printf("Enter your choice:\n1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
encrypt();
break;
case 2:
decrypt();
break;
case 3:
break;
default:
printf("Enter valid choice...\n");
}
if(choice==3)
break;
}
}

OUTPUT:
PRACTICAL 5
AIM: Implement Hill cipher encryption-decryption

PROGRAM:
#include<stdio.h>
#include<math.h>

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption(); //encrypts the message


void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix

int main() {
int choice;
while(1)
{
printf("Enter Your choice:\n1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
encryption();
break;
}
case 2:
{
decryption();
break;
}
case 3:
break;
default:
{
printf("Enter a valid choice\n");
}
}
if(choice==3)
return 0;
}
}
void encryption() {
int i, j, k;
getKeyMessage();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];

printf("\nEncrypted string is: ");


for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));
printf("\n");
}

void decryption() {
int i, j, k;
getKeyMessage();
inverse();

for(i = 0; i < 3; i++)


for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];

printf("\nDecrypted string is: ");


for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));

printf("\n");
}

void getKeyMessage() {
int i, j;
char msg[3];

printf("Enter 3x3 matrix for key (It should be inversible):\n");

for(i = 0; i < 3; i++)


for(j = 0; j < 3; j++) {
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
}
printf("\nEnter a 3 letter string: ");
scanf("%s", msg);

for(i = 0; i < 3; i++)


mes[i][0] = msg[i] - 97;
}
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
{
printf("\n");
for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
printf("\t %d",b[i][j]);
}
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];

for(j = 0; j < 3; j++) {


if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
printf("\n\nInverse Matrix is:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);
printf("\n");
}
}
OUTPUT:
PRACTICAL 6
AIM: Implement Simple DES or AES.

PROGRAM:
#include<bits/stdc++.h>
#include <tr1/unordered_map>
using namespace std::tr1;
using namespace std;
string hex2bin(string s){
// hexadecimal to binary conversion
unordered_map<char, string> mp;
mp['0']= "0000";
mp['1']= "0001";
mp['2']= "0010";
mp['3']= "0011";
mp['4']= "0100";
mp['5']= "0101";
mp['6']= "0110";
mp['7']= "0111";
mp['8']= "1000";
mp['9']= "1001";
mp['A']= "1010";
mp['B']= "1011";
mp['C']= "1100";
mp['D']= "1101";
mp['E']= "1110";
mp['F']= "1111";
string bin="";
for(int i=0; i<s.size(); i++){
bin+= mp[s[i]];
}
return bin;
}
string bin2hex(string s){
// binary to hexadecimal conversion
unordered_map<string, string> mp;
mp["0000"]= "0";
mp["0001"]= "1";
mp["0010"]= "2";
mp["0011"]= "3";
mp["0100"]= "4";
mp["0101"]= "5";
mp["0110"]= "6";
mp["0111"]= "7";
mp["1000"]= "8";
mp["1001"]= "9";
mp["1010"]= "A";
mp["1011"]= "B";
mp["1100"]= "C";
mp["1101"]= "D";
mp["1110"]= "E";
mp["1111"]= "F";
string hex="";
for(int i=0; i<s.length(); i+=4){
string ch="";
ch+= s[i];
ch+= s[i+1];
ch+= s[i+2];
ch+= s[i+3];
hex+= mp[ch];
}
return hex;
}
string permute(string k, int* arr, int n){
string per="";
for(int i=0; i<n ; i++){
per+= k[arr[i]-1];
}
return per;
}
string shift_left(string k, int shifts){
string s="";
for(int i=0; i<shifts; i++){
for(int j=1; j<28; j++){
s+= k[j];
}
s+= k[0];
k= s;
s="";
}
return k;
}
string xor_(string a, string b){
string ans= "";
for(int i=0; i<a.size(); i++){
if(a[i]==b[i]){
ans+= "0";
}else{
ans+= "1";
}
}
return ans;
}
string encrypt(string pt, vector<string> rkb, vector<string> rk){
//Hexadecimal to binary
pt= hex2bin(pt);
//Initial Permutation Table
int initial_perm[64]=
{ 58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7
};
//Initial Permutation
pt= permute(pt, initial_perm, 64);
cout<<"After initial permutation: "<<bin2hex(pt)<<endl;

//Splitting
string left= pt.substr(0, 32);
string right= pt.substr(32, 32);
cout<<"After splitting: L0="<<bin2hex(left)
<<" R0="<<bin2hex(right)<<endl;

//Expansion D-box Table


int exp_d[48]=
{ 32,1,2,3,4,5,4,5,
6,7,8,9,8,9,10,11,
12,13,12,13,14,15,16,17,
16,17,18,19,20,21,20,21,
22,23,24,25,24,25,26,27,
28,29,28,29,30,31,32,1
};
//S-box Table
int s[8][4][16]=
{{
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
},
{
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
},
{
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
},
{
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
},
{
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3 },
{
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13 },
{
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12 },
{
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11 }};

//Straight Permutation Table


int per[32]=
{ 16,7,20,21,
29,12,28,17,
1,15,23,26,
5,18,31,10,
2,8,24,14,
32,27,3,9,
19,13,30,6,
22,11,4,25 };
cout<<endl;
for(int i=0; i<16; i++){
//Expansion D-box
string right_expanded= permute(right, exp_d, 48);

//XOR RoundKey[i] and right_expanded


string x= xor_(rkb[i], right_expanded);

//S-boxes
string op="";
for(int i=0;i<8; i++){
int row= 2*int(x[i*6]-'0')+ int(x[i*6 +5]-'0');
int col= 8*int(x[i*6 +1 ]-'0')+ 4*int(x[i*6 +2]-'0')+
2*int(x[i*6 +3]-'0')+ int(x[i*6 +4]-'0');
int val= s[i][row][col];
op+= char(val/8+ '0');
val= val%8;
op+= char(val/4+ '0');
val= val%4;
op+= char(val/2+ '0');
val= val%2;
op+= char(val+ '0');
}
//Straight D-box
op= permute(op, per, 32);

//XOR left and op


x= xor_(op, left);
left= x;

//Swapper
if(i!= 15){
swap(left, right);
}
cout<<"Round "<<i+1<<" "<<bin2hex(left)<<" "
<<bin2hex(right)<<" "<<rk[i]<<endl;
}
//Combination
string combine= left+right;

//Final Permutation Table


int final_perm[64]=
{ 40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25 };
//Final Permutation
string cipher= bin2hex(permute(combine, final_perm, 64));
return cipher;
}
int main(){
// pt is plain text
string pt, key;
cout<<"Enter plain text(in hexadecimal): ";
cin>>pt;
cout<<"Enter key(in hexadecimal): ";
cin>>key;

//Key Generation

//Hex to binary
key= hex2bin(key);

//Parity bit drop table


int keyp[56]=
{ 57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4 };

//getting 56 bit key from 64 bit using the parity bits


key= permute(key, keyp, 56);// key without parity

//Number of bit shifts


int shift_table[16]=
{ 1, 1, 2, 2,
2, 2, 2, 2,
1, 2, 2, 2,
2, 2, 2, 1 };

//Key- Compression Table


int key_comp[48]=
{ 14,17,11,24,1,5,
3,28,15,6,21,10,
23,19,12,4,26,8,
16,7,27,20,13,2,
41,52,31,37,47,55,
30,40,51,45,33,48,
44,49,39,56,34,53,
46,42,50,36,29,32 };
string left= key.substr(0, 28); //Splitting
string right= key.substr(28, 28);
vector<string> rkb;//rkb for RoundKeys in binary
vector<string> rk;//rk for RoundKeys in hexadecimal
for(int i=0; i<16; i++){

left= shift_left(left, shift_table[i]); //Shifting


right= shift_left(right, shift_table[i]);

string combine= left + right; //Combining


//Key Compression
string RoundKey= permute(combine, key_comp, 48);
rkb.push_back(RoundKey);
rk.push_back(bin2hex(RoundKey));
}
cout<<"\nEncryption:\n\n";
string cipher= encrypt(pt, rkb, rk);
cout<<"\nCipher Text: "<<cipher<<endl;
cout<<"\nDecryption\n\n";
reverse(rkb.begin(), rkb.end());
reverse(rk.begin(), rk.end());
string text= encrypt(cipher, rkb, rk);
cout<<"\nPlain Text: "<<text<<endl;
}
OUTPUT:
PRACTICAL 7
AIM: Implement Diffi-Hellmen key Exchange method.

PROGRAM:
#include<stdio.h>
#include<math.h>
long long int power(long long int a, long long int b,long long int P){
if (b == 1)
return a;
else
return (((long long int)pow(a, b)) % P);
}
int main(){
long long int P, G, x, a, y, b, ka, kb;
printf("Enter first prime number:\t");
scanf("%lld",&P);
printf("Enter second prime number:\t");
scanf("%lld",&G);
printf("Enter private key for Alice :\t");
scanf("%lld",&a);
printf("Enter private key for Bob :\t");
scanf("%lld",&b);
x = power(G, a, P);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
printf("Secret key for the Alice(Ka) : %lld\n", ka);
printf("Secret Key for the Bob(Kb) : %lld\n", kb);
return 0;
}

OUTPUT:
PRACTICAL 8
AIM: Implement RSA encryption-decryption algorithm.

PROGRAM:
#include<stdio.h>
#include<math.h>
void encrypt();
void decrypt();
long int findGCD(long int,long int);
long int multipicative_inverse(long int,long int);
void key_pair(long int e,long int n,long int d) {
printf("\nEncryption key(e,n):%ld\t%ld\n",e,n);
printf("\nDecryption key(d,n):%ld\t%ld\n",d,n);
}
void encrypt(){
long int pt,p,q,n,fn,e=2,temp,d,ct;
printf("Enter your message to encrypt:\n");
scanf("%d",&pt);
printf("Enter two prime numbers p and q:\n");
scanf("%ld %ld",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
temp=findGCD(e,fn);
while(temp!=1) {
e++;
temp=findGCD(e,fn);
//printf("e=%ld\ttemp=%d\t",e,temp);
}
//printf("\ne=%ld\n",e);
d=multipicative_inverse(fn,e);
if(d==0) {
printf("\nrsa is not possible for given p and q.\n");
exit(0);
}
ct=fmod(pow(pt,e),n);
key_pair(e,n,d);
printf("\nencrypted text:\t%ld\n",ct);

}
void decrypt(){
long int pt,d,ct,n;
printf("Enter message to decrypt:\n");
scanf("%ld",&ct);
printf("Enter key pair(d,n):\n");
scanf("%ld %ld",&d,&n);
pt=fmod(pow(ct,d),n);
printf("Decrypted message:\t%ld",pt);
}
long int multipicative_inverse(long int m,long int b) {
long int a1=1,a2=0,a3=m,b1=0,b2=1,b3=b,t1,t2,t3,q;
top:
if(b3==0) return 0;
else if(b3==1) {
if(b2<0) return (b2+m);
else return b2;
}
q=a3/b3;
t1=b1;
t2=b2;
t3=b3;
b1=a1-(q*b1);
b2=a2-(q*b2);
b3=a3-(q*b3);
a1=t1;
a2=t2;
a3=t3;
//printf("\nb2=%ld\n",b2);
goto top;
}
long int findGCD(long int e,long int fn) {
long int c=e,d=fn;
long int r;
while(d!=0) {
r=c%d;
c=d;
d=r;
}
return c;
}
int main(){
int choice;
while(1){
printf("\nEnter your choice:\t1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice) {
case 1: {
encrypt();
break;
}
case 2: {
decrypt();
break;
}
case 3:
break;
default:
printf("\nInvalid choice,Enter valid choice.\n");
}
if(choice==3)
break;
}
return 0;
}

OUTPUT:
PRACTICAL 9
AIM: Write a program to generate SHA-1 hash.

PROGRAM:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#define rotateleft(x,n) ((x<<n) | (x>>(32-n)))
#define rotateright(x,n) ((x>>n) | (x<<(32-n)))
using namespace std;
void SHA1(unsigned char * str1)
{
unsigned long int h0,h1,h2,h3,h4,a,b,c,d,e,f,k,temp;
h0 = 0x67452301;
h1 = 0xEFCDAB89;
h2 = 0x98BADCFE;
h3 = 0x10325476;
h4 = 0xC3D2E1F0;

unsigned char * str;


int i,j;
str = (unsigned char *)malloc(strlen((const char *)str1)+100);
strcpy((char *)str,(const char *)str1);
int current_length = strlen((const char *)str);
int original_length = current_length;
str[current_length] = 0x80;
str[current_length + 1] = '\0';
char ic = str[current_length];
current_length++;
int ib = current_length % 64;
if(ib<56)
ib = 56-ib;
else
ib = 120 - ib;
for(int i=0;i < ib;i++)
{
str[current_length]=0x00;
current_length++;
}
str[current_length + 1]='\0';
for(i=0;i<6;i++)
{
str[current_length]=0x0;
current_length++;
}
str[current_length] = (original_length * 8) / 0x100 ;
current_length++;
str[current_length] = (original_length * 8) % 0x100;
current_length++;
str[current_length+i]='\0';
int number_of_chunks = current_length/64;
unsigned long int word[80];
for(i=0;i<number_of_chunks;i++)
{
for(int j=0;j<16;j++)
{
word[j] = str[i*64 + j*4 + 0] * 0x1000000 + str[i*64 + j*4 + 1] * 0x10000 +
str[i*64 + j*4 + 2] * 0x100 + str[i*64 + j*4 + 3];
}
for(j=16;j<80;j++)
word[j] = rotateleft((word[j-3] ^ word[j-8] ^ word[j-14] ^ word[j-16]),1);
a = h0;
b = h1;
c = h2;
d = h3;
e = h4;
for(int m=0;m<80;m++)
{
if(m<=19)
{
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if(m<=39)
{
f = b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if(m<=59)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
}
else
{
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = (rotateleft(a,5) + f + e + k + word[m]) & 0xFFFFFFFF;
e = d;
d = c;
c = rotateleft(b,30);
b = a;
a = temp;
}
h0 = h0 + a;
h1 = h1 + b;
h2 = h2 + c;
h3 = h3 + d;
h4 = h4 + e;
}
printf("\n\n");
printf("Hash: %x %x %x %x %x",h0, h1, h2, h3, h4);
printf("\n\n");
}
int main()
{
char str[100];
cout<<"Enter the String: ";
cin>>str;
SHA1((unsigned char *)str);
return 0;
}

OUTPUT:
PRACTICAL 10
AIM: Implement a digital signature algorithm.

PROGRAM:
#include<stdio.h>
#include<math.h>
void encrypt();
void decrypt();
long int findGCD(long int,long int);
long int multipicative_inverse(long int,long int);
void key_pair(long int e,long int n,long int d){
printf("\nEncryption key(Private Key Pair)(e,n):%ld\t%ld\n",d,n);
printf("\nDecryption key(Public Key Pair)(d,n):%ld\t%ld\n",e,n);
}
void encrypt(){
long int pt,p,q,n,fn,e=2,temp,d,ct;
printf("Enter your signature to encrypt:\n");
scanf("%d",&pt);
printf("Enter two prime numbers p and q:\n");
scanf("%ld %ld",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
temp=findGCD(e,fn);
while(temp!=1){
e++;
temp=findGCD(e,fn);
}
d=multipicative_inverse(fn,e);
if(d==0){
printf("\nkey generation is not possible for given p and q.\n");
// exit(0);
}
ct=fmod(pow(pt,d),n);
key_pair(e,n,d);
printf("\nGenerated encrypted signature text:\t%ld\n",ct);
}
void decrypt(){
long int pt,d,ct,e,n;
printf("Enter encrypted signature to verify:\n");
scanf("%ld",&ct);
printf("Enter public key pair(e,n):\n");
scanf("%ld %ld",&e,&n);
pt=fmod(pow(ct,e),n);
printf("Decrypted signature:\t%ld",pt);
}
long int multipicative_inverse(long int m,long int b){
long int a1=1,a2=0,a3=m,b1=0,b2=1,b3=b,t1,t2,t3,q;
top:
if(b3==0)
return 0;
else if(b3==1){
if(b2<0)
return (b2+m);
else
return b2;
}
q=a3/b3;
t1=b1;
t2=b2;
t3=b3;
b1=a1-(q*b1);
b2=a2-(q*b2);
b3=a3-(q*b3);
a1=t1;
a2=t2;
a3=t3;
//printf("\nb2=%ld\n",b2);
goto top;
}
long int findGCD(long int e,long int fn){
long int c=e,d=fn;
long int r;
while(d!=0){
r=c%d;
c=d;
d=r;
}
return c;
}
int main(){
int choice;
while(1){
printf("\nEnter your choice:\t1.Generate signature\t2.Verify Signature\
t3.Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:{
encrypt();
break;
}
case 2:{
decrypt();
break;
}
case 3:{
break;
}
default:{
printf("\nInvalid choice,Enter valid choice.\n");
}
}
if(choice==3)
break;
}
return 0;
}

OUTPUT:
PRACTICAL 11
AIM: Perform various encryption-decryption techniques with cryptool.

FILE TO BE ENCRYPTED:

1. CAESAR CIPHER
ENCRYPTION:
ENCRYPTED OUTPUT:

DECRYPTED OUTPUT:

2. MONOALPHABETIC SUBSTITUTION:
ENCRYPTION
ENCRYPTED OUTPUT:

3. PLAYFAIR CIPHER
ENCRYPTION:
4. HILL CIPHER
ENCRYPTION:

5. DES ALGORITHM

ENCRYPTION
6. AES ALGORITHM

ENCRYPTION:

7. RSA ALGORITHM

ENCRYPTION:
DECRYPTION:

8. VIGENERE ENCRYPTION:

9. RAIL FENCE CIPHER


10. DES ALGORITHM
PRACTICAL 12
AIM: Study and use the Wireshark for the various network protocols.

Description:
Wireshark is a free and open source packet analyzer. It is used for network troubleshooting,
analysis, software and communications protocol development, and education. Originally named
Ethereal, the project was renamed Wireshark in May 2006 due to trademark issues.
Wireshark is cross-platform, using the Qt widget toolkit in current releases to implement its user
interface, and using pcap to capture packets; it runs on Linux, macOS, BSD, Solaris, some other
Unix-like operating systems, and Microsoft Windows.
There is also a terminal-based (non-GUI) version called TShark. Wireshark, and the other programs
distributed with it such as TShark, are free software, released under the terms of the GNU General
Public License.
STUDING TCP/UDP USING WIRESHARK

 Internet Protocol

The Internet Protocol (IP) is


the method or protocol by
which data is sent from one
computer to another on the
Internet. Each computer
(known as a host) on the
Internet has at least one IP
address that uniquely identifies
it from all other computers on
the Internet.

Packet
Analysing:
TCP
Frame
format:
The

Transmission Control
Protocol (TCP) is a core
protocol of the Internet
protocol suite. It
originated in the initial network implementation in which it complemented the Internet Protocol (IP).
Therefore, the entire suite is commonly referred to as TCP/IP.

Packet Analysing:

UDP frame format:


UDP uses a simple connectionless transmission model with a minimum of protocol mechanism. It has
no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to
the user's program.

Packet Analyzing:

Address Resolution Protocol


(ARP)
Address Resolution Protocol (ARP)
is one of the major protocol in the
TCP/IP suit and the purpose of
Address Resolution Protocol (ARP)
is to resolve an IPv4 address (32 bit
Logical Address) to the physical
address (48 bit MAC Address). Network Applications at the Application Layer use IPv4 Address to
communicate with another device.

Packet Analysing:

You might also like