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

NSM Lab Final

The document describes experiments to be performed in a network security lab. It lists 6 experiments including implementing encryption algorithms like DES, Diffie-Hellman, and RSA. It also includes experiments on database security and server security.

Uploaded by

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

NSM Lab Final

The document describes experiments to be performed in a network security lab. It lists 6 experiments including implementing encryption algorithms like DES, Diffie-Hellman, and RSA. It also includes experiments on database security and server security.

Uploaded by

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

KG COLLEGE OF ARTS AND SCIENCE

(AFFILIATED TO BHARATHIAR UNIVERSITY & ACCREDITED BY NAAC)


KGISL CAMPUS, SARAVANAMPATTI, COIMBATORE-641035.

B.SC. COMPUTER TECHNOLOGY


SKILL BASED LAB-4
NETWORK SECURITY LAB
(III YEAR – VI SEMESTER)
List of Experiments

Page
S.No. DATE Name of the Experiment Signature
No.

Write a program to encrypt the data using


1.a
the Substitution Cipher encryption methods

Write a program to encrypt the data using


1.b. the Transposition Cipher encryption
methods

Write a program to implement DES


2
algorithm.

Write a program to implement the Public Key


3 Cryptography using Diffie –Hellman
Algorithm.

Write a program to implement the Public


4 Key Cryptography using RSA algorithm.

Write a program to secure the Database using


5 User Authentication Security

Write a server security program for


6 Dynamic Page Generation.
Ex.No.
SUBSTITUTION CIPHER
:1(a)

AIM:
To implement a program for encrypting a plain text and decrypting a cipher text using
substitution cipher technique.
ALGORITHM:
Step 1: Start the Process
Step 2: Declare the header files and variables
Step 3: Read the input plain text to be encrypted and also the Caeser cipher key an integer
between 0 and 25.
Step 4: Encrypt the plain text using the Caeser cipher key and the ALPHABET string.
Step 5: For every character in the plain text
Step 6: Search the ALPHABET string for the character and assign the numeric representation
of the character (plain numeric) as the index position of the character in the ALPHABET
string.
Step 7: Perform encryption using cipher numeric = (plainnumeric + Caeser cipher key) mod
26.
Step 8: Use cipher numeric as the index position and get the corresponding character from the
ALPHABET string as the equivalent cipher text character for the plain text character.
Step 9: Print the equivalent cipher text
Step 10: Decrypt the cipher text using the Caeser cipher key and the ALPHABET string.
Step 11: For every character in the cipher text
Step 12: Search the ALPHABET string for the character and assign the numeric
representation of the character (ciphernumeric) as the index position of the character in the
ALPHABET string.

Step 13: Perform decryption using


Plainnumeric = ( ciphernumeric - Caeser cipher key ) mod 26,
if plainnumeric < 0 , plainnumeric = plainnumeric + 26
Step 14: Use plainnumeric as the index position and get the corresponding character from the
ALPHABET string as the equivalent plain text character for the cipher text character.
Step 15: Print the equivalent plain text

PROGRAM:

#include<stdio.h>
int main()
{
char *message,*emessage,*dmessage;
int i,j=0,k,key,temp;
clrscr();
printf("\nEnter the key\n");
scanf("%d",&key);
key=key%26;
printf("\nEnter message\n");
fflush(stdin);
gets(message);
for (i=0;message[i]!=NULL;i++)
message[i]=tolower(message[i]);
for (i=0;message[i]!=NULL;i++) {
if(message[i]==' ')
emessage[j++]=message[i]; else {
if(message[i]>=48 && message[i]<=57) {
temp=message[i]+key;
if(temp>57)
emessage[j++]=48+(temp-58); else
emessage[j++]=temp;
} else {
if(message[i]>=97 && message[i]<=123) {
temp=message[i]+key;
if(temp>122)
emessage[j++]=97+(temp-123); else
emessage[j++]=temp;
} else
emessage[j++]=message[i];
}
// printf("%c ",emessage[j]);
}
}
emessage[j]='\0';
printf("\n\n\nEncrypted message is\n\n");
for (i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
// printf("\n end");
for (i=0,j=0;emessage[i]!=NULL;i++) {
if(emessage[i]==' ')
dmessage[j++]=emessage[i]; else {
if(emessage[i]>=48 && emessage[i]<=57) {
temp=emessage[i]-key;
if(temp<48)
dmessage[j++]=58-(48-temp); else
dmessage[j++]=temp;
} else {
if(emessage[i]>=97 && emessage[i]<=123) {
temp=emessage[i]-key;
if(temp<97)
dmessage[j++]=123-(97-temp); else
dmessage[j++]=temp;
} else
dmessage[j++]=emessage[i];
}
}
}
dmessage[j]='\0';
printf("\n\n\nRetrieved message is\n\n");
for (i=0;dmessage[i]!=NULL;i++)
printf("%c",dmessage[i]);
getch();
return(0);
}

OUTPUT:

RESULT:
Thus the program to implement substitution encryption technique was developed and
executed.

Ex.No.
TRANSPOSITION CIPHER
:1(a)

AIM:
To implement a program for encrypting a plain text and decrypting a cipher text using
substitution cipher technique.
ALGORITHM:

Step 1: Start the Process


Step 2: Declare the header files and variables
Step 3: Read the key for substitution
Step 4: Get the Message for Cipher
Step 5: Generate the (5 x 5) key table or matrix using the key.

Step 6: Fill the spaces in the table with the letters of the key without any duplication of
letters.

Step 8: Remove any punctuation or characters from the plain text that are not present in the
key square.

Step 9: Identify any double letters in the plaintext and insert ‘X’ between the two
occurrences.
Step 10: Split the plain text into groups of 3 letters

Step 11: Encryption: is a ‘simple columnar transposition’ cipher where the plaintext is
written horizontally with a certain alphabet width.

Step 12: The cipher text is obtained by reading column vertically downward from first to last
column.
Step 13: Decryption: the receiver prepares similar table.

Step 14: The number of columns is equal to key number.

Step 15: The number of rows is obtained by dividing number of total cipher text alphabets by
key value and rounding of the quotient to next integer value.
Step 16: The receiver then writes the received cipher text vertically down and from left to
right column.

Step 17: To obtain the text, he reads horizontally left to right and from top to bottom row.

Step 18: Stop the process

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void cipher(int i,int c);


int findMin();
void makeArray(int,int);
char arr[22][22],darr[22][22],emessage[111],retmessage[111],key[55];
char temp[55],temp2[55];
int k=0;

int main()
{
char*message,*dmessage;
int i,j,klen,emlen,flag=0;
int r,c,index,min,rows;
clrscr();
printf("\t\t\t TRANSPOSITION CIPHER \n");
printf("Enter the key\n");
fflush(stdin);
gets(key);
printf("\nEnter message to be ciphered\n");
fflush(stdin);
gets(message);
strcpy(temp,key);
klen=strlen(key);
k=0;
for(i=0;;i++)
{
if(flag==1)
break;
for(j=0;key[j]!=NULL;j++)
{
if(message[k]==NULL)
{
flag=1;
arr[i][j]='-';
}
else
{
arr[i][j]=message[k++];
}
}
}
r=i;
c=j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%c",arr[i][j]);
}
printf("\n");
}
k=0;
for(i=0;i<klen;i++)
{
index=findMin();
cipher(index,r);
}
emessage[k]='\0';
printf("\nEncrypted message is\n");
for(i=0;emessage[i]!=NULL;i++)
printf("%c",emessage[i]);
printf("\n\n");
emlen=strlen(emessage);
strcpy(temp,key);
rows=emlen/klen;
rows;
j=0;
for(i=0,k=1;emessage[i]!=NULL;i++,k++)
{
temp2[j++]=emessage[i];
if((k%rows)==0)
{
temp2[j]='\0';
index=findMin();
makeArray(index,rows);
j=0;
}
}
printf("\nArray Retrieved is\n");
k=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%c",darr[i][j]);
retmessage[k++]=darr[i][j];
}
printf("\n");
}
retmessage[k]='\0';
printf("\nMessage retrived is\n");
for(i=0;retmessage[i]!=NULL;i++)
printf("%c",retmessage[i]);
getch();
return(0);
}

void cipher(int i,int r)


{
int j;
for(j=0;j<r;j++)
{
{
emessage[k++]=arr[j][i];
}
}
}

void makeArray(int col,int row)


{
int i,j;
for(i=0;i<row;i++)
{
darr[i][col]=temp2[i];
}
}

int findMin()
{
int i,j,min,index;
min=temp[0];
index=0;
for(j=0;temp[j]!=NULL;j++)
{
if(temp[j]<min)
{
min=temp[j];
index=j;
}
}
temp[index]=123;
return(index);
}
OUTPUT:

RESULT:

Thus the program to implement transposition encryption technique was developed and
executed.
Ex.No. : 2 DATA ENCRYPTION STANDARD (DES)

AIM:
To develop a program to implement Data Encryption Standard for encryption and
decryption.

ALGORITHM:

Step 1: Start the Process


Step 2: Declare the header files and variables
Step 3: Read the Plain text and encrption key from the user.
Step 4: The Data Encrpytion process begins with the 64-bit plain text block getting handed
over to an initial permutation (IP) function.
Step 5: The initial permutation (IP) is then performed on the plain text.
Step 6: Next, the initial permutation (IP) creates two halves of the permuted block, referred to
as Left Plain Text (LPT) and Right Plain Text (RPT).
Step 7: Each LPT and RPT goes through 16 rounds of the encryption process.
Step 8: Finally, the LPT and RPT are rejoined, and a Final Permutation (FP) is performed on
the newly combined block.
Step 9: The result of this process produces the desired 64-bit ciphertext.
Step 10: The Data Decrpytion process begins with the 64-bit cipher text block getting handed
over to an initial permutation (IP) function.
Step 11: The initial permutation (IP) is then performed on the Cipher text.
Step 12: The result of this process produces the original plain text.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
int i,ch,lp;
char cipher[50],plain[50];
char key[50];
clrscr();
while(1)
{
printf(" \n-----MENU ---- ");
printf("\n1:Data Encryption\t2:DataDecryption\t3:Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nData Encryption");
printf("\nEnter the plain text:");
fflush(stdin);
gets(plain);
printf("\nEnter the encryption key:");
gets(key);
lp=strlen(key);
for(i=0;plain[i]!='\0';i++)
cipher[i]=plain[i]^lp;
cipher[i]='\0';
printf("\nThe encrypted textis:");
puts(cipher);
break;
case 2:
printf("\nData decryption");
for(i=0;cipher[i]!='\0';i++)
plain[i]=cipher[i]^lp;
printf("\nDecrypted text is:");
puts(plain);
break;
case 3:
exit(0);
}
getch();
}
}

OUTPUT:

RESULT:

Thus the program to implement transposition encryption technique was developed and
executed.
Ex.No. : 3 Public Key Cryptography using Diffie –Hellman Algorithm

AIM:
To Develop a program to implement Diffie Hellman Key Exchange Algorithm for
encryption and Decryption.
ALGORITHM:

Step 1: Start the Process


Step 2: Import the package,Declare the class and variables
Step 3: Person A and Person B shares the same public keys g and p.
Step 4: Person A selects a random public key p1.
Step 5: Person A computes his secret key a as ga mod p.
Step 6: Then Person A sends a to Person B.
Step 7: Similarly Person B also selects a public key p2 and computes his secret key as b
and sends the same back to Person A.
Step 8: Now both of them compute their common secret key as the other one’s secret key
power of a mod p.
Step 9: Stop the process
PROGRAM:

import java.util.*;
import java.math.BigInteger;

public class dh1


{
final static BigInteger one=new BigInteger("1");
public static void main(String args[])
{
Scanner stdin = new Scaner(System.in);
BigInteger p;
System.out.println("Enter the approximate value of p you want.");
String ans = stdin.next();
p = getNextPrime(ans);
System.out.println("Your prime is "+p+".");
System.out.println("Now, enter a number in betwen 2 and p-1");
BigInteger g = new BigInteger(stdin.next());
System.out.println("Person A: enter your secret number now.");
BigInteger a = new BigInteger(stdin.next());
BigInteger resulta = g.modPow(a,p);
System.out.println("Person A sends to person B "+resulta+".");
System.out.println("Person B: enter your secret number now.");
BigInteger b = new BigInteger(stdin.next());
BigInteger resultb = g.modPow(b,p);
System.out.println("Person B sends to person A "+resultb+".");
BigInteger KeyACalculates = resultb.modPow(a,p);
BigInteger KeyBCalculates = resultb.modPow(b,p);
System.out.println("A takes "+resultb+" raises it to the power "+a+" mod"+p);
System.out.println("The key A calculates is "+keyAcalculates+".");
System.out.println("B takes "+resulta+" raises it to the power "+b+" mod"+p);
System.out.println("The key B calculates is "+keyBCalculates+".");
}

public static BigInteger getNextPrime(String ans)


{
BigInteger test = new BigInteger(ans);
while(!test.isProbablePrime(99))
test = test.add(one);
return test;
}
}

OUTPUT:

RESULT:
Thus the program to implement Diffie-Hellman Key Exchange algorithm was
developed and executed successfully

Ex.No. :4 Public Key Cryptography using RSA algorithm

AIM:
To develop a program to implement RSA algorithm for encryption and decryption.

ALGORITHM:

Step 1: Start the Process


Step 2: Declare the header files and variables
Step 3: Read the encrption key and Plain text from the user.
Step 4: Key Generation, Choose two distinct prime numbers p and q.
Step 5: Find n such that n = pq, n will be used as the modulus for both the public and private
keys.
Step 6: Find the totient of n, ϕ(n) ϕ(n)=(p-1)(q-1)
Step 7: Choose an e such that 1 < e < ϕ(n), and such that e and ϕ(n) share no divisors other
than 1 (e and ϕ(n) are relatively prime). e is kept as the public key exponent
Step 8: Determine d (using modular arithmetic) which satisfies the congruence relation
de ≡ 1 (mod ϕ(n)).
Step 9: The public key has modulus n and the public (or encryption) exponent e. The private
key has modulus n and the private (or decryption) exponent d, which is kept secret.
Step 10: Perform the Encryption
c ≡ me (mod n).
Step 11: Perform the Decryption:
m ≡ cd (mod n).
Step12: Stop the process.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>

void main()
{
int a,b,i,t,j,x,n,k=0,flag=0,prime[100];
char m[20],pp[20];
float p[20],c[20];
double e,d;
clrscr();
for(i=0;i<50;i++)
{
flag=0;
for(j=2;j<i/2;j++)
if(i%j==0);
{
flag=0;
break;
}
if(flag==0)
prime[k++]=i;
}
a=prime[k-1];
b=prime[k-2];
n=a*b;
t=(a-1)*(b-1);
e=(double)prime[2];
d=1/(float)e;
printf("\nKey of encryption is%if",d);
printf("\nEnter the text:");
scanf("%s",&m);
printf("\n\tSource\t\t\t\tDestination");
printf("\nChar Numeric\tCipher\tNumeric\tChar\n");
for(i=0;i<x;i++)
{
printf("%c",m[i]);
printf("\t%d",m[i]-97);
c[i]=pow(m[i]-97,e);
c[i]=fmod(c[i],n);
printf("\t%f",c[i]);
p[i]=pow(c[i],d);
p[i]=fmod(p[i],n);
printf("\t %f",p[i]);
pp[i]=p[i]+97;
printf("\t%c\n",pp[i]);
}
getch();
}
OUTPUT:

RESULT:
Thus the program for implementation of RSA algorithm was executed and verified
successfully.
Ex.No. :5 Secure the Database using User Authentication Security

AIM:
To secure database using User Authentication Security

ALGORITHM:

Step 1: Start the process

Step 2: Import the package awt.event.*;

Step 3: Declare the class as Log extends JFrame

Step 4: Create a frame and set Size and Location.

Step 5: Create a JPanel as panel, JButton as blogin, JTextField as txuser and JPasswordField
as pass.

Step 6: Add the Button, TextField to panel and Panel to the frame using
getContentPane().add(panel).

Step 7: In ActionLogin() method, Get the text from user and check the given text with
username of “text” and password of “12345” using if condition.

Step 8: If the username and password is correct the new frame will be display.

Step 9: Otherwise, the message dialog box will show “Wrong Password / Username”.

Step 10: Stop the process.

PROGRAM:
mport java.awt.event.*;
import javax.swing.*;
public class Log extends JFrame
{
public static void main(String[] args)
{
Log frameTabel = new Log();
}

JButton blogin = new JButton("Login");


JPanel pane = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);

Log()
{
super("Login Autentification");
setSize(300,200);
setLocation(500,280);
pane.setLayout (null);
txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
blogin.setBounds(110,100,80,20);
pane.add(blogin);
pane.add(txuser);
pane.add(pass);
getContentPane().add(pane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}

public void actionlogin()


{
blogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("test") && ppaswd.equals("12345")) {
Newframe regFace =new Newframe();
regFace.setVisible(true);
dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
}
OUTPUT:

USER AUTHENTICATION SECURITY


RESULT:
Thus the program for implementation of Server Security for Dynamic Page Generation was
executed and verified successfully.

Ex.No. : 6 Server security Program for Dynamic Page Generation.

AIM:
To implement server security program for dynamic Page Generation.
ALGORITHM:

Step 1: Start the process

Step 2: Import the swing package javax.swing and AWT package awt.event.*;

Step 3: Create a class as newframe extends JFrame

Step 4: Create a new frame from JFrame()

Step 5: Create a JPanel as panel and JLabel as Welcome

Step5: In newfrane() method, set the size of the from and location.

setSize(300,200);
setLocation(500,280);

Step 6: Set the Bounding size of Label welcome.setBounds(70,50,150,60); and add to panel.

Step 7: Add the panel to the frame using getContentPane ().add (panel); and set the deafault
close operation for frame
Step 8: Enable the visibility of frame using setVisible(True).

Step 8: Stop the process.

PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class newframe extends JFrame


{
public static void main(String[] args)
{
newframe frameTabel = new newframe();
}
JLabel welcome = new JLabel("Welcome to a New Frame");
JPanel panel = new JPanel();

newframe()
{
super("Welcome");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null);
welcome.setBounds(70,50,150,60);
panel.add(welcome);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

OUTPUT:
RESULT:
Thus the program for implementation of Server Security for Dynamic Page Generation was
executed and verified successfully.

You might also like