Lab Manualof IS
Lab Manualof IS
Technology
Lab Manual
B.E Semester: - VII
Subject: - Information Security (3170720)
CERTIFICATE
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>
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];
void decryption() {
int i, j, k;
getKeyMessage();
inverse();
printf("\n");
}
void getKeyMessage() {
int i, j;
char msg[3];
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;
//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);
//Swapper
if(i!= 15){
swap(left, right);
}
cout<<"Round "<<i+1<<" "<<bin2hex(left)<<" "
<<bin2hex(right)<<" "<<rk[i]<<endl;
}
//Combination
string combine= left+right;
//Key Generation
//Hex to binary
key= hex2bin(key);
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;
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:
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
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:
Packet Analyzing:
Packet Analysing: