Cns 3-1 Lab Manual
Cns 3-1 Lab Manual
____________________________________________________
MALLA REDDY ENGINEERING COLLEGE & MANAGEMENT SCIENCES
(Approved by AICTE New Delhi & Affiliated to JNTUH, Kistapur (V), Medchal (M),
Medchal-MalkajgiriDist. -501401
Subject Code:
Course Objectives:
Course Outcomes:
➢ Student will be able to understand basic cryptographic algorithms, message and web
➢ Authentication and security issues.
➢ Ability to identify information system requirements for both of them such as client and
server.
➢ Ability to understand the current legal issues towards information security.
1. Write a C program that contains a string (char pointer) with a value ‘Hello world’. 4
The program 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’. 5
The program 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 7
algorithms.
a. Ceaser cipher
b. Substitution cipher 11
c. Hill Cipher 13
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello 26
world” using Blowfish. Create your own key using Java key tool.
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA. 34
11. Calculate the message digest of a text using the MD5 algorithm in JAVA. 37
Aim: Write a C program that contains a string (char pointer) with a value \Hello World’. The
program should XOR each character in this string with 0 and display the result.
PROGRAM:
#include<stdlib.h>
main()
char str1[11];
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
str1[i]=str[i]^0;
printf("%c",str1[i]);
printf("\n");
}
Output:
Hello World
AIM: Write a C program that contains a string (char pointer) with a value \Hello World’. 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()
char str1[11];
char str2[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
str1[i] = str[i]&127;
printf("%c",str1[i]);
printf("\n");
for(i=0;i<len;i++)
printf("%c",str2[i]);
printf("\n");
Output:
Hello World
Hello World
Hello World
AIM: 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 static String encrypt(String str, int key) { String encrypted = "";
int c = str.charAt(i);
if (Character.isUpperCase(c))
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
encrypted += (char) c;
}
MREM CRYPTOGRAPHY AND NETWORK SECURITY LAB Page 8
return encrypted;
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (key % 26);
if (c < 'A')
c = c + 26;
else if (Character.isLowerCase(c)) {
c = c - (key % 26);
if (c < 'a')
c = c + 26;
decrypted += (char) c;
return decrypted;
}
MREM CRYPTOGRAPHY AND NETWORK SECURITY LAB Page 9
}
Output:
PROGRAM:
import java.io.*;
import java.util.*;
String a = "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
char c;
for(int i=0;i<str.length();i++)
c = str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
Output:
C) Hill Cipher
import java.util.*;
import java.io.*;
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((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];
for(int i=0;i<3;i++)
System.out.print((char)(decrypt[i][0]%26+97));
System.out.print("\n");
for(int j=0;j<3;j++)
a[i][j] = sc.nextFloat();
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;
for(int k=0;k<3;k++) {
for(int i=0;i<3;i++) {
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("");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
System.out.print("\n");
}
MREM CRYPTOGRAPHY AND NETWORK SECURITY LAB Page 16
}
Output:
Inverse Matrix is :
PROGRAM:
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
privateKeySpecmyKeySpec;
privateSecretKeyFactorymySecretKeyFactory;
byte[] keyAsBytes;
SecretKey key;
myEncryptionKey = "ThisIsSecretEncryptionKey";
myEncryptionKey.getBytes(UNICODE_FORMAT);
mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
try
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedString = base64encoder.encode(encryptedText); }
catch (Exception e) {
e.printStackTrace(); }
returnencryptedString; }
String decryptedText=null;
try
cipher.init(Cipher.DECRYPT_MODE, key);
System.out.println("");
OUTPUT:
EXPERIMENT NO. 5
PROGRAM:
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Encoder;
KeyGeneratorkeyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
cipherOut.init(Cipher.ENCRYPT_MODE, secretKey);
int input = 0;
fin.close(); cout.close();
}
}
OUTPUT:
PROGRAM:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
int i;
strbuf.append("0");
return strbuf.toString();
public static void main(String[] args) throws Exception { String message="AES still rocks!!";
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
OUTPUT:
Initialization Vector of the Cipher: dI1MXzW97oQ
Using Java cryptography; encrypt the text “Hello world” using Blowfish. Create your own
key using Java key tool.
AIM: Write the RC4 logic in Java Using Java Cryptography, encrypt the text “Hello world”
using Blowfish. Create your own key using Java key tool.
PROGRAM:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
KeyGeneratorkeygenerator = KeyGenerator.getInstance("Blowfish");
// create a key
// encrypt message
// decrypt message
System.exit(0);
}}
OUTPUT:
PROGRAM:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.*;
import java.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);
int y, intGCD;
BigInteger e;
BigInteger gcd;
do {
y = x.nextInt(fiofn.intValue()-1);
String z = Integer.toString(y);
e = new BigInteger(z);
gcd = fiofn.gcd(e);
intGCD = gcd.intValue();
return e;
OUTPUT:
EXPERIMENT NO. 9
Aim: To i mplement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript.
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;
{
// TODO code application logic here
BigInteger(Integer.toString(gValue));
BigIntegerXa = new
BigIntegerXb = new
BigInteger(Integer.toString(XbValue));
createKey();
p = BigInteger.probablePrime(bitLength, rnd);
g = BigInteger.probablePrime(bitLength, rnd);
createSpecificKey(p, g);
KeyPairGeneratorkpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512);
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec.class);
KeyPairGeneratorkpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(param);
KeyPairkp = kpg.generateKeyPair();
KeyFactorykfactory = KeyFactory.getInstance("DiffieHellman");
DHPublicKeySpec.class);
OUTPUT:
EXPERIMENT NO. 10
Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
import java.security.*;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(input.getBytes());
System.out.println();
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
input = "abcdefghijklmnopqrstuvwxyz";
MREM CRYPTOGRAPHY AND NETWORK SECURITY LAB Page 34
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println(""); }
catch (Exception e) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
returnbuf.toString(); }
OUTPUT:
Algorithm = SHA1
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 SHA1("abc") =
A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz")=32D10C7B8CF96570CA04CE37F2A19D8424
0D3A89
EXPERIMENT NO. 11
Calculate the message digest of a text using the MD5 algorithm in JAVA.
AIM: To Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
import java.security.*;
try .
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(input.getBytes());
System.out.println();
input = "abc";
md.update(input.getBytes());
System.out.println();
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
catch (Exception e) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
return buf.toString();
}
MREM CRYPTOGRAPHY AND NETWORK SECURITY LAB Page 38
}
OUTPUT:
Algorithm = MD5
D41D8CD98F00B204E9800998ECF8427E MD5("abc") =
900150983CD24FB0D6963F7D28E17F72 MD5("abcdefghijklmnopqrstuvwxyz") =
C3FCD3D76192E4007DFB496CCA67E13B