CNS Lab Manual
CNS Lab Manual
DEPARTMENT
OF
COMPUTER SCIENCE ENGINEERING
1
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
LIST OF EXPERIMENTS
S.NO TOPIC PAGE
. NUMBER
7 Using Java Cryptography, encrypt the text “Hello world” using 19-20
BlowFish. Create your own key using Java keytool.
8 Write a Java program to implement RSA Algorithm 21-22
2
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
3
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
4
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
5
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
c = c - (key % 26);
if (c < 'a')
c = c + 26;
}
decrypted += (char) c;
}
return decrypted;
}
}
Output:
Enter any String: Hello World
Enter the Key: 5
Encrypted String is: MjqqtBtwqi
Decrypted String is: Hello World
7
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
b) Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
public class SubstitutionCipher {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// TODO code application logic here
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
System.out.print("Enter any string: ");
String str = br.readLine();
String decrypt = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encrypted data is: " +decrypt);
}
}
Output:
Enter any string: aceho
The encrypted data is: zxvsl
8
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
c) Hill Cipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*; public
class HillCipher {
static float[][] decrypt = new float[3][1];
static float[][] a = new float[3][3]; static
float[][] b = new float[3][3]; static
float[][] mes = new float[3][1]; static
float[][] res = new float[3][1];
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
// TODO code application
logic here getkeymes();
for(int i=0;i<3;i++) for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }
System.out.print("\nEncrypted string is :
"); for(int i=0;i<3;i++) {
System.out.print((char)(res[i][0]%26+97));
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]; }
9
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
q = c[k][k];
for(int j=0;j<3;j++) {
if(i!=k) { b[i][j] = b[i][j]*q-p*b[k][j];
}}}}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
b[i][j] = b[i][j]/c[i][i]; }
System.out.println("");
System.out.println("\nInverse Matrix is : ");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
System.out.print(b[i][j] + " ");
System.out.print("\n"); }
}}
Output:
Enter a 3 letter string: hai
Encrypted string is :fdx
Inverse Matrix is :
0.083333336 0.41666666 -0.33333334
-0.41666666 -0.083333336 0.6666667
0.5833333 -0.083333336 -0.33333334
Decrypted string is: hai
11
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec = new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
public String encrypt(String unencryptedString)
{ String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);
BASE64Encoder base64encoder = new BASE64Encoder();
encryptedString = base64encoder.encode(encryptedText); }
catch (Exception e) {
e.printStackTrace(); }
returnencryptedString; }
public String decrypt(String encryptedString)
{ String decryptedText=null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
byte[] plainText = cipher.doFinal(encryptedText); decryptedText=
bytes2String(plainText); }
catch (Exception e) {
e.printStackTrace(); }
returndecryptedText; }
private static String bytes2String(byte[] bytes)
13
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
{ StringBufferstringBuffer = new
StringBuffer(); for (int i = 0; i <bytes.length;
i++) { stringBuffer.append((char) bytes[i]); }
returnstringBuffer.toString(); }
public static void main(String args []) throws Exception
{ System.out.print("Enter the string: ");
DES myEncryptor= new DES();
String stringToEncrypt = br.readLine();
String encrypted = myEncryptor.encrypt(stringToEncrypt);
String decrypted = myEncryptor.decrypt(encrypted);
System.out.println("\nString To Encrypt: " +stringToEncrypt);
System.out.println("\nEncrypted Value : " +encrypted);
System.out.println("\nDecrypted Value : " +decrypted);
System.out.println("");
}
}
OUTPUT:
Enter the string: Welcome
String To Encrypt: Welcome
Encrypted Value : BPQMwc0wKvg=
Decrypted Value : Welcome
14
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
15
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
OUTPUT:
Initialization Vector of the Cipher: dI1MXzW97oQ=
Contents of inputFile.txt: Hello World
Contents of outputFile.txt: ùJÖ˜ NåI“
16
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
18
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
System.exit(0);
}}
OUTPUT:
Input your message: Hello world
Encrypted text: 3ooo&&(*&*4r4
Decrypted text: Hello world
20
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
8. RSA Algorithm
AIM: Write a Java program to implement RSA Algorithm.
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Random;
import java.util.Scanner;
public class RSA {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter a Prime number: ");
BigInteger p = sc.nextBigInteger(); // Here's one prime
number.. System.out.print("Enter another prime number:
"); BigInteger q = sc.nextBigInteger(); // ..and another.
BigInteger n = p.multiply(q);
BigInteger n2 = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e = generateE(n2);
BigInteger d = e.modInverse(n2); // Here's the multiplicative inverse
System.out.println("Encryption keys are: " + e + ", " + n);
System.out.println("Decryption keys are: " + d + ", " + n);
}
public static BigIntegergenerateE(BigIntegerfiofn) {
int y, intGCD;
BigInteger e;
21
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
BigInteger gcd;
Random x = new Random();
do {
y = x.nextInt(fiofn.intValue()-1);
String z = Integer.toString(y);
e = new BigInteger(z);
gcd = fiofn.gcd(e);
intGCD = gcd.intValue();
}
while(y <= 2 || intGCD != 1);
return e;
}
}
OUTPUT:
Enter a Prime number: 5
Enter another prime number: 11
Encryption keys are: 33, 55
Decryption keys are: 17, 55
22
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
9. Diffie-Hellman
AIM: Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript
application as another party (bob).
PROGRAM:
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom; import
javax.crypto.spec.DHParameterSpec; import
javax.crypto.spec.DHPublicKeySpec; public
class DiffeHellman { public final static int
pValue = 47;
public final static int gValue = 71;
public final static int XaValue = 9;
public final static int XbValue = 14;
public static void main(String[] args) throws
Exception { // TODO code application logic here
BigInteger p = new BigInteger(Integer.toString(pValue));
BigInteger g = new BigInteger(Integer.toString(gValue));
BigIntegerXa = new
BigInteger(Integer.toString(XaValue)); BigIntegerXb =
new BigInteger(Integer.toString(XbValue)); createKey();
intbitLength = 512; // 512 bits
SecureRandomrnd = new SecureRandom();
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
23
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
createSpecificKey(p, g);
}
public static void createKey() throws Exception {
KeyPairGeneratorkpg =
KeyPairGenerator.getInstance("DiffieHellman"); kpg.initialize(512);
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpeckspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
DHPublicKeySpec.class);
System.out.println("Public key is: " +kspec);
}
public static void createSpecificKey(BigInteger p, BigInteger g) throws
Exception { KeyPairGeneratorkpg =
KeyPairGenerator.getInstance("DiffieHellman"); DHParameterSpecparam = new
DHParameterSpec(p, g); kpg.initialize(param);
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpeckspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
DHPublicKeySpec.class);
System.out.println("\nPublic key is : " +kspec);
}
}
OUTPUT:
Public key is: javax.crypto.spec.DHPublicKeySpec@5afd29
Public key is: javax.crypto.spec.DHPublicKeySpec@9971ad
24
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
10. SHA-1
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import java.security.*;
public class SHA1 {
public static void main(String[] a) {
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();
System.out.println("SHA1(\"" +input+"\") = " +bytesToHex(output));
System.out.println(""); }
catch (Exception e) {
25
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
26
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
27
PYDAH COLLEGE OF ENGINEERING (Approved by AICTE, New
Delhi and Affiliated to JNTUK, Kakinada) YANAM ROAD, PATAVALA KAKINADA,
533461, E.G.Dist,
}
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'};
StringBufferbuf = 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:
Message digest object info:
Algorithm = MD5
Provider = SUN version 1.6
ToString = MD5 Message Digest from SUN, <initialized> MD5("") =
D41D8CD98F00B204E9800998ECF8427E MD5("abc") =
900150983CD24FB0D6963F7D28E17F72 MD5("abcdefghijklmnopqrstuvwxyz") =
C3FCD3D76192E4007DFB496CCA67E13B
28
29