Nothing Special   »   [go: up one dir, main page]

Smt. Radhikatai Pandav College of Engineering Nagpur

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

1

SMT. RADHIKATAI
PANDAV COLLEGE
OF ENGINEERING
NAGPUR
DEPARTMENT OF
COMPUTER ENGINEERING
SUBJECT: INFORMATION
ASSURANCE & NETWORK
SECURITY
SEMESTER :VII
YEAR: 2021-22

1
2
List of Practicals

1. 1.To study and analyse network security model..

2. Write a program for a plain text message "I AM


HACKER ''. Encrypt it with the help of 7 -bit ASCII code.

3. Write a program to perform encryption and decryption


using the Caesar Cipher.

4. Write a program to perform encryption and decryption


using the Rail Fence Cipher technique.

5. Write a program to perform encryption and decryption


using Columnar Transposition Cipher technique.

6. Write a program to perform encryption and decryption


using data Encryption Standard (DES) algorithm.

7. Write a program to implement the RSA algorithm.

8. Write a program to implement SHA-1 Algorithm.

9. Write a program to implement Message Digest -5.


10. Write a program to implement the Diffie-Hellman
Key Exchange algorithm.

2
3

Practical no.: 1
Aim: Write a program to perform encryption and decryption
using the Caesar Cipher.
ALGORITHMS:

1. In Ceaser Cipher each letter in the plaintext is replaced by a letter


some

fixed number of positions down the alphabet.

2. For example, with a left shift of 3, D would be replaced by A, E would

become B, and so on.

3. The encryption can also be represented using modular arithmetic by


first

transforming the letters into numbers, according to the scheme, A = 0,


B = 1,

Z = 25.

4. Encryption of a letter x by a shift n can be described mathematically


as,

En(x) = (x + n) mod 26

5. Decryption is performed similarly,

Dn (x)=(x - n) mod26

3
4
PROGRAM:

CaesarCipher.java

class caesarCipher {

public static String encode(String enc, int offset) {

offset = offset % 26 + 26;

StringBuilder encoded = new StringBuilder();

for (char i : enc.toCharArray()) {

if (Character.isLetter(i)) {

if (Character.isUpperCase(i)) {

encoded.append((char) ('A' + (i - 'A' + offset) % 26));

} else {

encoded.append((char) ('a' + (i - 'a' + offset) % 26));

} else {

encoded.append(i);

return encoded.toString();

4
5
}

public static String decode(String enc, int offset) {

return encode(enc, 26 - offset);

public static void main(String[] args) throws java.lang.Exception {

String msg = "Nagpur University";

System.out.println("Simulating Caesar Cipher\n------------------------");

System.out.println("Input : " + msg);

System.out.printf("Encrypted Message : ");

System.out.println(caesarCipher.encode(msg, 3));

System.out.printf("Decrypted Message : ");

System.out.println(caesarCipher.decode(caesarCipher.encode(msg, 3),
3));

OUTPUT:

Simulating Caesar Cipher

------------------------

Input : Nagpur University

5
6
Encrypted Message : Dqqd Xqlyhuvlwb

Decrypted Message : Nagpur University

RESULT:

Thus the program for caesar cipher encryption and decryption


algorithm has

been implemented and the output verified successfully.

Practical no: 4

6
7
Aim: Write a program to perform encryption and decryption using
the Rail Fence Cipher technique.

DESCRIPTION:

In the rail fence cipher, the plain text is written downwards and
diagonally on

successive "rails" of an imaginary fence, then moving up when we


reach the bottom rail.

When we reach the top rail, the message is written downwards again
until the whole plaintext

is written out. The message is then read off in rows.

EXAMPLE:

ALGORITHM:

STEP-1: Read the Plain text.

STEP-2: Arrange the plain text in row columnar matrix format.

STEP-3: Now read the keyword depending on the number of columns


of the plain text.

STEP-4: Arrange the characters of the keyword in sorted order and the
corresponding

columns of the plain text.

7
8
STEP-5: Read the characters row wise or column wise in the former
order to get the

cipher text.

PROGRAM: (Rail Fence)

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int i,j,k,l;

char a[20],c[20],d[20];

clrscr();

printf("\n\t\t RAIL FENCE TECHNIQUE");

printf("\n\nEnter the input string : ");

gets(a);

l=strlen(a);

/*Ciphering*/

for(i=0,j=0;i<l;i++)

8
9
if(i%2==0)

c[j++]=a[i];

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

if(i%2==1)

c[j++]=a[i];

c[j]='\0';

printf("\nCipher text after applying rail fence :");

printf("\n%s",c);

/*Deciphering*/

if(l%2==0)

k=l/2;

else

k=(l/2)+1;

for(i=0,j=0;i<k;i++)

9
10
d[j]=c[i];

j=j+2;

for(i=k,j=1;i<l;i++)

d[j]=c[i];

j=j+2;

d[l]='\0';

printf("\nText after decryption : ");

printf("%s",d);

getch();

Output:

10
11

RESULT:

Thus the rail fence algorithm had been executed successfully.

Practical no: 5

11
12
Aim: Write a program to perform encryption and decryption using Columnar Transposition
Cipher technique.

LGORITHM:

1. Consider the plain text hello world, and let us apply the simple columnar

transposition technique as shown below

hell

owor

ld

2. The plain text characters are placed horizontally and the cipher text is

created with vertical format as: holewdlo lr.

3. Now, the receiver has to use the same table to decrypt the cipher text to

plain text.

PROGRAM:

TransCipher.java

import java.util.*;

class TransCipher {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the plain text");

String pl = sc.nextLine();

sc.close();

String s = "";

12
13
int start = 0;

for (int i = 0; i < pl.length(); i++) {

if (pl.charAt(i) == ' ') {

s = s + pl.substring(start, i);

start = i + 1;

s = s + pl.substring(start);

System.out.print(s);

System.out.println();

// end of space deletion

int k = s.length();

int l = 0;

int col = 4;

int row = s.length() / col;

char ch[][] = new char[row][col];

for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

if (l < k) {

ch[i][j] = s.charAt(l);

l++;

} else {

13
14
ch[i][j] = '#';

// arranged in matrix

char trans[][] = new char[col][row];

for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++) {

trans[j][i] = ch[i][j];

for (int i = 0; i < col; i++) {

for (int j = 0; j < row; j++) {

System.out.print(trans[i][j]);

// display

System.out.println();

OUTPUT:

Enter the plain text

14
15
Security Lab

SecurityLab

Sreictuy

RESULT:

Thus the java program for Row and Column Transposition Technique has

been implemented and the output verified successfully.

Practical No. 06

Aim :Write a program to perform encryption and decryption using data Encryption Standard
(DES) algorithm.

DESCRIPTION:

DES is a symmetric encryption system that uses 64-bit blocks, 8 bits of which are

used for parity checks. The key therefore has a "useful" length of 56 bits, which means that

only 56 bits are actually used in the algorithm. The algorithm involves carrying out

combinations, substitutions and permutations between the text to be encrypted and the key,

while making sure the operations can be performed in both directions. The key is ciphered on

64 bits and made of 16 blocks of 4 bits, generally denoted k1 to k16. Given that "only" 56 bits

are actually used for encrypting, there can be 256 different keys.

The main parts of the algorithm are as follows:

Fractioning of the text into 64-bit blocks

Initial permutation of blocks

Breakdown of the blocks into two parts: left and right, named L and R

Permutation and substitution steps repeated 16 times

15
16
Re-joining of the left and right parts then inverse initial permutation

ALGORITHM:

STEP-1: Read the 64-bit plain text.

STEP-2: Split it into two 32-bit blocks and store it in two different arrays.

STEP-3: Perform XOR operation between these two arrays.

STEP-4: The output obtained is stored as the second 32-bit sequence and the original

second 32-bit sequence forms the first part.

STEP-5: Thus the encrypted 64-bit cipher text is obtained in this way. Repeat the same

process for the remaining plain text characters.

PROGRAM:

DES.java

import javax.swing.*;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.util.Random ;

class DES {

byte[] skey = new byte[1000];

String skeyString;

static byte[] raw;

16
17
String inputMessage,encryptedData,decryptedMessage;

public DES()

try

generateSymmetricKey();

inputMessage=JOptionPane.showInputDialog(null,"Enter

message to encrypt");

byte[] ibyte = inputMessage.getBytes();

byte[] ebyte=encrypt(raw, ibyte);

String encryptedData = new String(ebyte);

System.out.println("Encrypted message "+encryptedData);

JOptionPane.showMessageDialog(null,"Encrypted Data

"+"\n"+encryptedData);

byte[] dbyte= decrypt(raw,ebyte);

String decryptedMessage = new String(dbyte);

System.out.println("Decrypted message

"+decryptedMessage);

JOptionPane.showMessageDialog(null,"Decrypted Data

"+"\n"+decryptedMessage);

catch(Exception e)

17
18
{

System.out.println(e);

void generateSymmetricKey() {

try {

Random r = new Random();

int num = r.nextInt(10000);

String knum = String.valueOf(num);

byte[] knumb = knum.getBytes();

skey=getRawKey(knumb);

skeyString = new String(skey);

System.out.println("DES Symmetric key = "+skeyString);

catch(Exception e)

System.out.println(e);

private static byte[] getRawKey(byte[] seed) throws Exception

KeyGenerator kgen = KeyGenerator.getInstance("DES");

18
19
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

sr.setSeed(seed);

kgen.init(56, sr);

SecretKey skey = kgen.generateKey();

raw = skey.getEncoded();

return raw;

private static byte[] encrypt(byte[] raw, byte[] clear) throws

Exception {

SecretKeySpec skeySpec = new SecretKeySpec(raw,

"DES");

Cipher cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

byte[] encrypted = cipher.doFinal(clear);

return encrypted;

private static byte[] decrypt(byte[] raw, byte[] encrypted)

throws Exception

SecretKeySpec skeySpec = new SecretKeySpec(raw,

"DES");

Cipher cipher = Cipher.getInstance("DES");

19
20
cipher.init(Cipher.DECRYPT_MODE, skeySpec);

byte[] decrypted = cipher.doFinal(encrypted);

return decrypted;

public static void main(String args[]) {

DES des = new DES();

OUTPUT:

20
21

RESULT:

Thus the data encryption standard algorithm had been implemented successfully

Practical No.07

Aim: Write a program to implement the RSA algorithm.


DESCRIPTION:

RSA is an algorithm used by modern computers to encrypt and decrypt messages. It

is an asymmetric cryptographic algorithm. Asymmetric means that there are two different

keys. This is also called public key cryptography, because one of them can be given to

everyone. A basic principle behind RSA is the observation that it is practical to find three

very large positive integers e, d and n such that with modular exponentiation for all

integer m:

(me

= m (mod n)

The public key is represented by the integers n and e; and, the private key, by the

21
22
integer d. m represents the message. RSA involves a public key and a private key. The public

key can be known by everyone and is used for encrypting messages. The intention is that

messages encrypted with the public key can only be decrypted in a reasonable amount of

time using the private key.

ALGORITHM:

STEP-1: Select two co-prime numbers as p and q.

STEP-2: Compute n as the product of p and q.

STEP-3: Compute (p-1)*(q-1) and store it in z.

STEP-4: Select a random prime number e that is less than that of z.

STEP-5: Compute the private key, d as e * mod-1(z).

STEP-6: The cipher text is computed as messagee

* mod n.

STEP-7: Decryption is done as cipherdmod n.

PROGRAM: (RSA)

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<math.h>

#include<string.h>

long int

p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;

char msg[100];

22
23
int prime(long int);

void ce();

long int cd(long int);

void encrypt();

void decrypt();

void main()

clrscr();

printf("\nENTER FIRST PRIME NUMBER\n");

scanf("%d",&p);

flag=prime(p);

if(flag==0)

printf("\nWRONG INPUT\n");

getch();

printf("\nENTER ANOTHER PRIME NUMBER\n");

scanf("%d",&q);

flag=prime(q);

if(flag==0||p==q)

printf("\nWRONG INPUT\n");

23
24
getch();

printf("\nENTER MESSAGE\n");

fflush(stdin);

scanf("%s",msg);

for(i=0;msg[i]!=NULL;i++)

m[i]=msg[i];

n=p*q;

t=(p-1)*(q-1);

ce();

printf("\nPOSSIBLE VALUES OF e AND d ARE\n");

for(i=0;i<j-1;i++)

printf("\n%ld\t%ld",e[i],d[i]);

encrypt();

decrypt();

getch();

int prime(long int pr)

int i;

j=sqrt(pr);

for(i=2;i<=j;i++)

24
25
{

if(pr%i==0)

return 0;

return 1;

void ce()

int k;

k=0;

for(i=2;i<t;i++)

if(t%i==0)

continue;

flag=prime(i);

if(flag==1&&i!=p&&i!=q)

e[k]=i;

flag=cd(e[k]);

if(flag>0)

d[k]=flag;

25
26
k++;

if(k==99)

break;

}}}

long int cd(long int x)

long int k=1;

while(1)

k=k+t;

if(k%x==0)

return(k/x);

}}

void encrypt() {

long int pt,ct,key=e[0],k,len;

i=0;

len=strlen(msg);

while(i!=len) {

pt=m[i];

pt=pt-96;

k=1;

26
27
for(j=0;j<key;j++)

{ k=k*pt;

k=k%n;

temp[i]=k;

ct=k+96;

en[i]=ct;

i++;

en[i]=-1;

printf("\nTHE ENCRYPTED MESSAGE IS\n");

for(i=0;en[i]!=-1;i++)

printf("%c",en[i]);

void decrypt()

long int pt,ct,key=d[0],k;

i=0;

while(en[i]!=-1)

ct=temp[i];

k=1;

27
28
for(j=0;j<key;j++)

k=k*ct;

k=k%n;

pt=k+96;

m[i]=pt;

i++;

m[i]=-1;

printf("\nTHE DECRYPTED MESSAGE IS\n");

for(i=0;m[i]!=-1;i++)

printf("%c",m[i]);

RESULT:

Thus the C program to implement RSA encryption technique had been implemented
successfully.

28
29

Practical No. 8

Aim: Write a program to implement SHA-1 Algorithm

DESCRIPTION:

In cryptography, SHA-1 (Secure Hash Algorithm 1) is a


cryptographic hash function.

SHA-1 produces a 160-bit hash value known as a message digest.


The way

this algorithm works is that for a message of size < 264 bits it
computes a 160-bit condensed

output called a message digest. The SHA-1 algorithm is designed


so that it is practically

infeasible to find two input messages that hash to the same


output message. A hash function

29
30
such as SHA-1 is used to calculate an alphanumeric string that
serves as the cryptographic

representation of a file or a piece of data. This is called a digest


and can serve as a digital

signature. It is supposed to be unique and non-reversible.

EXAMPLE:
ALGORITHM:

STEP-1: Read the 256-bit key values.

STEP-2: Divide into five equal-sized blocks named A, B, C, D and E.

STEP-3: The blocks B, C and D are passed to the function F.

STEP-4: The resultant value is permuted with block E.

STEP-5: The block A is shifted right by ‘s’ times and permuted with the result of step-4.

STEP-6: Then it is permuted with a weight value and then with some other key pair and

taken as the first block.

STEP-7: Block A is taken as the second block and the block B is shifted by ‘s’ times and

taken as the third block.

STEP-8: The blocks C and D are taken as the block D and E for the final output.

PROGRAM: (Secure Hash Algorithm)

import java.security.*;

public class SHA1 {

public static void main(String[] a) {

30
31
try {

MessageDigest md = MessageDigest.getInstance("SHA1");

System.out.println("Message digest object info: ");

System.out.println(" Algorithm = " +md.getAlgorithm());

System.out.println(" Provider = " +md.getProvider());

System.out.println(" ToString = " +md.toString());

String input = "";

md.update(input.getBytes());

byte[] output = md.digest();

System.out.println();

System.out.println("SHA1(\""+input+"\") =

+bytesToHex(output));

input = "abc";

md.update(input.getBytes());

output = md.digest();

System.out.println();

System.out.println("SHA1(\""+input+"\") = "

+bytesToHex(output));

input = "abcdefghijklmnopqrstuvwxyz";

md.update(input.getBytes());

output = md.digest();

System.out.println();

31
32
System.out.println("SHA1(\"" +input+"\") = "

+bytesToHex(output));

System.out.println(""); }

catch (Exception e) {

System.out.println("Exception: " +e);

public static String bytesToHex(byte[] b)

char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6',

'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

StringBuffer buf = new StringBuffer();

for (int j=0; j<b.length; j++) {

buf.append(hexDigit[(b[j] >> 4) & 0x0f]);

buf.append(hexDigit[b[j] & 0x0f]); }

return buf.toString(); }

OUTPUT:

RESULT:

Thus the SHA-1 hashing technique had been implemented successfully.

Practical No. 9

32
33
Aim: Write a program to implement Message Digest -5.

DESCRIPTION:

EXAMPLE:

ALGORITHM:

STEP-1: Read the 128-bit plain text.

STEP-2: Divide into four blocks of 32

CS6711 SECURITY LABORATORY

IMPLEMENTATION OF MD5

To write a C program to implement the MD5 hashing technique.

MD5 processes a variable-length message into a fixed-length output of 128 bits. The

p into chunks of 512-bit blocks. The message is padded

2. The padding works as follows: first a single bit, 1, is appended to

the end of the message. This is followed by as many zeros as are required to bring the length

of the message up to 64 bits less than a multiple of 512. The remaining bits are filled up

64 bits representing the length of the original message, modulo 264.The main MD5 algorithm

bit state, divided into four 32-bit words, denoted A, B, C, and

initialized to certain fixed constants. The main algorithm then uses each 512

block in turn to modify the state.

bit plain text.

33
34
Divide into four blocks of 32-bits named as A, B, C and D.

length output of 128 bits. The

padded s

34

You might also like