CNS Lab Manual
CNS Lab Manual
CNS Lab Manual
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
i
ii
INDEX
1. Write a C program that contains a string (char pointer) with a value ‘Hello world’. The program 1-2
should XOR each character in this string with 0 and displays the result.
2. Write a C program that contains a string (char pointer) with a value ‘Hello world’. The program 3-4
should AND or and XOR each character in this string with 127 and display the result
3. Write a Java program to perform encryption and decryption using the following algorithms
a. Ceaser cipher 5-7
b. Substitution cipher 8-9
c. Hill Cipher 10-13
4. Write a C/JAVA program to implement the DES algorithm logic. 14-17
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello world” using 23-24
Blowfish. Create your own key using Java key tool.
8. Write a Java program to implement RSA algorithm. 25-27
9. Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript 28-30
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA. 31-33
11. Calculate the message digest of a text using the MD5 algorithm in JAVA 34-36
iii
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
1) Write a C program that contains a string (char pointer) with a value \HelloWorld’. The program
should XOR each character in this string with 0 an display the result.
PROGRAM:
#include<stdlib.h>
main()
{
printf("\n");
}
1
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Hello World
Hello World
2
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
2)Write a C program that contains a string (char pointer) with a value \HelloWorld’. The program
should AND or and XOR each character in this string with 127 and display the result.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
void main()
{
printf("\n"); for(i=0;i<len;i++)
{
str3[i] = str2[i]^127;
printf("%c",str3[i])
}
printf("\n");
}
3
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Hello World
Hello World
Hello World
4
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
3) Write a Java program to perform encryption and decryption using the following algorithms:
a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher
PROGRAM:
a) Ceaser Cipher
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CeaserCipher {
String encrypted = encrypt(str, key); System.out.println("\nEncrypted String is: " +encrypted); String
str.charAt(i);
}
else if (Character.isLowerCase(c)) { c = c + (key % 26);
if (c > 'z')
c = c - 26;
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key)
{ String decrypted = "";
for(int i = 0; i < str.length(); i++)
{ int c = str.charAt(i);
if (Character.isUpperCase(c))
c = c - (key % 26);
if (c < 'A')
decrypted += (char) c;
}
return decrypted;
}
6
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Enter any String: Hello World
7
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
b) Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
{
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encrypted data is: " +decrypt);
}
8
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Enter any string: aceho
The encrypted data is: zxvsl
9
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
C) Hill Cipher
PROGRAM:
import java.io.*; import java.util.*;
HillCipher
static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws
IOException {
getkeymes();
for(int k=0;k<3;k++)
res[i][j]=res[i][j]+a[i][k]*mes[k][j]; }
res[i][0]=res[i][0];
}
inverse();
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
10
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
for(int k=0;k<3;k++) {
decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j];
}
System.out.print("\nDecrypted string is : "); for(int
i=0;i<3;i++)
{ System.out.print((char)(decrypt[i][0]%26+97));
}
System.out.print("\n");
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) a[i][j] = sc.nextFloat();
System.out.print("\nEnter a 3 letter string: ");
String msg = br.readLine();
for(int i=0;i<3;i++)
mes[i][0] = msg.charAt(i)-97;
{ floatp,q;
float[][] c = a;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
//a[i][j]=sc.nextFloat();
if(i==j)
b[i][j]=1;
else b[i][j]=0;
11
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
for(int k=0;k<3;k++)
{
for(int i=0;i<3;i++)
{
p = c[i][k];
q = c[k][k];
for(int j=0;j<3;j++)
{
if(i!=k)
{
c[i][j] = c[i][j]*q-p*c[k][j];
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("");
12
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Enter a 3 letter string: hai Encrypted
13
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DES
{
private static final String UNICODE_FORMAT = "UTF8";
public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
privateKeySpecmyKeySpec;
privateSecretKeyFactorymySecretKeyFactory;
myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
myKeySpec c = c + 26;
14
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
}
else if (Character.isLowerCase(c)) { c = c - (key % 26);
if (c < 'a') c = c + 26
}
= new DESedeKeySpec(keyAsBytes);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme); cipher =
Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
}
bytes2String(plainText); }
catch (Exception e) {
e.printStackTrace(); }
returndecryptedText; }
private static String bytes2String(byte[] bytes)
15
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
System.out.println("");
}
}
16
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Enter the string: Welcome String To
Encrypt: Welcome
17
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
java.io.FileOutputStream; import
java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import avax.crypto.KeyGenerator; import
BlowFish
{
public static void main(String[] args) throws Exception {
KeyGeneratorkeyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
CipherOutputStream(fout, cipherOut);
int input = 0;
cout.write(input); }
fin.close(); cout.close(); } }
18
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Initialization Vector of the Cipher: dI1MXzW97oQ=
19
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
javax.crypto.*; import
int i;
} return strbuf.toString();
skey.getEncoded();
20
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
}
}
21
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Input your message: Hello KGRCET
22
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
7)Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello world” using Blowfish.
Create your own key using Java key tool.
import javax.crypto.Cipher;
importjavax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class BlowFishCipher{
public static void main(String[Jargs) throws Exception{
//create a keygenerator based upon the
KeyGenerator keygenerator= KeyGenerator.getlnstance("Blowfish");
//create a key
SecretKeysecretkey=keygenerator.generateKey();
//create a cipher based upon Blowfish
Cipher cipher=Cipher getlnstance("Blowfish");
//initialise cipher to with secretkey
cipher.init(Cipher.ENCRYPT_IVIODE,secretkey);
//get the text to encrypt
String inputText = "Hello world";
//encrypt message
byte[] encrypted=cipher.doFinal(inputText.getBytes());
//re-initialise the cipher to be in decrypt mode
cipher.init(Cipher.DECRYPT_IVIODE,secretkey);
//decrypt messagebyte[] decrypted=cipher.doFinal(encrypted);
//and display the results
System.out.println("Original String " + inputText);
System.out.println("Encrypted:" + new String(encrypted));
System.out.println("Decrypted " + new String(decrypted));
}
}
23
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
24
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
importjava.util.Random;
import java.util.Scanner;
BigInteger p = sc.nextBigInteger()
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);
25
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
int y, intGCD;
BigInteger e; BigInteger
gcd;
y = x.nextInt(fiofn.intValue()-1); String z =
Integer.toString(y);
fiofn.gcd(e);
intGCD = gcd.intValue();
}
}
}
26
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Enter a Prime number: 5
Enter another prime number: 11
27
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
9) Implement the Diffie-Hellman Key Exchange mechanism using HTML andJavaScript. Consider the end
user as one of the parties (Alice) and theJavaScript application as other 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;
new BigInteger(Integer.toString(XaValue));
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p, g);
}
public static void createKey() throws Exception {
28
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec.class);
System.out.println("Public key is: " +kspec);
}
public static void createSpecificKey(BigInteger p, BigInteger g)
kpg.initialize(param);
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec.class);
29
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Public key is: javax.crypto.spec.DHPublicKeySpec@5afd29 Public key
is: javax.crypto.spec.DHPublicKeySpec@9971ad
30
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
10)Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
PROGRAM:
import ava.security.*; public class
SHA1
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.digest(); System.out.println();
"abcdefghijklmnopqrstuvwxyz"; md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println(""); }
catch (Exception e)
}
31
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBufferbuf = new StringBuffer();
} returnbuf.toString();
}
}
32
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
Output:
Message digest object info:
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz")=32D10C7B8CF96570CA04CE37F2A19D8424 0D3A89
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
33
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
11)Calculate the message digest of a text using the MD5 algorithm in JAVA
import java.security •;
35
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
OUTPUT
36
JNTUH College of Enginnering Manthani
DEPARTMENT OF CSE CRYPTOGRAPHY AND NETWORK SECURITY
37
JNTUH College of Enginnering Manthani