C&NS Lab Manual
C&NS Lab Manual
NETWORK SECURITY
Lab Manual
JNTUK – R16
AUTHOR: MADHU BABU JANJANAM
Assoc. Professor, CSE
Madhu Babu Janjanam Cryptography and Network Security Lab
VVIT
1
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.util.*;
class CaesarCipher
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int shift,i,n,p,key;
String str;
String str1="";
System.out.println("Enter the Plain Text");
str=sc.nextLine();
str=str.toLowerCase();
n=str.length();
char ch1[]=str.toCharArray();
char ch4;
System.out.println("Enter the value by which each letter of the string is to be shifted");
shift=sc.nextInt();
System.out.println();
System.out.println("Encrypted text is:");
for(i=0;i<n;i++)
{
if(Character.isLetter(ch1[i]))
{
ch4=(char)(((int)ch1[i]+shift-97)%26+97);
str1=str1+ch4;
}
else if(ch1[i]==' ')
{
str1=str1+ch1[i];
}
}
System.out.println(str1);
System.out.println("Cipher Text:"+str1);
n=str1.length();
char ch2[]=str1.toCharArray();
char ch3;
System.out.println();
System.out.println("Possible Plain text is");
str1="";
for(key=26;key>=1;key--)
{
for(i=0;i<n;i++)
{
if(Character.isLetter(ch2[i]))
{
VVIT
2
Madhu Babu Janjanam Cryptography and Network Security Lab
ch3=(char)(((int)ch2[i]+key-97)%26+97);
str1=str1+ch3;
}
else if(ch2[i]==' ')
{
str1=str1+ch2[i];
}
}
p=26-key;
System.out.println("For Key "+p+":"+str1);
str1="";
}
}
}
Output:
VVIT
3
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.util.Scanner;
public class MonoalphabeticCipher
{
public static char p[] = { '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' };
public static char ch[] = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C',
'V', 'B', 'N', 'M' };
static String str;
public static String doEncryption(String s)
{
char c[] = new char[(s.length())];
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j < 26; j++)
{
if (p[j] == s.charAt(i))
{
c[i] = ch[j];
break;
}
}
}
return (new String(c));
}
VVIT
4
Madhu Babu Janjanam Cryptography and Network Security Lab
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message: ");
str=sc.next();
String en = doEncryption(str.toLowerCase());
System.out.println("Encrypted message: " + en);
System.out.println("Decrypted message: " + doDecryption(en));
sc.close();
}
}
Output:
VVIT
5
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.util.*;
class msg{
int a=97;
char all[]=new char[27];
msg()
{
for(int i=0;i<26;i++)
{
all[i]=(char)a;
a++;
}
}
int Ipos(char c)
{
int i=0;
for(;i<26;i++)
{
if(all[i]==c)
{
break;
}
}
return i;
}
char Cpos(int c)
{
int i=0;
for(;i<c;i++)
{
}
return all[i];
}
}
class OneTimePadCipherImplementation{
String Encryption(String plaintext,String key)
{
plaintext=plaintext.toLowerCase();
msg m1=new msg();
int pt[]=new int[plaintext.length()];
int k[]=new int[key.length()];
int ct[]=new int[plaintext.length()];
for(int i=0;i < plaintext.length();i++)
{
pt[i]=m1.Ipos(plaintext.charAt(i));
}
VVIT
6
Madhu Babu Janjanam Cryptography and Network Security Lab
VVIT
7
Madhu Babu Janjanam Cryptography and Network Security Lab
}
return plaintext;
}
}
class OneTimePad{
public static void main(String args[])throws Exception
{
String plaintext,key;
Scanner scn=new Scanner(System.in);
System.out.println("Enter plaintext:");
plaintext=scn.nextLine();
System.out.println("Enter key:");
key=scn.nextLine();
OneTimePadCipherImplementation OneTimePad=new
OneTimePadCipherImplementation();
String ciphertext=OneTimePad.Encryption(plaintext,key);
System.out.println("Encrpted text is:"+ciphertext);
System.out.println("Decrypted text is:"+OneTimePad.Decryption(ciphertext,key));
}
}
Output:
VVIT
8
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "VVIT";
System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
}
}
VVIT
9
Madhu Babu Janjanam Cryptography and Network Security Lab
Output:
VVIT
10
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "VVIT";
System.out.println("Your HashCode Generated by SHA is: " + getSHA(s));
}
}
VVIT
11
Madhu Babu Janjanam Cryptography and Network Security Lab
Output:
VVIT
12
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
class DesEncrypter {
Cipher ecipher;
Cipher dcipher;
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
VVIT
13
Madhu Babu Janjanam Cryptography and Network Security Lab
Output:
VVIT
14
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
class AesEncrypter {
Cipher ecipher;
Cipher dcipher;
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
VVIT
15
Madhu Babu Janjanam Cryptography and Network Security Lab
Output:
VVIT
16
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
DHServer.java
import java.net.*;
import java.io.*;
VVIT
17
Madhu Babu Janjanam Cryptography and Network Security Lab
out.writeUTF(Bstr); // Sending B
Bdash = ((Math.pow(clientA, b)) % clientP); // calculation of Bdash
System.out.println("Secret Key to perform Symmetric Encryption = "
+ Bdash);
server.close();
}
catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
}
catch (IOException e) {
}
}
}
DHClient.java
import java.net.*;
import java.io.*;
pstr = Integer.toString(p);
out.writeUTF(pstr); // Sending p
gstr = Integer.toString(g);
out.writeUTF(gstr); // Sending g
VVIT
18
Madhu Babu Janjanam Cryptography and Network Security Lab
serverB = Double.parseDouble(in.readUTF());
System.out.println("From Server : Public Key = " + serverB);
Output:
VVIT
19
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.math.BigInteger;
import java.security.SecureRandom;
modulus = p.multiply(q);
System.out.println("phi = " + phi);
publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1
privateKey = publicKey.modInverse(phi);
}
VVIT
20
Madhu Babu Janjanam Cryptography and Network Security Lab
Output:
VVIT
21
Madhu Babu Janjanam Cryptography and Network Security Lab
Program:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Scanner;
import java.io.*;
VVIT
22
Madhu Babu Janjanam Cryptography and Network Security Lab
System.out.print(" "+signature[i]);
System.out.println();
if(bool) {
System.out.println("Signature verified");
} else {
System.out.println("Signature failed");
}
}
}
Output:
VVIT
23