61
TY.Bsc.IT NETWORK SECURITY Practuical No:-1 Aim:- Implementing AES import java.security.*; import javax.crypto.*; import java.io.*; public class AESAlgo { Cipher ecipher; Cipher dcipher; AESAlgo(SecretKey key) { try { ecipher=Cipher.getInstance("AES"); dcipher=Cipher.getInstance("AES"); ecipher.init(Cipher.ENCRYPT_MODE,key); dcipher.init(Cipher.DECRYPT_MODE,key); } catch(Exception e) {} } public String encrypt(String str) { Name :-Bharat Choudhary Roll No:-153

Documentns

Embed Size (px)

DESCRIPTION

xbdfgdg

Citation preview

TY.Bsc.IT NETWORK SECURITY

Practuical No:-1

Aim:- Implementing AES

import java.security.*;

import javax.crypto.*;

import java.io.*;

public class AESAlgo

{

Cipher ecipher;

Cipher dcipher;

AESAlgo(SecretKey key)

{

try

{

ecipher=Cipher.getInstance("AES");

dcipher=Cipher.getInstance("AES");

ecipher.init(Cipher.ENCRYPT_MODE,key);

dcipher.init(Cipher.DECRYPT_MODE,key);

}

catch(Exception e)

{}

}

public String encrypt(String str)

{

try

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

byte[] utf8=str.getBytes("UTF8");

byte[] enc=ecipher.doFinal(utf8);

return new sun.misc.BASE64Encoder().encode(enc);

}

catch(Exception e)

{}

return null;

}

public String decrypt(String str)

{

try

{

byte[] dec=new sun.misc.BASE64Decoder().decodeBuffer(str);

byte[] utf8=dcipher.doFinal(dec);

return new String(utf8,"UTF8");

}

catch(Exception e)

{}

return null;

}

public static void main(String args[])

{

SecretKey key=null;

try

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

KeyGenerator keygen=KeyGenerator.getInstance("AES");

key=keygen.generateKey();

}

catch(Exception e)

{

e.printStackTrace();

}

AESAlgo ae=new AESAlgo(key);

String ori="Advance Encryption Standard";

String en=ae.encrypt(ori);

String de=ae.decrypt(en);

System.out.println("Original String:"+ori);

System.out.println("Encrypted String:"+en);

System.out.println("Decrypted String:"+de);

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No:-1{a}

Aim:- Caesar Cipher

import java.io.*;

public class Caesar

{

int offset=3;

public String encrypt(String s) throws IOException

{

StringBuffer sb=new StringBuffer();

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

{

char t=s.charAt(i);

if((t>='A')&&(t<='Z'))

{

int t1=t-'A'+offset;

t1=t1%26;

sb.append((char)(t1+'A'));

}

else if((t>='a')&&(t<='z'))

{

int t1=t-'a'+offset;

t1=t1%26;

sb.append((char)(t1+'a'));

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

return sb.toString();

}

public String decrypt(String s) throws IOException

{

StringBuffer sb=new StringBuffer();

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

{

char t=s.charAt(i);

if((t>='A')&&(t<='Z'))

{

int t1=t-'A'-offset;

if(t1<0) t1=26+t1;

sb.append((char)(t1+'A'));

}

else if((t>='a')&&(t<='z'))

{

int t1=t-'a'-offset;

if(t1<0) t1=26+t1;

sb.append((char)(t1+'a'));

}

}

return sb.toString();

}

public static void main(String[] args) throws IOException

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

BufferedReader b;

System.out.println("\n\t\t Caesar Encryption Technology");

System.out.print(" Enter the string:");

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

String oriStr=b.readLine();

Caesar c=new Caesar();

String encStr=c.encrypt(oriStr);

System.out.println("\n\t\t The Encrypted string is: "+encStr);

String decStr=c.decrypt(encStr);

System.out.println("\n\t\t The Decrypted string is: "+decStr);

System.out.println("\n");

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No: 1. {b}

Aim:- Modified Caesar Cipher

import java.io.*;

public class ModifiedCaesar

{

int offset=7;

public String encrypt(String s) throws IOException

{

StringBuffer sb=new StringBuffer();

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

{

char t=s.charAt(i);

if((t>='A')&&(t<='Z'))

{

int t1=t-'A'+offset;

t1=t1%26;

sb.append((char)(t1+'A'));

}

else if((t>='a')&&(t<='z'))

{

int t1=t-'a'+offset;

t1=t1%26;

sb.append((char)(t1+'a'));

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

}

return sb.toString();

}

public String decrypt(String s) throws IOException

{

StringBuffer sb=new StringBuffer();

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

{

char t=s.charAt(i);

if((t>='A')&&(t<='Z'))

{

int t1=t-'A'-offset;

if(t1<0) t1=26+t1;

sb.append((char)(t1+'A'));

}

else if((t>='a')&&(t<='z'))

{

int t1=t-'a'-offset;

if(t1<0) t1=26+t1;

sb.append((char)(t1+'a'));

}

}

return sb.toString();

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

public static void main(String[] args) throws IOException

{

BufferedReader b;

System.out.println("\n\t\t Modified Caesar Encryption Technology");

System.out.print(" Enter the string:");

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

String oriStr=b.readLine();

ModifiedCaesar c=new ModifiedCaesar();

String encStr=c.encrypt(oriStr);

System.out.println("\n\t\t The Encrypted string is: "+encStr);

String decStr=c.decrypt(encStr);

System.out.println("\n\t\t The Decrypted string is: "+decStr);

System.out.println("\n");

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No.1. {C}

Aim: Mono-Alphabetic

import java.io.*;

import java.util.Random;

public class Monoalphabetic

{

public int offset()

{

int set;

Random ran= new Random();

set= ran.nextInt(10);

System.out.print(" "+set);

return set;

}

public String encrypt(String s)throws IOException

{

StringBuffer sb=new StringBuffer();

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

{

char t=s.charAt(i);

if((t>='A')&&(t<='Z'))

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

int t1=t-'A'+offset();

System.out.println(" "+offset());

t1=t1%26;

sb.append((char)(t1+'A'));

}

else if((t>='a')&&(t<='z'))

{

int t1=t-'a'+offset();

t1=t1%26;

sb.append((char)(t1+'a'));

}

}

return sb.toString();

}

public static void main(String[] args)throws IOException

{

BufferedReader b;

System.out.println("\n\t\t Monoalphabetic Encryption Technique ");

System.out.print("\n Enter the string: ");

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

String oriStr=b.readLine();

Monoalphabetic c=new Monoalphabetic();

String encStr=c.encrypt(oriStr);

System.out.println("\n The encrypted string is: "+encStr);

System.out.println("\n");

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

}

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No.2. {A}

Aim: Program to implement Transposition Techniques

A) Rail Fence Technique

Source Code:

import java.io.*;

import java.lang.*;

public class Rail

{

public static void main(String args[])

{

try

{

System.out.println("\n\n\t Rail Fence Technique ");

BufferedReader b;

String oriText,encText,decText;

System.out.print("\n Enter the string to Encrypt: ");

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

oriText=b.readLine();

int m=oriText.length();

System.out.print("\n The encrypted string is: ");

for(int i=0;i<m;)

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

{

char t=oriText.charAt(i);

System.out.print(t);

i=i+2;

}

for(int i=1;i<m;)

{

char t=oriText.charAt(i);

System.out.print(t);

i=i+2;

}

System.out.println("\n");

}

catch(Exception I)

{

System.out.print(" "+I);

}

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No.2. {B}

Aim: Program to implement Transposition Techniques

B) Simple Columnar Technique

Source Code:

import java.util.*;

public class SimpleColumnar

{

String cipherText=" ";

String plainText=" ";

int numrow=0;

int numcol=0;

char[][] strpt;

public void generateCipher()

{

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][3];

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][5];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][0];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][1];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][4];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][2];

}

public void generatePlain()

{

int k=0;

for(int j=0;j<numcol;j++)

{

strpt[j][3]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

for(int j=0;j<numcol;j++)

{

strpt[j][5]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][0]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][1]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][4]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

for(int j=0;j<numcol;j++)

{

strpt[j][2]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

}

public String Encrypt(String text)

{

plainText=text.toLowerCase();

plainText=text.replaceAll("\\s "," ");

strpt=new char[20][6];

int row=0, col=0,j=0;

while(j<plainText.length())

{

for(col=0;col<6;col++)

{

if(j<(plainText.length()))

strpt[row][col]=plainText.charAt(j++);

else

break;

}

col=0;

++row;

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

int r=row;

System.out.println("Columns are: ");

System.out.println("col1|col2 |col3 |col4 |col5 |col6 |");

System.out.println("-----------------------------------");

for(row=0;row<r;row++)

{

for(col=0;col<6;col++)

{

System.out.print(" "+ strpt[row][col] + " | ");

}

System.out.println(" ");

}

System.out.println("-----------------------------------");

numrow=plainText.length()/6;

numcol=plainText.length()%6;

if(numcol!=0)

numrow +=1;

switch(numcol)

{

case 0:

generateCipher(); break;

case 1:

generateCipher(); break;

case 2:

generateCipher(); break;

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

case 3:

generateCipher(); break;

case 4:

generateCipher(); break;

case 5:

generateCipher(); break;

}

cipherText=cipherText.toUpperCase();

return cipherText;

}

public String Decrypt(String text)

{

cipherText=text.replaceAll("\\s"," ");

cipherText=text.toLowerCase();

char[][] strpt=new char[20][6];

numrow=cipherText.length()/6;

numcol=cipherText.length()%6;

int k=0;

switch(numcol)

{

case 0:

generatePlain();break;

case 1:

generatePlain();break;

case 2:

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

generatePlain();break;

case 3:

generatePlain();break;

case 4:

generatePlain();break;

case 5:

generatePlain();break;

}

int row=0,col=0;

for(k=0;k<cipherText.length();k++)

{

text+=strpt[row][col];

if(col>=5)

{

col=0;

row+=1;

}

else

col+=1;

}

return plainText;

}

public static void main(String args[])

{

String pt=" ",ct=" ",dpt=" ";

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

System.out.println("\n\t\t Simple Columnar Technique \n");

Scanner str=new Scanner(System.in);

System.out.print("Enter a plain text: ");

pt=str.nextLine();

System.out.println("\n");

SimpleColumnar s1=new SimpleColumnar();

ct=s1.Encrypt(pt);

System.out.println("Encrypted text is: "+ct);

dpt=s1.Decrypt(ct);

System.out.println("Decrypted text is: "+dpt);

}

}

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No: 2. {C}

Aim: Multicolumnar

import java.util.*;

public class MultipleColumnar

{

String cipherText=" ";

String plainText=" ";

int numrow=0;

int numcol=0;

char[][] strpt;

public void generateCipher()

{

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][3];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][5];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][0];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][1];

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][4];

for(int i=0;i<numrow;i++)

cipherText+=strpt[i][2];

}

public void generatePlain()

{

int k=0;

for(int j=0;j<numcol;j++)

{

strpt[j][3]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][5]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

strpt[j][0]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][1]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][4]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

for(int j=0;j<numcol;j++)

{

strpt[j][2]=cipherText.charAt(k++);

if(k>=cipherText.length())

break;

}

}

public String Encrypt(String text)

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

plainText=text.toLowerCase();

plainText=text.replaceAll("\\s "," ");

strpt=new char[20][6];

int row=0, col=0,j=0;

while(j<plainText.length())

{

for(col=0;col<6;col++)

{

if(j<(plainText.length()))

strpt[row][col]=plainText.charAt(j++);

else

break;

}

col=0;

++row;

}

int r=row;

System.out.println("Columns are: ");

System.out.println("col1| col2 | col3 | col4 | col5 | col6 | ");

System.out.println("----------------------------------------");

for(row=0;row<r;row++)

{

for(col=0;col<6;col++)

{

System.out.print(" "+ strpt[row][col] + " | ");

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

System.out.println(" ");

}

System.out.println("----------------------------------------");

numrow=plainText.length()/6;

numcol=plainText.length()%6;

if(numcol!=0)

numrow +=1;

switch(numcol)

{

case 0:

generateCipher(); break;

case 1:

generateCipher(); break;

case 2:

generateCipher(); break;

case 3:

generateCipher(); break;

case 4:

generateCipher(); break;

case 5:

generateCipher(); break;

}

cipherText=cipherText.toUpperCase();

return cipherText;

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

public static void main(String args[])

{

String pt=" ",ct=" ",dpt=" ";

System.out.println("\n\t\t Simple Columnar Technique \n");

Scanner str=new Scanner(System.in);

System.out.print("Enter a plain text: ");

pt=str.nextLine();

System.out.println("\n");

System.out.print("Enter the no. of rounds: ");

int num=Integer.parseInt(str.nextLine());

System.out.println("\n");

for(int i=1;i<=num;i++)

{

System.out.println("Round "+i);

MultipleColumnar s1=new MultipleColumnar();

ct=s1.Encrypt(pt);

pt=ct;

System.out.println("Encrypted text is: "+ct);

System.out.println("\n");

}

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No:-2.{D}

Aim:- D) Vigenere Cipher (Polyalphabetic Cipher)

import java.io.*;

public class VigenereCipher

{

public static void main(String args[])

{

String key="CIPHER";

String ori="thiscryptosystemisnotsecure";

String enc=encrypt(ori,key);

System.out.println("\n"+enc);

System.out.println("\n"+decrypt(enc,key));

}

static String encrypt(String text,final String key)

{

String res=" ";

text=text.toUpperCase();

for(int i=0,j=0;i<text.length();i++)

{

char c=text.charAt(i);

if(c<'A'||c>'Z')continue;

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

res +=(char)((c+key.charAt(j)-2*'A')%26+'A');

j=++j%key.length();

}

return res;

}

static String decrypt(String text,final String key)

{

String res=" ";

text=text.toUpperCase();

for(int i=0,j=0;i<text.length();i++)

{

char c=text.charAt(i);

if(c<'A'||c>'Z')continue;

res +=(char)((c-key.charAt(j)+26)%26+'A');

j=++j%key.length();

}

return res;

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No:- 3

Aim: - Implementing Diffie Helman Key Exchange Algorithm

import java.util.*;

import java.lang.*;

import java.math.*;

class DHelman

{

int x,y;

BigInteger n,g,a1,b1,k1,k2;

Random rnd;

public DHelman()

{

rnd=new Random();

x=3;

y=6;

n=new BigInteger(7,2,rnd);

g=new BigInteger(7,2,rnd);

}

void calk()

{

a1=(g.pow(x)).mod(n);

b1=(g.pow(y)).mod(n);

k1=(b1.pow(x)).mod(n);

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

k2=(a1.pow(y)).mod(n);

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

System.out.println("g"+g);

System.out.println("k1"+k1);

System.out.println("k2"+k2);

}

public static void main(String [] args)

{

DHelman d=new DHelman();

d.calk();

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No: 4

Aim: Implementing DES Algorithm

import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.*;

class DESAlgo

{

public static void main(String args[])

{

try

{

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

Key key=keygen.generateKey();

Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE,key);

String str="DES Cipher";

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

byte[] arr1=str.getBytes();

byte[] arr2=cipher.doFinal(arr1);

byte[] initvector=cipher.getIV();

System.out.println("Encrypted String:" +arr2);

System.out.println("we are now decrypting");

IvParameterSpec ps=new IvParameterSpec(initvector);

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

cipher.init(Cipher.DECRYPT_MODE,key,ps);

byte[] arr3=cipher.doFinal(arr2);

String str2= new String(arr3);

System.out.println("The Decrypted String:" +str2);

}

catch(Exception e)

{

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

}

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No:5

Aim:- Implementing AES

import java.security.*;

import javax.crypto.*;

import java.io.*;

public class AESAlgo

{

Cipher ecipher;

Cipher dcipher;

AESAlgo(SecretKey key)

{

try

{

ecipher=Cipher.getInstance("AES");

dcipher=Cipher.getInstance("AES");

ecipher.init(Cipher.ENCRYPT_MODE,key);

dcipher.init(Cipher.DECRYPT_MODE,key);

}

catch(Exception e)

{}

}

public String encrypt(String str)

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

{

try

{

byte[] utf8=str.getBytes("UTF8");

byte[] enc=ecipher.doFinal(utf8);

return new sun.misc.BASE64Encoder().encode(enc);

}

catch(Exception e)

{}

return null;

}

public String decrypt(String str)

{

try

{

byte[] dec=new sun.misc.BASE64Decoder().decodeBuffer(str);

byte[] utf8=dcipher.doFinal(dec);

return new String(utf8,"UTF8");

}

catch(Exception e)

{}

return null;

}

public static void main(String args[])

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

SecretKey key=null;

try

{

KeyGenerator keygen=KeyGenerator.getInstance("AES");

key=keygen.generateKey();

}

catch(Exception e)

{

e.printStackTrace();

}

AESAlgo ae=new AESAlgo(key);

String ori="Advance Encryption Standard";

String en=ae.encrypt(ori);

String de=ae.decrypt(en);

System.out.println("Original String:"+ori);

System.out.println("Encrypted String:"+en);

System.out.println("Decrypted String:"+de);

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No:-6

Aim:- Implementing RSA Algorithm

import java.math.*;

import java.security.*;

public class RSAalgo

{

BigInteger p,q,n,d,e,ph,t;

SecureRandom r;

public RSAalgo()

{

r=new SecureRandom();

p=new BigInteger(512,100,r);

q=new BigInteger(512,100,r);

System.out.println("\n RSA algo");

System.out.println("\n prime no p="+p.intValue());

System.out.println("\n Prime no q="+q.intValue());

n=p.multiply(q);

ph=(p.subtract(new BigInteger("1")));

ph=ph.multiply(q.subtract(new BigInteger("1")));

e=new BigInteger("2");

while(ph.gcd(e).intValue()>1||e.compareTo(ph)!=-1)

e=e.add(new BigInteger("1"));

d=e.modInverse(ph);

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

System.out.println("\n Public key=(" +n.intValue()+","+e.intValue()+")");

System.out.println("\n Private key=(" +n.intValue()+","+d.intValue()+")");

BigInteger msg=new BigInteger("96");

System.out.println("\n Message is:"+msg);

BigInteger enmsg=encrypt(msg,e,n);

System.out.println("\n Encrypted msg is:"+enmsg.intValue());

BigInteger demsg=decrypt(enmsg,d,n);

System.out.println("\n Decrypted msg is:"+demsg.intValue());

}

BigInteger encrypt(BigInteger msg,BigInteger e,BigInteger n)

{

return msg.modPow(e,n);

}

BigInteger decrypt(BigInteger msg,BigInteger e,BigInteger n)

{

return msg.modPow(d,n);

}

public static void main(String[] args)

{

new RSAalgo();

}

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practical No: 7

Aim: Implementing RC4.

import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.*;

class RC4algo

{

String strPlainText;

static char cipher[];

RC4algo(String strPlainText,int[]key)

{

this.strPlainText=strPlainText;

int S[]=new int[255];

cipher=new char[strPlainText.length()];

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

{

S[i]=1;

}

int i=0;

int j=0;

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

{

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

int modk=(k% key.length);

int Kc=key[modk];

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=S[Sc];

cipher[k]=(char)(Ck^(int)strPlainText.charAt(k));

i=i+1;

}

}

public static void main(String args[])

{

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

String strOriginal="Hello world";

System.out.println("Original string is: "+strOriginal);

new RC4algo(strOriginal,K);

System.out.println("\n Encrypted String is:");

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

{

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

}

System.out.println("\n");

}

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

}

Output:-

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

Practitcal No:-8

Aim:- Implementing BlowFish

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import java.io.*;

public class BlowFishAlgo

{

public static void main(String[] args) throws Exception

{

KeyGenerator keygen=KeyGenerator.getInstance("BlowFish");

SecretKey secretkey=keygen.generateKey();

Cipher cip=Cipher.getInstance("BlowFish");

cip.init(Cipher.ENCRYPT_MODE,secretkey);

System.out.println("\n\t BlowFish cipher \n");

System.out.println("enter string: ");

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

String inputText=br.readLine();

byte[] encrypted=cip.doFinal(inputText.getBytes());

cip.init(Cipher.DECRYPT_MODE,secretkey);

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

String en=new String(encrypted);

System.out.println("\n");

String de=new String(decrypted);

System.out.println("\n Encrypted String: " +en);

Name :-Bharat Choudhary Roll No:-153

TY.Bsc.IT NETWORK SECURITY

System.out.println("\n Decrypted String: " +de);

}

}

Output:-

Name :-Bharat Choudhary Roll No:-153