Top Banner
AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15 Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 1 Roll No of Student: 31 Date: __________ IMPLEMENTING DIFFIE-HELMAN The Diffie-Hellman protocol is a method for two computer users to generate a shared private key with which they can then exchange information across an insecure channel. Let the users be named Alice and Bob. First, they agree on two prime numbers and , where is large (typically at least 512 bits) and is a primitive root modulo . (In practice, it is a good idea to choose such that is also prime.) The numbers and need not be kept secret from other users. Now Alice chooses a large random number as her private key and Bob similarly chooses a large number . Alice then computes , which she sends to Bob, and Bob computes , which he sends to Alice. Now both Alice and Bob compute their shared key , which Alice computes as and Bob computes as Alice and Bob can now use their shared key to exchange information without worrying about other users obtaining this information. In order for a potential eavesdropper (Eve) to do so, she would first need to obtain knowing only , , and . This can be done by computing from and from . This is the discrete logarithmproblem, which is computationally infeasible for large . Computing the discrete logarithm of a number modulo takes roughly the same amount of time as factoring the product of two primes the same size as , which is what the security of the RSA cryptosystem relies on. Thus, the Diffie-Hellman protocol is roughly as secure as RSA The following java program shows the implementation of Diffie-Helman algorithm import java.util.*; import java.io.*; public class Diffe { public static void main(String[] ar) { double a,b,k1,k2,x,y,n,g; System.out.println("Enter Two Prime No:"); Scanner input = new Scanner(System.in); System.out.print("N="); n=input.nextDouble(); System.out.print("G="); g=input.nextDouble(); System.out.println("Enter Random Number Of Allice And Bob:"); System.out.print("X="); x=input.nextDouble(); System.out.print("Y="); y=input.nextDouble();
24

Network Security

Jul 01, 2015

Download

Education

Network Security. Various Protocols & Algorithms.

A Practical HandBook for B.Sc IT Students.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 1

Roll No of Student: 31 Date: __________

IMPLEMENTING DIFFIE-HELMAN

The Diffie-Hellman protocol is a method for two computer users to generate a shared private key with which they can then exchange information across an insecure channel. Let the users be named Alice and Bob. First, they agree on two prime numbers and , where is large (typically at least 512 bits) and is a primitive root modulo . (In practice, it is a good idea to choose such that is also prime.) The numbers and need not be kept secret from other users. Now Alice chooses a large random number as her private key and Bob similarly chooses a large number . Alice then

computes , which she sends to Bob, and Bob computes , which he sends to Alice.

Now both Alice and Bob compute their shared key , which Alice computes as

and Bob computes as

Alice and Bob can now use their shared key to exchange information without worrying about other users obtaining this information. In order for a potential eavesdropper (Eve) to do so, she would first

need to obtain knowing only , , and .

This can be done by computing from and from . This is the discrete logarithmproblem, which is computationally infeasible for large . Computing the discrete logarithm of a number modulo takes roughly the same amount of time as factoring the product of two primes the same size as , which is what the security of the RSA cryptosystem relies on. Thus, the Diffie-Hellman protocol is roughly as secure as RSA The following java program shows the implementation of Diffie-Helman algorithm

import java.util.*; import java.io.*;

public class Diffe { public static void main(String[] ar)

{ double a,b,k1,k2,x,y,n,g; System.out.println("Enter Two Prime No:");

Scanner input = new Scanner(System.in); System.out.print("N=");

n=input.nextDouble(); System.out.print("G="); g=input.nextDouble();

System.out.println("Enter Random Number Of Allice And Bob:"); System.out.print("X=");

x=input.nextDouble(); System.out.print("Y="); y=input.nextDouble();

Page 2: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 2

Roll No of Student: 31 Date: __________

a=Math.pow(g,x)%n; b=Math.pow(g,y)%n;

System.out.println("A:"+a); System.out.println("B:"+b); System.out.println("-------The Key Generated by Allice & Bob-------");

k1=Math.pow(b,x)%n; System.out.println("K1:"+k1); k2=Math.pow(a,y)%n;

System.out.println("K2:"+k2); } }

OUTPUT :

Page 3: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 3

Roll No of Student: 31 Date: __________

PRACTICAL NO.2

import java.io.*;

import java.lang.*;

import java.util.*;

class RSA

{

public static void main(String[] args)

{

int p,q;

Scanner sc=new Scanner(System.in);

System.out.println("Prime Number");

p=sc.nextInt();

q=sc.nextInt();

int i=2;

for(i=2;i<=p-q;i++)

{

if(p-1.i==0)

{

System.out.println("Not a Prime Number");

break;

}

}

}

Page 4: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 4

Roll No of Student: 31 Date: __________

if(i==p)

System.out.println("No: is a Prime Number");

for(i=2;i<=q-1;i++)

{

if(q%i==0)

{

System.out.println("Not a Prime Number");

break;

}

}

if(1==q)

System.out.println("No: is a Prime Number");

int n=(p*q);

System.out.println("Multiply"+n);

int fact=[(p-1)*(q-1)];

System.out.println(fact);

System.out.println("Enter e for RSA");

int e=sc.nextInt();

while(fact % e==0)

{

e=sc.nextInt();

}

System.out.println("fact is divisible by"+e);

int d;

Page 5: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 5

Roll No of Student: 31 Date: __________

for(d=1;d<=fact;d++)

{

if((d*e)% fact==1)

{

System.out.println(d);

break;

}

}

System.out.println("Plain Text");

double pt=sc.nextInt();

double ct=(Math.Pow(pt,e)%n);

System.out.println(ct);

System.out.println("Send"+ct+"to receiver");

double ans= (Math.Pow(ct,d)%n);

System.out.println("pt"+ans);

}

}

OUTPUT

Page 6: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 6

Roll No of Student: 31 Date: __________

IMPLEMENTING CAESAR CIPHER

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's

code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type

of substitution cipherin which each letter in the plaintext is replaced by a letter some fixed number of

positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would

become B, and so on. The method is named after Julius Caesar, who used it in his private

correspondence.

The following java program shows the implementation of Caesar Cipher

import java.util.*; class CaesarCipher { private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; public String encrypt(String plainText,int shiftKey) { plainText = plainText.toLowerCase(); String cipherText=""; for(int i=0;i<plainText.length();i++) { int charPosition = ALPHABET.indexOf(plainText.charAt(i)); int keyVal = (shiftKey+charPosition)%26; char replaceVal = this.ALPHABET.charAt(keyVal); cipherText += replaceVal; } return cipherText; } public String decrypt(String cipherText, int shiftKey) { cipherText = cipherText.toLowerCase(); String plainText=""; for(int i=0;i<cipherText.length();i++) { int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i)); int keyVal = (charPosition-shiftKey)%26; if(keyVal<0) { keyVal = this.ALPHABET.length() + keyVal; } char replaceVal = this.ALPHABET.charAt(keyVal); plainText += replaceVal; } return plainText; }

Page 7: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 7

Roll No of Student: 31 Date: __________

} class CaesarDemo { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter Plain Text :"); String plainText =sc.nextLine(); int shiftKey=3; CaesarCipher cc = new CaesarCipher(); String cipherText = cc.encrypt(plainText,shiftKey); System.out.println("Your Cipher Text :" + cipherText); String cPlainText = cc.decrypt(cipherText,shiftKey); System.out.println("Your Plain Text :" + cPlainText); } } OUTPUT :

PRACTICAL NO.4

class RC4

{

Page 8: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 8

Roll No of Student: 31 Date: __________

string str PlainText;

static char cipher[];

RC4(String str PlainText,int []key)

{

this.str PlainText=str PlainText;

int s[]=new int[255];

cipher =new char[str Plaintext.length()];

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

{

s[i]=i;

}

int i=0;int j=0;

for(int k=0;k<str PlainText.length();k++)

{

int mod k=(k% key.length);

int kc= key[mod k];

j=(s[i]+j+kc)% 256+1;

int temp=s[i];

s[i]=s[j];

s[j]=temp;

int sc=(s[i]+s[j]%256);

int ck =(sc);

cipher[k]=(char)(ck^(int)str PlainText.charAt(k));

i=i+1;

Page 9: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 9

Roll No of Student: 31 Date: __________

}

}

public static void main(String []args)

{

int K[]={1,2,3,4,5,6};

string str Original="HELLO WORLD";

System.out.println("Original String-->>"+str Original);

new RC4 (str Original,K);

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

{

System.out.println(""+cipher[i]);

}

}

}

}

IMPLEMENTING MONO-ALPHABETIC CIPHER

A mono-alphabetic cipher is a simple substitution cipher wherein each letter of the plaintext is replaced by another letter in the ciphertext. An example of a mono-alphabetic cipher key follows:

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 j r s q x z o e w n d y v p f a t b c i l h g k m u

Page 10: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 10

Roll No of Student: 31 Date: __________

This key means that any 'j' in the plaintext will be replaced by an 'A' in the ciphertext, any 'r' in the plaintext will be replaced by a 'B' in the ciphertext, and so on.

The following java program shows the implementation of Mono-alphaetic Cipher

class MonoAlphabetic { private final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; private String newKey = ""; private static int isGenerated = 0; private void generateKey(String userKey) { // removing duplicate charaters from user key userKey = userKey.toLowerCase(); for(int i=0;i<userKey.length();i++) { int flag = 0; for(int j=0;j<this.newKey.length();j++) { if(userKey.charAt(i)==newKey.charAt(j)) { flag = 1; break; } } if(flag==0) this.newKey += userKey.charAt(i); } if(isGenerated==0){ isGenerated = 1; this.generateKey(this.newKey+""+this.ALPHABET); } } public String encrypt(String plainText, String userKey) { this.generateKey(userKey); String cipherText = ""; String tmpStr = plainText; for(int i=0;i<plainText.length();i++) { char replaceVal = this.newKey.charAt(this.ALPHABET.indexOf(plainText.charAt(i))); tmpStr = tmpStr.replace(tmpStr.charAt(i),replaceVal); } cipherText = tmpStr; return cipherText; }

Page 11: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 11

Roll No of Student: 31 Date: __________

public String decrypt(String cipherText, String userKey) { this.generateKey(userKey); String plainText = ""; String tmpStr = cipherText; for(int i=0;i<cipherText.length();i++) { char replaceVal = this.ALPHABET.charAt(this.newKey.indexOf(cipherText.charAt(i))); tmpStr = tmpStr.replace(tmpStr.charAt(i),replaceVal); } plainText = tmpStr; return plainText; } } class MonoAlphabeticDemo { public static void main(String args[]) { MonoAlphabetic ma = new MonoAlphabetic(); String en = ma.encrypt("hihowareyou","studentitzone"); String de = ma.decrypt(en,"studentitzone"); System.out.println(en + " - " + de); } } OUTPUT:

IMPLEMENTING MODIFIED CAESAR CIPHER

In cryptography, a substitution cipher is a method of encoding by which units of plaintext are replaced with cipher text, according to a regular system; the "units" may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by

performing an inverse substitution. In Caesar cipher technique we displace the alphabets 3 places down the line, we can use a variant of Caesar cipher where we can move any places up or down the line.

Page 12: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 12

Roll No of Student: 31 Date: __________

The following java program shows the implementation of Modified Caesar Cipher import java.util.Scanner; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Caesar { public static void main(String[] args){ String cip=Caesar.encrypt(); Caesar.decrypt(cip); } private static String encrypt() { char alphanum[]= {'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','0','1','2','3','4', '5','6','7','8','9','!','@','#','$','%','^','&','(',')','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','+','-','*','/','[',']','{','}','=','<','>','?','_'}; String empty = "empty"; Scanner input = new Scanner(System.in); System.out.println("Enter the plaintext"); String plainText = input.nextLine(); String cipher = null; char[] plain = plainText.toCharArray(); for(int i = 0;i<plain.length;i++){ for(int j = 0 ; j<85;j++){ if(j<=80){ if(plain[i]==alphanum[j]){ plain[i] = alphanum[j+5]; break; } } else if(plain[i] == alphanum[j]){ plain[i] = alphanum [j-81]; } } } cipher = String.valueOf(plain); System.out.println(" cipher text is "+cipher);

Page 13: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 13

Roll No of Student: 31 Date: __________

Scanner in = new Scanner(System.in); System.out.println("To Decrypt plaintext enter 1"); int choice = in.nextInt(); if(choice == 1) { return cipher; } else { System.out.println("Thank you"); } return empty; } private static String decrypt(String cip) { char alphanum[] ={'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','0','1','2','3','4','5','6','7','8','9','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','!','@','#','$','%', '^','&','(',')','+','-','*','/','[',']','{','}','=','<','>','?','_'}; String cipher = null; String empty = "empty"; char[] cipher1 = cip.toCharArray(); if(cip .equals(empty)) { System.out.println(" No text is Decrypted"); } else { for(int i = 0;i<cipher1.length;i++) { for(int j = 0 ; j<85;j++) { if(j>=5 && cipher1[i]==alphanum[j]) { cipher1[i] = alphanum[j-5]; break; } if(cipher1[i] == alphanum[j] && j<5){ cipher1[i] = alphanum[81+j]; break; } }

Page 14: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 14

Roll No of Student: 31 Date: __________

} } cipher=String.valueOf(cipher1); System.out.println(" Plain text is "+cipher); return cipher; } } OUTPUT :

IMPLEMENTING POLY-ALPHABETIC CIPHER

In a polyalphabetic cipher, multiple cipher alphabets are used. To facilitate encryption, all the alphabets

are usually written out in a largetable, traditionally called a tableau. The tableau is usually 26×26, so that

26 full ciphertext alphabets are available. The method of filling the tableau, and of choosing which

alphabet to use next, defines the particular polyalphabetic cipher. All such ciphers are easier to break

than once believed, as substitution alphabets are repeated for sufficiently large plaintexts.

Page 15: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 15

Roll No of Student: 31 Date: __________

The following java program shows the implementation of Poly-alphabetic Cipher

package polyciipher; import java.util.*; public class PolyCiipher { public static void main(String[] args) { int[] j = new int[100]; int[] s = new int[100]; String test=""; try{ Scanner in = new Scanner(System.in); System.out.println("Enter the plain text(STRING SHOULD BE IN UPPERCASE AND DONT GIVE SPACE BETWEEN WORDS)::"); test = in.nextLine(); for ( int i = 0; i < test.length(); ++i ) { char c = test.charAt( i );// "c" holds the individual character of the string s[i] = (int) c-65; } for(int i=0;i<test.length()-1;i++){ j[i+1]=s[i]; } System.out.println("Enter the key::"); int k = Integer.parseInt(in.nextLine()); j[0]=k; System.out.println(); System.out.println("The position of the character in the cipher text::"); for(int i=0;i<test.length();i++){ j[i]=j[i]+s[i]; j[i]=j[i]%26; System.out.print(j[i]); } System.out.println(); System.out.println("The cipher text::"); for(int i=0;i<test.length();i++){ char c=(char) (j[i]+65); System.out.print(c); } System.out.println(); } catch(Exception er){ System.out.println("--YOU HAVE TYPE INVALID DATA--"); } } }

Page 16: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 16

Roll No of Student: 31 Date: __________

OUTPUT :

IMPLEMENTING RAIL-FENCE CIPHER

Rail Fence Cipher" (also called a zigzag cipher) generally refers to a form of transposition cipher. It derives its name from the way in which it is encoded.

In the rail fence cipher, the plaintext 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 The following java program shows the implementation of Rail-fence Cipher

Page 17: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 17

Roll No of Student: 31 Date: __________

import java.util.*; public class railfence { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter Plain Text:"); String input =sc.nextLine(); String output = ""; int len = input.length(),flag = 0; for(int i=0;i<len;i+=2) { output += input.charAt(i); } for(int i=1;i<len;i+=2) { output += input.charAt(i); } System.out.println("Ciphered Text : "+output); } } OUTPUT:

IMPLEMENTING SIMPLE COLUMNAR CIPHER

In cryptography, a transposition cipher is a method of encryption by which the positions held by units

of plaintext (which are commonly characters or groups of characters) are shifted according to a regular

system, so that the ciphertext constitutes apermutation of the plaintext. That is, the order of the units is

changed. Mathematically a bijective function is used on the characters' positions to encrypt and

an inverse function to decrypt.

Page 18: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 18

Roll No of Student: 31 Date: __________

In a columnar transposition, the message is written out in rows of a fixed length, and then read out

again column by column, and the columns are chosen in some scrambled order. Both the width of the

rows and the permutation of the columns are usually defined by a keyword. For example, the

word ZEBRAS is of length 6 (so the columns are of length 6), and the permutation is defined by the

alphabetical order of the letters in the keyword. In this case, the order would be "6 3 2 4 1 5".

In a regular columnar transposition cipher, any spare spaces are filled with nulls; in an irregular

columnar transposition cipher, the spaces are left blank. Finally, the message is read off in columns, in

the order specified by the keyword.

The following java program shows the implementation of Simple columnar Cipher

import java.io.*; public class Play { static String s1,st,d; static StringBuffer s; static int m,n,c,choice,p,q,k; static int z[]=new int[10]; static char a[][]; public static void dis() { System.out.println(); System.out.println("Matrix :"); System.out.println(); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(a[i][j]!='$') System.out.print(a[i][j]+" "); else System.out.print(" "); } System.out.println(); } System.out.println(); } public static void enc(DataInputStream dis)throws Exception {

Page 19: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 19

Roll No of Student: 31 Date: __________

while(true) { c=0; s1=""; System.out.println("------------------------------------------------------------------------"); System.out.print("Enter columns Sequence between 1 to "+n+" : "); st=dis.readLine(); d=st+d; System.out.println(); for(int i=0;i<n;i++) { c=(int)st.charAt(i)-49; for(int j=0;j<m;j++) { if(a[j][c]!='$') s1=s1+a[j][c]; } } s1.trim(); c=0; for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(c<s1.length()) a[i][j]=s1.charAt(c++); else a[i][j]='$'; dis(); System.out.println(); System.out.println(); System.out.print("Do You want to continue(yes(1)/no(0)) : "); choice=Integer.parseInt(dis.readLine()); if(choice==0) { System.out.println("****************************************************************"); System.out.println(); System.out.println("Ecryption results in the ciphertext : "+s1); System.out.println(); //System.exit(0); return; }

Page 20: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 20

Roll No of Student: 31 Date: __________

} } public static void dec() { k=0; p=s1.length()/n; q=s1.length()%n; //System.out.println("p = "+p+" q = "+q+" d = "+d); for(int i=0;i<m;i++) for(int j=0;j<n;j++) a[i][j]='$'; for(int i=0;i<d.length();i++) { c=(int)d.charAt(i)-49; //System.out.println("c = "+c); if(c>=q) { for(int j=0;j<p;j++) { a[j][c]=s1.charAt(k++); } } else { for(int j=0;j<p+1;j++) { a[j][c]=s1.charAt(k++); } } dis(); if(k==s1.length()) { s1=""; k=0; for(int x=0;x<m;x++) for(int j=0;j<n;j++) if(a[x][j]!='$') { s1=s1+a[x][j]; a[x][j]='$'; }

Page 21: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 21

Roll No of Student: 31 Date: __________

} } System.out.println("Decryption results in the plaintext : "+s1); } public static void main(String[] args) { try { DataInputStream dis=new DataInputStream(System.in); System.out.print("Enter Plain text : "); s1=dis.readLine(); s=new StringBuffer(s1); //REMOVING WIDE-SPACES for(int i=0;i<s.length();i++) if(s.charAt(i)==' ') s.deleteCharAt(i); s1=new String(s); d=""; System.out.println("Enter size of the array "); System.out.print("Enter no of rows = "); m=Integer.parseInt(dis.readLine()); System.out.print("Enter no of columns = "); n=Integer.parseInt(dis.readLine()); a=new char[m][n]; c=0; //ENTERING IN THE ARRAY for(int i=0;i<m;i++) for(int j=0;j<n;j++) if(c<s1.length()) a[i][j]=s1.charAt(c++); else a[i][j]='$'; dis(); System.out.println("------------------------------------------------------------------------"); enc(dis); System.out.println("-------------------------------Decryption-------------------------------"); dec(); } catch(Exception e) {}

Page 22: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 22

Roll No of Student: 31 Date: __________

} } OUTPUT :

IMPLEMENTING VERNAM CIPHER

The Vernam Cipher is based on the principle that the plain text of a message is 'mixed' with random text

from a One Time Pad (OTP). Because the resulting cipher text is still truely random, it can safely be sent

over the air, without the risk of being deciphered by an interceptor. At the receiving end, the same OTP is

used to 'unmix' the random text from the cipher text, which results in the original plain text. One only

Page 23: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 23

Roll No of Student: 31 Date: __________

has to guarantee that the OTP is safe, that there are only two copies of it, and that both copies are

destroyed immediately after use

The following java program shows the implementation of Vernam Cipher

//VernamCipher.java - cipher based on random pad

import java.io.*;

import java.util.*; //random number methods

class VernamCipher{

public static void main(String[] args)

throws IOException

{

if (args.length < 3) {

System.out.println("Usage: " +

"java VernamCipher clearFile codeFile key(an int)");

System.exit(1);

}

// open the input and output files

Scanner in = Scanner.create(new File(args[0]));

PrintWriter out = new PrintWriter(args[1]);

// use the key to start the pseudo-random sequence

Random r = new Random(Integer.parseInt(args[2]));

// encrypt one line at a time

while (in.hasNext()){

out.println(encrypt(in.nextLine(), r));

}

in.close();

out.close();

}

/**

Encrypt one string using a Vernam cipher.

@param message - a string to be encrypted

@param r - the source of random characters for

the encryption

@return the encrypted string

*/

public static String encrypt(String message,

Random r)

{

// for monitoring purposes print the unencrypted string

System.out.println(message);

char c;

// for efficiency use a StringBuffer instead of a String

StringBuffer cipher =

new StringBuffer(message.length());

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

c = message.charAt(i);

if (Character.isLetter(c)) {

Page 24: Network Security

AKBAR PEERBHOY COLLEGE OF COMMERCE AND ECONOMICS T. Y. B. Sc. IT Network Security - Journal 2014-15

Name of Student: Fahad Shaikh Subject Teachers Sign: _____________ Page 24

Roll No of Student: 31 Date: __________

// for simplicity we only handle upper case letters

cipher.append( (char)

((Character.toUpperCase(c) - 'A' +

(int)(r.nextDouble() * 26)) % 26 + 'A'));

}

else {

// don't change non-letters

cipher.append(c);

}

}

String result = cipher.toString();

// for monitoring purposes print the encrypted string

System.out.println(result);

return result;

}

}

OUTPUT :