NS Lab Report
NS Lab Report
NS Lab Report
TRISHAL, MYMENSINGH
Lab Report
Course Name: Network Security Lab
Course Code: CSE-436
Submitted To
Associate Professor
Dept. of CSE,JKKNIU
Submitted By
Deena Faria
Roll: 17102005
Reg:5682
Session : 2016-17
Objectives:
Algorithm:
Step 1: Start
Step 2: Input a string
Step 3: Input an Integer between 0-25 denoting the required shift.
Step 4: Traverse the given text one character at a time.
Step 5: For each character, transform the given character as per the rule,
depending on whether we’re encrypting or decrypting the text.
Step 7: Stop
Code:
#include<iostream>
void input();
int main()
int i,j,len,choice;
string text;
int key;
cout<<"Enter text:";
getline(cin,text);
cout<<"Enter key:";
cin>>key;
do{
cin>>choice;
switch(choice){
case 1:
text=encryption(text,key);
cout<<text;
break;
case 2:
text=decryption(text,key);
cout<<text;
break;
case 3:
cout<<"Exit";
break;
default:
break;
while(choice!=3);
return 0;
int i;
string res="";
for(int i=0;i<text.length();i++)
if(isupper(text[i]))
res+=char(int(text[i]+key-65)%26+65);
else if(islower(text[i]))
res+=char(int(text[i]+key-97)%26+97);
res+=' ';
}
return res;
string res="";
for(int i=0;i<text.length();i++)
if(isupper(text[i]))
res+=char(int(text[i]-key-65)%26+65);
else if(islower(text[i]))
res+=char(int(text[i]-key-97)%26+97);
res+=' ';
return res;
Key=2.
Output:
Objective:
Algorithm:
Step 1: Start.
Step 7: Stop
Code:
#include<iostream>
int main()
string text;
cout<<"Enter text:";
getline(cin,text);
crypto(text);
return 0;
for(int key=0;key<26;key++){
string res="";
for(int i=0;i<text.length();i++)
if(isupper(text[i]))
res+=char(int(text[i]+key-65)%26+65);
else if(islower(text[i]))
res+=char(int(text[i]+key-97)%26+97);
res+=' ';
}
cout<<"Key"<<key<<" = "<<res;
cout<<"\n";
Output:
Objective:
Algorithm:
The key square is a 5×5 grid of alphabets that acts as the key for
encrypting the plaintext. Each of the 25 alphabets must be unique and one
letter of the alphabet (usually J) is omitted from the table (as the table can
hold only 25 alphabets). If the plaintext contains J, then it is replaced by
I.
The initial alphabets in the key square are the unique alphabets of the key
in the order in which they appear followed by the remaining letters of the
alphabet in order.
The plaintext is split into pairs of two letters (digraphs). If there is an odd
number of letters, a Z is added to the last letter.
The key square is a 5×5 grid of alphabets that acts as the key for encrypting the
plaintext. Each of the 25 alphabets must be unique and one letter of the alphabet
(usually J) is omitted from the table (as the table can hold only 25 alphabets). If
the plaintext contains J, then it is replaced by I.
The initial alphabets in the key square are the unique alphabets of the key in the
order in which they appear followed by the remaining letters of the alphabet in
order.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
int i;
if(isupper(plain[i]))
plain[i]=tolower(plain[i]);
int i, count = 0;
plain[count++] = plain[i];
plain[count] = '\0';
return count;
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
if (dicty[k] == 0) {
j++;
if (j == 5) {
i++;
j = 0;
}
}
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
int mod5(int a)
return (a % 5);
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
return ptrs;
int i, a[4];
if (a[0] == a[2]) {
}
else if (a[1] == a[3]) {
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
int i, a[4];
if (a[0] == a[2]) {
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
int main()
int choice;
printf("Enter text:");
gets(str);
printf("Enter key:");
gets(key);
do
scanf("%d",&choice);
switch(choice){
case 1:
encryption(str,key);
break;
case 2:
decryption(str,key);
break;
case 3:
printf("Exit");
break;
default:
break;
while(choice!=3);
return 0;
Input:
Text=Bangladesh
Key=monarchy
Output:
Objective:
Algorithm:
Step-4: Plaintext P = p1 p2 ... pn, blocked with the same size as the row or
column column K
Step-8: Change the result of step 7 into the alphabet using alphabetical
correspondence with numeric in step 1 to obtain the ciphertext
Code:
#include<stdio.h>
#include<math.h>
void encryption();
void decryption();
void inverse();
int main()
int i, j,choice;
char text[3];
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
scanf("%s", text);
do{
scanf("%d",&choice);
switch(choice){
case 1:
encryption();
break;
case 2:
decryption();
break;
case 3:
printf("Exit");
break;
default:
while(choice!=3);
return 0;
void encryption() {
int i, j, k;
void decryption() {
int i, j, k;
inverse();
printf("\n");
void inverse() {
int i, j, k;
float p, q;
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
p = c[i][k];
q = c[k][k];
printf("\n");
Input:
Key matrix:
6 24 1
13 16 10
20 17 15
Text=act
Output:
Objective:
Algorithm:
For encryption:
Step-1: Start.
The plaintext (P) and key (K) are added modulo 26.
Step-5: Stop.
For decryption:
Step-1: Start.
Where, Di denotes the offset of the i-th character of the plaintext. Like offset
of A is 0 and of B is 1 and so on.
Step-5: Stop.
Code:
#include<iostream>
int i,j,choice,keylen;
string text;
string key;
string newkey;
int main()
cout<<"Enter text:";
getline(cin,text);
cout<<"Enter key:";
cin>>key;
int textlen=text.length();
keylen=key.length();
newkey=key_gen(text,key,textlen);
cout<<"\nNewkey:"<<newkey;
do
cin>>choice;
switch(choice){
case 1:
text=encryption(text,newkey,textlen);
cout<<text;
break;
case 2:
text=decryption(text,newkey,textlen);
cout<<text;
break;
case 3:
cout<<"Exit";
break;
default:
break;
while(choice!=3);
return 0;
}
char newkey[textlen],res[textlen];
for(i=0,j=0;i<textlen;++i,++j)
if(j==keylen)
j=0;
newkey[i]=key[j];
if(text[i]==' '){
newkey[i]=' ';
j-=1;
newkey[i]='\0';
return newkey;
char res[textlen];
for(i=0;i<textlen;++i)
if(islower(text[i])||islower(newkey[i])){
text[i]=toupper(text[i]);
newkey[i]=toupper(newkey[i]);
res[i]=((text[i]+newkey[i])%26)+'A';
if(text[i]==' ')
res[i]=' ';
res[i]='\0';
return res;
char res[textlen];
for(i=0;i<textlen;++i)
if(islower(text[i])||islower(newkey[i])){
text[i]=toupper(text[i]);
newkey[i]=toupper(newkey[i]);
res[i]=(((text[i]-newkey[i])+26)%26)+'A';
if(text[i]==' ')
res[i]=' ';
}
res[i]='\0';
return res;
Key=pen
Output:
Objective:
Algorithm:
Step-4: Choose an integer e such that 1 < e <φ ( n ), and e is co-prime to φ ( n )i.e., e
and φ ( n ) share no factors other than 1; gcd(e,φ ( n ))=1.
c=me mod n
#include<iostream>
#include<math.h>
int t;
while(1) {
t= a%b;
if(t==0)
return b;
a = b;
b= t;
int main() {
double message;
cout<<"Enter message:";
cin>>message;
double p = 23;
double q = 13;
double n=p*q;
double track;
double phi= (p-1)*(q-1);
//public key
double e=2;
//for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are
coprime.
while(e<phi) {
track = gcd(e,phi);
if(track==1)
break;
else
e++;
//private key
double d1=1/e;
double d=fmod(d1,phi);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
cout<<"\n"<<"p = "<<p;
cout<<"\n"<<"q = "<<q;
cout<<"\n"<<"n = pq = "<<n;
cout<<"\n"<<"phi = "<<phi;
cout<<"\n"<<"e = "<<e;
cout<<"\n"<<"d = "<<d;
return 0;
Input: Message=10.
Output:
Algorithm:
Step-1: Start
Step-2: Select global public elements; a large prime number q and a primitive
root of q, α ; α <q
Select private X A , where, X A <q, and calculate public Y A , where , Y A=α XA mod q
K= ( Y B ) XA mod q
K= ( Y B ) XB mod q
Step-7: Stop.
Code:
#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
int main()
P = 23;
G = 9;
a = 4;
x = power(G, a, P);
b = 3;
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
return 0;
Output:
Experiment No.08
Name of the Experiment: Write a C/C++ program to implement Elgamal
Public-key algorithm.
Objective:
Algorithm:
Each user of ElGamal cryptosystem generates the key pair through as follows −
Step-3: Choosing the private key. The private key x is any number bigger than
1 and smaller than p−1.
Step-4: Computing part of the public key. The value y is computed from the
parameters p, g and the private key x as follows −
y=g x mod p
Step-5: Obtaining Public key. The ElGamal public key consists of the three
parameters (p, g, y).
ElGamal Encryption
C 1=g k mod p
Step-3: Send the ciphertext C, consisting of the two separate values (C1, C2),
sent together.
ElGamal Decryption
To decrypt the ciphertext (C1, C2) using private key x, the following two steps
are taken −
Code:
#include<bits/stdc++.h>
bool isPrimeNumber(int n) {
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
int res = 1;
x = x % p;
if (y & 1)
res = (res*x) % p;
y = y >> 1;
x = (x*x) % p;
return res;
s.insert(2);
n = n/2;
s.insert(i);
n = n/i;
}
}
if (n > 2)
s.insert(n);
int findPrimitiveRoot(int n) {
unordered_set<int> s;
if (isPrimeNumber(n)==false)
return -1;
GeneratePrimes(s, ETF);
flag = true;
break;
if (flag == false)
return r;
return -1;
a = fmod(a, m);
if (fmod((a * x) , m) == 1)
return x;
int main()
double K,alpha,zz,k,xa,ya,c1,c2,temp1,temp2,temp3,temp4,temp5,decrypt;
int q1=19;
xa=double(rand()%(q1-2)+1);
alpha=double(findPrimitiveRoot(q1));
double q=double(q1);
// xa=5;
zz=pow(alpha,xa);
ya=fmod(zz,q);
k=6;
temp1=pow(ya,k);
K=fmod(temp1,q);
double m=17;
cout<<"\nOriginal Message="<<m<<endl;
temp2=pow(alpha,k);
c1=fmod(temp2,q);
c2=fmod((K*m),q);
cout<<"\nEncrypted Message:(C1,C2)="<<c1<<","<<c2<<endl;
temp3=pow(c1,xa);
K=fmod(temp3,q);
cout<<"\nK="<<K;
temp4=modInverse(K,q);
temp5=c2*temp4;
decrypt=fmod(temp5,q);
cout<<"\nDecrypted Message="<<decrypt<<endl;
return 0;
}
Input:
17
Output:
Experiment No.09
Name of the Experiment: Write a C/C++ program to implement a Simple
Hash Function using simple XOR operation.
Objective:
Algorithm:
Step-1: Start.
Where,
C i=ith bit of the hash code ,1 ≤i ≤ n
⊕=XOR operation
Step-5: Return C.
Step-6: Stop.
Code:
#include<stdio.h>
#include<string.h>
int main()
char str[50];
int ascii[50],i,j=0,block[50][50],remainder[50][50],arr_size;
printf("Enter a string:");
gets(str);
int len=strlen(str);
for(i=0;i<len;i++)
j=0;
ascii[i]=(int)str[i];
while(ascii[i]!=0)
remainder[i][j]=ascii[i]%2;
// printf("%d",remainder[i][j]);
ascii[i]=ascii[i]/2;
j++;
}
printf("\n\n");
i=0;
for(j=0;j<8;j++)
block[i][j]=remainder[i][j];
for(i=0;i<len-1;i++)
for(j=0;j<8;j++)
if(block[i][j]!=remainder[i+1][j])
block[i+1][j]=1;
else block[i+1][j]=0;
// printf("%d",block[i+1][j]);
// printf("\n");
i=len-1;
for(j=7;j>=0;j--)
printf("%d",block[i][j]);
printf("\n");
return 0;
Input: word.
Output: