Professional Documents
Culture Documents
416 - Gauravpandey - INS - TYBCS CS Ebook Journal
416 - Gauravpandey - INS - TYBCS CS Ebook Journal
S
SEM V - Information & Network Security
E-Journal
Roll No. 416/B
Date:
Place: Mumbai
External Examiner
INDEX
Code:
import java.util.Scanner;
Output:
Conclusion: Thus we have studied the implementation of Caesar’s Cipher.
Code :
import java.util.Scanner;
Output:
Practical No. 2
Aim: Write a program to execute One Time Pad Cipher.
Theory :
-Also called as Vernam Cipher or the Perfect Cipher.
-Here plaintext is combined with a random key.
-The Key must be atleast as long as the plaintext.
-Each key is used only once and sender receiver must destroy the key after use.
-There should be only two copies of the keys,1 with sender other
with receiver.
Code :
import java.util.Scanner;
if (message.length() == key.length()) {
System.out.println("Encrypted Text:");
System.out.println(Encrypt(message, key));
System.out.println("Decrypted Text:");
System.out.println(Decrypt((Encrypt(message, key)), key));
}
else
System.out.println("Please enter same length Plaintext and keyword.");
sc.close();
}
}
Output :
Practical No. 3
Aim: Write programs to implement the following Transposition Cipher
Techniques:
- 3.1.Simple Columnar Technique
- 3.2.Rail Fence Cipher
3.1.Columnar Cipher:
Theory:
-The message is written out in rows of a fixed length, and then read out again
column by column, and the columns are chosen in some scrambled order.
-Width of the rows and the permutation of the columns are usually defined
by a keyword.
-For example, the word HACK is of length 4 (so the rows are of length 4),
and the permutation is defined by the alphabetical order of the letters in the
keyword. In this case, the order would be “3 1 2 4”.
-Any spare spaces are filled with nulls or left blank or placed by Filler alphabets.
Code :
import java.util.Scanner;
//COLUMNAR LOGIC
}
}
System.out.println("Encrypted String: "+output);
}
Output :
Code :
import java.util.Arrays;
import java.util.Scanner;
Output :
Practical No. 4
Aim: Write a program to implement RSA algorithm to perform encryption /
decryption of a given string.
Theory:
-RSA algorithm is a public key encryption technique and is considered as the
most secure way of encryption.
-It was invented by Rivest, Shamir and Adleman in year 1978 and hence name
RSA algorithm.
-The entire RSA is all about creating public key, private key for encryption and
decryption.
-Lets see the encryption algorithm in detail.
RSA Logic:
Code :
import java.util.Scanner;
if (d[i] == 1)
{
pvtkey = b[i];
break;
}
else
{
i++;
}
}
//Step 5: Applying conditions on your private key
if (pvtkey > m) {
pvtkey = pvtkey % m;
}
if (pvtkey < 0) {
pvtkey = pvtkey + m;
}
System.out.println("*************************************************");
System.out.println("Public Key: " + e);
System.out.println("Private Key: " + pvtkey);
System.out.println("*************************************************");
}
else
System.out.println("The public key you chose is not a prime
number.");
}
else
System.out.println("The public key you chose is bigger than derived number
" +m);
}
if(inputNumber <= 1)
{
isItPrime = false;
return isItPrime;
}
else
{
for (int i = 2; i<= inputNumber/2; i++)
{
if ((inputNumber % i) == 0)
{
isItPrime = false;
break;
}
}
return isItPrime;
}
}
Output :
Theory:
-The Diffie–Hellman key exchange method allows two parties that have no prior
knowledge of each other to jointly establish a shared secret key over an insecure
channel.
-This key can then be used to encrypt and decrypt subsequent communications
using a symmetric key cipher.
-The whole point is to share a secret symmetric key between alice and bob which
they can use for further communication.
Code :
import java.util.Scanner;
if (SecretKeyAlice == SecretKeyBob)
{
System.out.println("Shared Secret Key = " + (int)SecretKeyAlice);
}
else
System.out.println("Your values don't match. Please try again.");
sc.close();
}
if(inputNumber <= 1)
{
isItPrime = false;
return isItPrime;
}
else
{
for (int i = 2; i<= inputNumber/2; i++)
{
if ((inputNumber % i) == 0)
{
isItPrime = false;
break;
}
}
return isItPrime;
}
}
Output :
Conclusion : Thus we have studied the implementation of Diffie-Hellman Key
Agreement.
Practical No. 6
Aim: Write a program to implement the MD5 algorithm compute the message
digest.
Theory:
-Message Digest is used to ensure the integrity of a message transmitted over an
insecure channel (where the content of the message can be changed).
-The message is passed through a
-Cryptographic hash function. This function creates a compressed image of the
message called Digest.
-Produces 128 bit message digest.
-Lets assume, Alice sent a message and digest pair to Bob. To check the integrity
of the message Bob runs the cryptographic hash function on the received
message and gets a new digest.
-Now, Bob will compare the new digest and the digest sent by Alice. If, both are
same then Bob is sure that the original message is not changed.
Code :
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
return hashtext;
// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "Thakur";
System.out.println("Your HashCode Generated by MD5 is: " + getMd5(s));
}
Output :
Practical No. 7
Aim: Write a program to calculate HMAC-SHA1 Signature.
Theory:
-In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash
function which takes an input and produces a 160-bit (20-byte) hash value known
as a message digest
-This was designed by the National Security Agency (NSA) to be part of the
Digital Signature Algorithm. Cryptographic weaknesses were discovered in
SHA-1, and the standard was no longer approved for most cryptographic uses
after 2010.
Code :
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
// Convert byte array into signum representation - convert byte array to a positive
magnitute
BigInteger no = new BigInteger(1, messageDigest);
return hashtext;
// Driver code
public static void main(String args[]) throws NoSuchAlgorithmException
{
String s = "Thakur";
System.out.println("Your HashCode Generated by SHA-1 is: " + getSHA(s));
}
Output :
Practical No. 8
Aim: Write a program to implement Socket Programming in java.
Theory:
-A socket is one endpoint of a two-way communication link between two
programs running on the network.
-A socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent to.
-An endpoint is a combination of an IP address and a port number. Every TCP
connection can be uniquely identified by its two endpoints. That way you can
have multiple connections between your host and the server.
-The java.net package in the Java platform provides a class, Socket, that
implements one side of a two-way connection between your Java program and
another program on the network.
Code :
Server.java
package server;
import java.io.*;
import java.net.*;
try
{
//ServerSocket class provides a mechanism for the server program to listen
for clients
ServerSocket ss=new ServerSocket(6666);
//establishes connection
Socket s=ss.accept();
//Data Input stream creates a stream to read the incoming data
DataInputStream dis=new DataInputStream(s.getInputStream());
//readUTF reads the incoming streaming data in string format
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}
catch(Exception e)
{
System.out.println(e);}
}
}
Client.java
package client;
import java.net.*;
import java.io.*;
try{
//Creates a connection to mentioned port and
//first arg deals with ip address of the server machine, in our case it is
the same machine so localhost
Socket s = new Socket("localhost",6666);
//Creates a stream to send data
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
//Writes data in String format
dout.writeUTF("Hello Server!");
//flush() method clears the stream
dout.flush();
dout.close();
s.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output :