0% found this document useful (0 votes)
149 views

Practical

The document contains programs to implement various encryption algorithms including Caesar cipher, Monoalphabetic cipher, Vernam cipher, Rail fence cipher, simple columnar transposition cipher, DES algorithm, AES algorithm, and RSA algorithm. For each algorithm, the program code is provided to encrypt and decrypt a given string as well as sample outputs.

Uploaded by

Vaibhav Karambe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

Practical

The document contains programs to implement various encryption algorithms including Caesar cipher, Monoalphabetic cipher, Vernam cipher, Rail fence cipher, simple columnar transposition cipher, DES algorithm, AES algorithm, and RSA algorithm. For each algorithm, the program code is provided to encrypt and decrypt a given string as well as sample outputs.

Uploaded by

Vaibhav Karambe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Practical No.

Aim: Write programs to implement Caesar Cipher.

Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class caesarcipher
{
static String str
="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
static String strOutput="";
public static String encrypt(String strinput,int ishift)
{
for(int i=0;i<strinput.length();i++)
{
for(int j=0;j<26;j++)
{
if(strinput.charAt(i)==str.charAt(j))
{
strOutput+=str.charAt(j+ishift);
}
}
}
return strOutput;
}
public static String decrypt(String strinput,int ishift)
{
strOutput="";
for(int i=0;i<strinput.length();i++)
{
for(int j=26;j<52;j++)
{
if(strinput.charAt(i)==str.charAt(j))
{
strOutput+=str.charAt(j-ishift);
}
}
}
return strOutput;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE STRING TO BE ENCRYPTED");
String strInput=(String)br.readLine();
String s=strInput.toLowerCase();
strOutput=encrypt(s,3);
System.out.println("STRING IN ENCRYPTED FORMAT:"+strOutput);
strOutput=decrypt(strOutput,3);
System.out.println("STRING IN DECRYPTED FORMAT:"+strOutput);
}
}
Output :
C:\Users>cd..

C:\>cd jdk1.8.0_201

C:\jdk1.8.0_201>cd bin

C:\jdk1.8.0_201\bin>javac caesarcipher.java

C:\jdk1.8.0_201\bin>java caesarcipher
ENTER THE STRING TO BE ENCRYPTED
hello
STRING IN ENCRYPTED FORMAT:khoor
STRING IN DECRYPTED FORMAT:hello
Practical No.2

Aim: Write programs to implement Monoalphabetic Cipher.

Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class monoapl
{
static String str ="abcdefghijklmnopqrstuvwxyz";
static String str1="qwertyuioplkjhgfdsazxcvbnm";
static String strOutput="";
public static String encrypt(String strinput)
{
strinput=strinput.toLowerCase();
for(int i=0;i<strinput.length();i++)
{
for(int j=0;j<26;j++)
{
if(strinput.charAt(i)==str.charAt(j))
{
strOutput+=str.charAt(j);
}
}
}
return strOutput;
}
public static String decrypt(String strinput)
{
strOutput="";
for(int i=0;i<strinput.length();i++)
{
for(int j=0;j<26;j++)
{
if(strinput.charAt(i)==str1.charAt(j))
{
strOutput+=str.charAt(j);
}
}
}
return strOutput;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE STRING TO BE ENCRYPTED");
String strInput=(String)br.readLine();
strOutput=encrypt(strInput);
System.out.println("STRING IN ENCRYPTED FORMAT:"+strOutput);
strOutput=decrypt(strOutput);
System.out.println("STRING IN DECRYPTED FORMAT:"+strOutput);
}
}
Output:
C:\jdk1.8.0_201\bin>javac monoapl.java

C:\jdk1.8.0_201\bin>java monoapl
ENTER THE STRING TO BE ENCRYPTED
namrata
STRING IN ENCRYPTED FORMAT:namrata
STRING IN DECRYPTED FORMAT:yszdses
Practical No.3

Aim: Write Program to implement Vernam Cipher

Program:
import java.io.*;
import java.util.*;
class VernamCipherDemo
{
static String charset="abcdefghijklmnopqrstuvwxyz";
public static int getIndex(char s)
{
int indx=0;
for(int i=0;i<26;i++)
if(charset.charAt(i)==s)
indx=i;
return indx;
}
public static void main(String[]args)
{
String plaintext,key,ciphertext="";
Console c=System.console();
plaintext=c.readLine("Enter the message to be encrypted \n");
int len=plaintext.length();
int indx1,indx2,indx;
key=c.readLine("Enter the key to be used for the encryption of length"
+len+"characters:");
for(int i=0;i<len;i++){
indx1=getIndex(plaintext.charAt(i));
indx2=getIndex(key.charAt(i));
indx=(indx1+indx2)%26;
ciphertext=ciphertext+charset.charAt(indx);
}
System.out.println("The Cipher text is"+ciphertext);
String choice=c.readLine("\n Do you wan to decrypt the cipher text(yes/no)");
plaintext="";
if(choice.equalsIgnoreCase("yes")==true){
for(int i=0;i<len;i++) {
indx1=getIndex(ciphertext.charAt(i));
indx2=getIndex(key.charAt(i));
indx=indx1-indx2;
if(indx<0)
indx=26+indx;
else
indx=indx%26;
plaintext=plaintext+charset.charAt(indx);
}
System.out.println("The Decrypted message is "+plaintext);
}
else
System.exit(0);
}
}
Output:
C:\jdk1.8.0_201\bin>javac VernamCipherDemo.java
C:\jdk1.8.0_201\bin>java VernamCipherDemo
Enter the message to be encrypted
hello
Enter the key to be used for the encryption of length5characters:olleh
The Cipher text isvpwpv

Do you wan to decrypt the cipher text(yes/no)yes


The Decrypted message is hello
Practical No.4

Aim: Write Program to implement Rail Fence Cipher.

Program:
public class Railfence
{
public static void main(String[] args)
{
String input = "inputstring";
String output = "";
int len = input.length(),flag = 0;
System.out.println("Input String : " + input);
for(int i=0;i<len;i+=2)
{
output += input.charAt(i);
}
for(int i=1;i<len;i+=2)
{
output += input.charAt(i);
}
System.out.println("Ciphered Text : "+output);
}
}
Output:
C:\jdk1.8.0_201\bin>javac Railfence.java

C:\jdk1.8.0_201\bin>java Railfence
Input String : inputstring
Ciphered Text : ipttignusrn
Practical No.5

Aim: Write program to implement Simple Columnar Technique

Program:
import java.io.*;
import java.math.*;
class simplecolumner
{
public static void main(String[] args)throws IOException
{
String plaintext,ciphertext="",temp="";
BufferedReader br = new BufferedReader(new InputStreamReader
(System.in));
int i=0,j=0;
System.out.print("Enter plaintext :");
plaintext=br.readLine();
int plen=plaintext.length();
for(i=0;i<plen;i++)
{
if(plaintext.charAt(i)!='\0')
temp+=plaintext.charAt(i);
}
System.out.println("temp = "+temp);
int len=temp.length();
System.out.print("Enter no.of columns(max. "+len+"):");
int cols=Integer.parseInt(br.readLine());
if (cols<=len)
{
int rows=1,k=0;
if(len%cols==0)
rows=len/cols;
else
rows=(len/cols)+1;
char matrix[][] = new char[rows][cols];
for(i=0;i<rows;i++)
{
for(j=0;(j<cols)&&(k<len);++j)
{
matrix[i][j]=temp.charAt(k);
k++;
}
}
i--;
for(;j<cols;++j)
matrix[i][j]='#';
for(i=0;i<rows;i++)
{
for(j=0;j<cols;++j)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
System.out.println();
int enCol[]=new int[cols];
for(i=0;i<cols;++i)
{
System.out.print("Enter column no. "+(i+1)+" ");
enCol[i]=Integer.parseInt(br.readLine())-1;
}
if(enCol.length==cols)
{
for(i=0;i<cols;++i)
{
for(j=0;j<rows;++j)
{
ciphertext+=matrix[j][enCol[i]];
}
}
}
System.out.println("Ciphertext: "+ciphertext);
temp="";
for(i=0;i<ciphertext.length();++i)
if(ciphertext.charAt(i)!='#')
temp+=ciphertext.charAt(i);
ciphertext=temp;
System.out.println("Ciphertext : "+ciphertext);
}
else
System.out.println("Incorrect no.of columns!!!");
}
}
Output:
C:\jdk1.8.0_201\bin>javac simplecolumner.java

C:\jdk1.8.0_201\bin>java simplecolumner
Enter plaintext :namratabhoir
temp = namratabhoir
Enter no.of columns(max. 12):5
n a m r a
t a b h o
i r # # #

Enter column no. 1 3


Enter column no. 2 1
Enter column no. 3 5
Enter column no. 4 2
Enter column no. 5 4
Ciphertext: mb#ntiao#aarrh#
Ciphertext : mbntiaoaarrh
Practical No.6

Aim: Write program to encrypt and decrypt string using DES Algorithm.

Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class DES{
public static void main(String[]args)throws Exception{
KeyGenerator kg = KeyGenerator.getInstance("DES");//kg as keygen
// create a key
SecretKey sk = kg.generateKey();//sk as secretkey
Cipher cp = Cipher.getInstance("DES");//cp as cip
// initialise cipher to with secretkey
cp.init(Cipher.ENCRYPT_MODE,sk);
String it = JOptionPane.showInputDialog("Give Input:");//it as inputText
byte[] encryp = cp.doFinal(it.getBytes());// encryp as encrypted
cp.init(Cipher.DECRYPT_MODE,sk);
byte[] decryp = cp.doFinal(encryp); //decryp as decrypted
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted : "
+ new String(encryp) + "\n" +"decrypted : "+ new String(decryp));
System.exit(0);
}
}
Output:
C:\jdk1.8.0_201\bin>javac DES.java

C:\jdk1.8.0_201\bin>java DES
Practical No.7

Aim: Write Program to encrypt and decrypt string using AES Algorithm.

Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class AES{
public static void main(String[] args)throws Exception{
KeyGenerator kg = KeyGenerator.getInstance("AES");
SecretKey sk = kg.generateKey();
Cipher cp = Cipher.getInstance("AES");
cp.init(Cipher.ENCRYPT_MODE,sk);
String it = JOptionPane.showInputDialog("Give Input:");
byte[] encryp = cp.doFinal(it.getBytes());
cp.init(Cipher.DECRYPT_MODE,sk);
byte[] decryp = cp.doFinal(encryp);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted :"
+ new String(encryp) + "\n" + "decrypted :" + new String(decryp));
System.exit(0);
}
}
Output:
C:\jdk1.8.0_201\bin>javac AES.java

C:\jdk1.8.0_201\bin>java AES
Practical No.8

Aim: Write a Program to implement RSA algorithm to perform encryption


/decryption of a given string.

Program:
import java.math.*;
import java.security.*;
public class RSA
{
BigInteger p,q,n,d,e,ph;
SecureRandom r;
public RSA()
{
r=new SecureRandom();
p=new BigInteger(512,100,r);
q=new BigInteger(512,100,r);
System.out.println("prine nos p and q are "+p.intValue()+","+q.intValue());
n=p.multiply(q);
ph=(p.subtract(new BigInteger("1")));
ph=ph.multiply(q.subtract(new BigInteger("1")));
e=new BigInteger("2");
while(ph.gcd(e).intValue()>1)
e = e.add(new BigInteger("1"));
d=e.modInverse(ph);
System.out.println("public key is("+n.intValue()+","+e.intValue()+")");
System.out.println("private key is("+n.intValue()+"," +d.intValue()+")");
BigInteger msg= new BigInteger("15");
System.out.println("\nMessage is: "+msg);
BigInteger enmsg= encrypt(msg,e,n);
System.out.println("\nEncrypted msg is: "+enmsg.intValue());
BigInteger demsg=decrypt(enmsg,d,n);
System.out.println("\nDecrypted msg is: "+demsg.intValue());
}
BigInteger encrypt(BigInteger msg,BigInteger e,BigInteger n)
{
return msg.modPow(e,n);
}
BigInteger decrypt(BigInteger msg,BigInteger d,BigInteger n)
{
return msg.modPow(d,n);
}
public static void main(String[]args)
{
new RSA();
}
}
Output:
C:\jdk1.8.0_201\bin>javac RSA.java

C:\jdk1.8.0_201\bin>java RSA
prine nos p and q are 239610629,1284997201
public key is(1984634005,5)
private key is(1984634005,1135009165)

Message is: 15

Encrypted msg is: 759375

Decrypted msg is: 15


Practical No.9
Aim: Write a program to implement the Diffie-Hellman Key Agreement
algorithm to generate symmetric keys.

Program:
import java.util.*;
import java.math.BigInteger.*;
import java.math.*;
class DiffieHellmanDemo
{
public static void main(String args[])
{
BigInteger DJ,PJ,K1,K2,x,y,g,n;
Scanner s = new Scanner(System.in);
System.out.println("Enter DJ's prime number!");
n = s.nextBigInteger();
System.out.println("Enter PJ's prime number!");
g = s.nextBigInteger();
System.out.println("Enter DJ's secret key!");
x = s.nextBigInteger();
System.out.println("This key is sent to PJ's!");
System.out.println("Enter PJ's secret key!");
y = s.nextBigInteger();
System.out.println("This key is sent to DJ!");
DJ = g.modPow(x,n);
PJ = g.modPow(y,n);
K1 = PJ.modPow(x,n);
K2 = DJ.modPow(y,n);
System.out.println("DJ's key is " +K1);
System.out.println("PJ's key is " +K2);
}
}
Output:
C:\jdk1.8.0_201\bin>javac DiffieHellmanDemo.java

C:\jdk1.8.0_201\bin>java DiffieHellmanDemo
Enter DJ's prime number!
7
Enter PJ's prime number!
11
Enter DJ's secret key!
7
This key is sent to PJ's!
Enter PJ's secret key!
15
This key is sent to DJ!
DJ's key is 1
PJ's key is 1
INDEX
Sr.No. Practical Name Date Remark Sign

O1 Write programs to implement


Caesar Cipher.
02 Write programs to implement
Monoalphabetic Cipher
03 Write Program to implement
Vernam Cipher
04 Write Program to implement
Rail Fence Cipher.
05 Write program to implement
Simple Columnar Technique
06 Write program to encrypt
and decrypt string using DES
Algorithm.
07 Write Program to encrypt
and decrypt string using AES
Algorithm.
08 Write a Program to
implement RSA algorithm to
perform encryption
/decryption of a given string.
09 Write a program to
implement the Diffie-
Hellman Key Agreement
algorithm to generate
symmetric keys.

You might also like