NetworkSecurity

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Name: Sachin Sudam Ugale

Class: TY Computer Science


Roll No.:2009858
Subject: Network Security
K.J.Somaiya College of Science and Commerce
Vidyavihar, Mumbai-400077
Autonomous- Affiliated to University of Mumbai

Department of Computer Science

CERTIFICATE
This is to certified that the experimental work as entered in this journal is as
per syllabus in B.Sc Computer Science for Network Security was done in
the Computer Science laboratory of K.J.Somaiya College of Science
andCommerce by the student
Bro/Sis: Sachin Sudam Ugale having Seat No.:2009858 of class T.Y.B.Sc
Computer Science Semester V during the academic year 2020-2021.
No.of Experiments completed 9 out of 9.

Course Coordinator of CS Department


Date:

Sign of Subject In-charge College seal Sign of Examiner


Date: Date:
INDEX

Sr. No. Title Page No.


1 Practical 1 : Caesar Cipher 4

2 Practical 2: Rail Fence Cipher 7

3 Practical 3: Vernam Cipher 9

4 Practical 4: Monoalphabetic Cipher 11

5 Practical 5: Deffie-Hellman Cipher 13

6 Practical 6: Simple Columnar Cipher 16

7 Practical 7: Digital Encryption Standard 19

8 Practical 8: RSA 22

9 Practical 9: Advanced Encryption Standard 24


Practical 1: Caesar Cipher
Algorithm:
Step1:- Take the plain text
Step2:- Take the integer value for the key.
Step3:- Change the alphabet into the number that goes with its order in the alphabet initial from
0, and label this numeral X.
(A=0,B=1,C=2,….......................................... ,Y=24,Z=25)
Step4:-If Key>0 then shift forward by key value.
Calculate Y=(X + Key) mod 26
Step5:- If Key<0 then shift forward by key value.
Calculate Y=(X + Key) mod 26

Code:
import java.io.*;
public class Caeser
{
static String encryption;
static String decryption;
static char ch;

public static String encrypt(String str)


{
encryption="";
System.out.print("\n\t\t*****Encryption****\n");
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='x' )
encryption+='a';
else if(str.charAt(i)=='y' )
encryption+='b';
else if(str.charAt(i)=='z' )
encryption+='c';
else
{
ch=str.charAt(i);
encryption+=(char)(ch+3);
}
}
return encryption;
}
public static String decrypt(String str)
{
decryption="";
System.out.print("\n\t\t*****Decryption****\n");
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='a' )
decryption+='x';
else if(str.charAt(i)=='b' )
decryption+='y';
else if(str.charAt(i)=='c' )
decryption+='z';

else
{
ch=str.charAt(i);
decryption+=(char)(ch-3);
}
}
return decryption;
}
public static void main(String args[])throws IOException
{

String en;

System.out.print("\n\t****The Caesar Cipher Substitution Algo***\n\n");


BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the string:");
String str=br.readLine();
en=encrypt(str);
System.out.print("Ciphertext is:"+en+"\n");
System.out.print("Decrypted plaintext is:"+ decrypt(en));
}
}

Output:
Practical 2: Rail Fence Cipher
Algorithm:
Step 1: Accept plain text
Step 2: Accept No of Row
Step3: Letters of the plaintext are written downwards diagonally after reaching the bottom most
row we go upwards in traverse direction diagonally.
Step 4: Step 3 is repeated until the last letter of the plain text.
Step 5:After Completion of step 4 all the individual row values are combined and gives the
cipher text.
Code: import java.io.*;
class Rail
{
Rail()
{
try{
String msg="";
String str1="";
String str2="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a msg");
msg=br.readLine();
for(int i=0;i<msg.length();i++)
{
if(i%2==0)
{
str1=str1+msg.charAt(i);
}
else if(i%2==1)
{
str2=str2+msg.charAt(i);
}
}
System.out.println("Encrypted Text: " +str1+str2);
}
catch(Exception e)
{
}
}
public static void main(String args[])
{
new Rail();
}
}

Output:
Practical 3: Vernam Cipher
Algorithm:
Step1: Accept the plain text and the key with the same size as the plain text.
Step2: Assign a number to each character of the plain-text and the key according to alphabetical
order.
Step3: Add both the number (Corresponding plain-text character number and Key character
number).
Step4: Subtract the number from 26 if the added number is greater than 26, if it isn’t then leave
it.

Code:
import java.io.*;

class Vernam{

Vernam(){
try {
String msg,key,Cipher="";
int sum=0,num1=0,num2=0;
char pt,k;

char[] alpha1=new char[26];


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

alpha1[i]=(char)('A'+i);
}

BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter the message");
msg=reader.readLine();
System.out.println("Enter the Key Same as the message size");
key=reader.readLine();
System.out.println("Cipher Text");

for(int i=0;i<msg.length();i++){
pt=msg.charAt(i);
k=key.charAt(i);
for(int j=0;j<alpha1.length;j++){
if(pt==alpha1[j]){
num1=j;
}
if(k==alpha1[j]){
num2=j;
}
sum=num1+num2;
if(sum>25){
sum=sum%26;
}
}
Cipher+=alpha1[sum];
// System.out.println(alpha1[sum]);
}
System.out.println(Cipher);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
new Vernam();
}

Output:
Practical 4: Monoaplhabetic Cipher
Algorithm:
Step 1: Accept Plain text
Step 2: Plain text alphabet is mapped with alphabet of ciphertext key. For example A is mapped
to G and B is mapped to Z.
Step 3: Values of plain text are replaced by their mapped cipher text values and we get encrypted
text.
Step 4: By doing opposite of Step 2 and 3 we get the decrypted text.

Code: import java.util.Scanner;

public class MonoalphabeticCipher


{
public static char p[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };

public static String doEncryption(String s)


{
char c[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (p[j] == s.charAt(i))
{
c[i] = ch[j];
break;
}
}
}
return (new String(c));
}

public static String doDecryption(String s)


{
char p1[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (ch[j] == s.charAt(i))
{
p1[i] = p[j];
break;
}
}
}
return (new String(p1));
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message: ");
String en = doEncryption(sc.next().toLowerCase());
System.out.println("Encrypted message: " + en);
System.out.println("Decrypted message: " + doDecryption(en));
sc.close();
}
}

Output:
Practical 5: Deffie-Hellman Key Exchange Algorithm
Algorithm:
Step 1: User A and User B have two large prime numbers public key P and G.
Step 2: User A uses Private Key X and User B uses Private Key Y.
Step 3: Using formula I=G^X mod P User A creates a new key I using his private key X.
Step 4: Using a similar formula User B creates new key J using his own private key Y i.e. the
G^X is replaced by G^Y.
Step 5: Users A and B exchange the new key they generated i.e. I and J.
Step 6: User A uses formula KX=J^X mod P and User B uses formula KY=I^Y mod P.
Step 7: If KX=KY user now have symmetric secret key to encrypt.

Code: import java.io.*;


import java.math.BigInteger;
public class DeffieHellman
{
public static void main(String[] args) {
try {
BigInteger n,g,x,y,A,B,k1,k2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// accept the values of n and g
System.out.println("\n Enter Two Large Prime Number:");
System.out.println("\n Enter the value of n:");
n=new BigInteger(br.readLine());
chkPrime(n);
System.out.println("Enter Value of g:");
g=new BigInteger(br.readLine());
chkPrime(g);

//Accept values for x and y


System.out.println("Enter value of x:");
x=new BigInteger(br.readLine());
System.out.println("Enter value of y:");
y=new BigInteger(br.readLine());

// Calculate A
A=g.modPow(x,n);
// Calculate B
B=g.modPow(y,n);
// Calculate key k1 and k2
k1=B.modPow(x,n);
k2=A.modPow(y,n);

if(k1.equals(k2))
{
System.out.println("Key 1 is:"+k1+"Key 2 is:" +k2);
System.out.println("Both keys are equal !!! Successfull");
}
}catch (Exception e) {
System.out.println(e);
}
}
public static void chkPrime(BigInteger n) {
try{
if(n.isProbablePrime(10))
{
System.out.println("Prime,Continue");
}
else
{
System.out.println("Not Prime");
System.out.println("Enter value:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BigInteger x=new BigInteger(br.readLine());
chkPrime(x);
}
}catch(Exception ae){
System.out.println(ae);
}
}
}
Output:
Practical 6: Simple Columnar Transposition
Algorithm:
Step 1:Write the plain text message row-by-row in grids of rectangle of a predefined size.
Step 2: Read the message column by column. However, it needs to be in the order of column
1,2,3,4,5,6 etc. It can be any random order such as 2, 6, 4, 3, 5, and 1.
Step 3: After this operation message obtained is a cipher text message.
Step 4: Simple columnar transposition technique simply arranges the plain text as a sequence of
rows of rectangle that is read in column randomly.
Step 5: Simple columnar transposition technique is also quite simple to break after trying some
permutation combinations of column orders to get original plain text. To make this encryption
more complex we can perform more than one round of transposition using the same technique.
Step 6: Let’s see an example. Suppose we have plain text
HELLO YOU ARE READING THIS ON MY BSC IT.
Step 7: If we perform Simple columnar transposition technique operation on this text in column
order of 2, 6, 4, 3, 5, and 1 then encrypted text will be
EUAHYYRGNILRISSLADIBOENOCHOETMT .

Code: import java.io.*;


class SCT
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your plain text");
String accept=br.readLine();
System.out.println("Enter the no of rows ");
int r=Integer.parseInt(br.readLine());
System.out.println("Enter the no of cols");
int c=Integer.parseInt(br.readLine());
int count=0;
char cont[][]=new char[r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(count>=accept.length())
{
cont[i][j]=' ';
count++;
}
else
{
cont[i][j]=accept.charAt(count);
count++;
}
}
}
System.out.println("\nEnter the order of cols you want to view them in");
int choice[]=new int[c];
for(int k=0;k<c;k++)
{
System.out.println("Choice "+k+"-> ");
choice[k]=Integer.parseInt(br.readLine());
}
System.out.println("\nCipher text in matrix is ->");
String cipher="";
for(int j=0;j<c;j++)
{
int k=choice[j];
for(int i=0;i<r;i++)
{
cipher+=cont[i][k];
}
}
cipher=cipher.trim();
System.out.println(cipher);
}
}
Output:
Practical 7: Digital Encryption Standard (DES)
Algorithm:
Step 1: In the first step, the 64 bit plain text block is handed over to an initial Permutation (IP)
function.
Step 2: The initial permutation performed on plain text.
Step 3: Next the initial permutation (IP) produces two halves of the permuted block; says Left
Plain Text (LPT) and Right Plain Text (RPT).
Step 4: Now each LPT and RPT to go through 16 rounds of encryption process.
Step 5: In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on the
combined block
Step 6: The result of this process produces 64 bit cipher text.

Code: import java.io.*;


import javax.crypto.*;
import java.util.Base64;

public class DES


{
Cipher ecipher,dcipher;
public static void main(String args[])
{
System.out.println("String Entered:");
System.out.println(args[0]);
try
{
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
DES encryter=new DES(key);
String encryted=encryter.encrypt1(args[0]);
String decryted=encryter.decrypt1(encryted);

System.out.println("Original String is :"+args[0]);


System.out.println("Encrypted String is :"+encryted);
System.out.println("Decrypted String is :"+decryted);

}
catch(Exception e)
{
}
}
public DES(SecretKey key)
{
try{
ecipher=Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE,key);
dcipher=Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE,key);
}
catch(Exception e)
{
}
}
public String encrypt1(String str)
{
try
{
Base64.Encoder encoder = Base64.getEncoder();
byte[] utf8=str.getBytes("UTF8");
byte[] enc=ecipher.doFinal(utf8);
return encoder.encodeToString(enc);
}
catch(Exception e)
{
}
return null;
}
public String decrypt1(String str)
{
try
{
Base64.Decoder decoder = Base64.getDecoder();
byte[]dec =decoder.decode(str);
byte[] utf8=dcipher.doFinal(dec);
return new String(utf8,"UTF8");
}
catch(Exception e)
{
}
return null;
}
}

Output:
Practical 8: RSA Encryption
Algorithm:
Step 1: Choose two different large random prime numbers p and q
Step 2:Calculate n=p*q. n is the modulus for the public key and the private keys
Step 3: Calculate the totient: Ø(n)=(p-1)(q-1).
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. e is released as the public key exponent
Step 5: Compute d to satisfy the congruence relation de Ξ 1 (mod Ø(n)) i.e.: de=1+x Ø(n) for
some integer x. (Simply to say : Calculate d=(1+x Ø(n))/e to be an integer).
d is kept as the private key exponent

Code: import java.math.*;


import java.security.*;

public class RSA


{
public static void main(String args[])
{
SecureRandom r;
BigInteger p,q,p1,q1,n,n1,e,d,msg,ct,pt;

int bitLength=512;
int certainty=100;
r=new SecureRandom();

//step 1: Generate prime p & q


p=new BigInteger(bitLength,certainty,r);
q=new BigInteger(bitLength,certainty,r);

//step 2: n=p*q
n=p.multiply(q);

System.out.println("Prime no P is:"+p.intValue());
System.out.println("Prime no Q is:"+q.intValue());
System.out.println("N = P*Q is:"+n.intValue());

//setp 3:Generating Public key (e)


p1=p.subtract(new BigInteger("1"));
q1=q.subtract(new BigInteger("1"));
n1=p1.multiply(q1);
e=new BigInteger("2");

while(n1.gcd(e).intValue()>1 || e.compareTo(p1)!=-1)
e=e.add(new BigInteger("1"));
System.out.println("Public key is("+n.intValue()+","+e.intValue()+")");

//step 4:D=E^-1 mod(P-1)(Q-1)


d=e.modInverse(n1);
System.out.println("Private key is("+n.intValue()+","+d.intValue()+")");

//step 5:Encryption CT=(PT)^e mod n


msg=new BigInteger("5");
ct=msg.modPow(e,n);
System.out.println("Encrypted Text is:" +ct.intValue());

//step 6:Decryption PT=(CT)^d mod n


pt=ct.modPow(d,n);
System.out.println("Decrypted Text is:" +pt.intValue());
}

Output:
Practical 9: Advanced Encryption Standard (AES)
Algorithm:
Step 1: Derive the set of round keys from the cipher key.
Step 2: Initialize the state array with the block data (plaintext).
Step 3:Add the initial round key to the starting state array.
Step 4: Perform nine rounds of state manipulation.
Step 5: Perform the tenth and final round of state manipulation.
Step 6: Copy the final state array out as the encrypted data (ciphertext).

Code: import java.io.*;


import javax.crypto.*;
import java.util.Base64;

public class AES


{
Cipher ecipher,dcipher;
public static void main(String args[])
{
System.out.println("String Entered:");
System.out.println(args[0]);
try
{
SecretKey key = KeyGenerator.getInstance("AES").generateKey();
AES encryter=new AES(key);
String encryted=encryter.encrypt1(args[0]);
String decryted=encryter.decrypt1(encryted);

System.out.println("Original String is :"+args[0]);


System.out.println("Encrypted String is :"+encryted);
System.out.println("Decrypted String is :"+decryted);

}
catch(Exception e)
{
}
}
public AES(SecretKey key)
{
try{
ecipher=Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE,key);
dcipher=Cipher.getInstance("AES");
dcipher.init(Cipher.DECRYPT_MODE,key);
}
catch(Exception e)
{
}
}
public String encrypt1(String str)
{
try
{
Base64.Encoder encoder = Base64.getEncoder();
byte[] utf8=str.getBytes("UTF8");
byte[] enc=ecipher.doFinal(utf8);
return encoder.encodeToString(enc);
}
catch(Exception e)
{
}
return null;
}
public String decrypt1(String str)
{
try
{
Base64.Decoder decoder = Base64.getDecoder();
byte[]dec =decoder.decode(str);
byte[] utf8=dcipher.doFinal(dec);
return new String(utf8,"UTF8");
}
catch(Exception e)
{
}
return null;
}

}
Output:

You might also like