0% found this document useful (0 votes)
113 views26 pages

INS

The document contains code for implementing various classical encryption techniques like Caesar cipher, Monoalphabetic cipher, Vernam cipher, Rail fence cipher, and Simple Columnar Transposition cipher in Java. The Caesar cipher and Monoalphabetic cipher code samples show how to encrypt and decrypt text using a shift value or keyword. The Vernam cipher code generates a random key of the same length as plaintext for encryption. The Rail fence cipher and Simple Columnar Transposition cipher code samples demonstrate how to encrypt plaintext by arranging it in a rail fence or columnar format according to a keyword or key, and decrypt the cipher text back to the original plaintext.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views26 pages

INS

The document contains code for implementing various classical encryption techniques like Caesar cipher, Monoalphabetic cipher, Vernam cipher, Rail fence cipher, and Simple Columnar Transposition cipher in Java. The Caesar cipher and Monoalphabetic cipher code samples show how to encrypt and decrypt text using a shift value or keyword. The Vernam cipher code generates a random key of the same length as plaintext for encryption. The Rail fence cipher and Simple Columnar Transposition cipher code samples demonstrate how to encrypt plaintext by arranging it in a rail fence or columnar format according to a keyword or key, and decrypt the cipher text back to the original plaintext.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

BNN COLLEGE, BHIWANDI INS

PRACTICAL-1
1.Caesar Cipher

Code:

package caesar.cipher;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CaesarCipher {
public static void main(String[] args) throws Exception {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter plain text:");
String pt=br.readLine();
String ct;
System.out.println("Enter key value:");
int key=Integer.parseInt(br.readLine());
ct=doEncrypt(pt,key);
System.out.println("The cipher text:"+ct);
pt=doDecrypt(ct,key);
System.out.println("The plain text:"+pt);
}
static String doEncrypt(String pt,int key){
char c;
int num;
StringBuffer sb=new StringBuffer(pt);
for(int i=0;i<sb.length();i++)
{
num=sb.charAt(i)-95;
num=num+key;
if(num>=26)

TYCS 1
BNN COLLEGE, BHIWANDI INS

num=num-26;
c=(char)((char)num+95);
sb.setCharAt(i, c);
}
return new String(sb);
}
static String doDecrypt(String ct,int key){
char c;
int num;
StringBuffer sb1=new StringBuffer(ct);
for(int i=0;i<sb1.length();i++)
{
num=sb1.charAt(i)-95;
num=num+key;
if(num>=26)
num=num-26;
c=(char)((char)num+95);
sb1.setCharAt(i, c);
}
return new String(sb1); }
}
OUTPUT:

TYCS 2
BNN COLLEGE, BHIWANDI INS

2.Monoalphabetic Cipher

Code:

package monoalphabetic;

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;

public class Monoalphabetic

{
public static void main(String[] args) throws IOException, Exception

{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the plain text");

String pt=br.readLine().toUpperCase();
String key="WXYNTFJQRILODHGFSABCZMVUEK";
String ct=doEncrypt(pt,key);

System.out.println("The cipher text:"+ct);

pt=doDecrypt(ct,key);
System.out.println("The Plain text:"+pt);

}
static String doEncrypt(String pt, String key)throws Exception

char a;
int num;

StringBuffer sb=new StringBuffer(pt);


for (int i=0;i<sb.length();i++)

TYCS 3
BNN COLLEGE, BHIWANDI INS

num=sb.charAt(i)-65;

a=key.charAt(num);
sb.setCharAt(i, a);

return new String (sb);


}

static String doDecrypt(String ct, String key) throws Exception


{

char a;
int num=0;

StringBuffer sb1=new StringBuffer(ct);

for(int i=0;i<sb1.length();i++)
{

a=sb1.charAt(i);
num=getIndex(a,key);

a=(char)(num+65);

sb1.setCharAt(i, a);
}
return new String (sb1);

static int getIndex(char a, String key) throws Exception

{
int j=-1;
int len=key.length();

for(int i=0;i<len;i++)

{
if(key.charAt(i)==a)

TYCS 4
BNN COLLEGE, BHIWANDI INS

j=i;

}
return j;

}
OUTPUT:

TYCS 5
BNN COLLEGE, BHIWANDI INS

PRACTICAL-2
Vernam Cipher

Code:

package vernam;

import java.util.Random;

import java.util.Scanner;

public class Vernam {

private static String tycs="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static String doEncrypt(String s,String key)

String encoded="";

for(int i=0;i<s.length();i++)

char c=s.charAt(i);

int first=position(c);

char d=key.charAt(i);

int second=position(d);

int sum=(first+second)%26;

sum+=65;

char v=(char)sum;

encoded+=String.valueOf(v);

return encoded;

public static void main(String[] args)

TYCS 6
BNN COLLEGE, BHIWANDI INS

Scanner sc=new Scanner(System.in);

System.out.println("Enter the plain text:");

String pt=sc.next();

String ct;

int l=pt.length();

String key=keyRandom(l);

ct=doEncrypt(pt,key);

System.out.println(ct);

public static String keyRandom(int lenOfkey)

String key="";

Random r=new Random();

for(int i=0;i<lenOfkey;i++)

int a=r.nextInt(26)+'A';

char c;

key+=(char)a;

return key;

public static int position(char c)

int l=0;

TYCS 7
BNN COLLEGE, BHIWANDI INS

for(int i=0;i<tycs.length();i++)

char m=tycs.charAt(i);

if(c==m)

l=i;

return l;

OUTPUT:

TYCS 8
BNN COLLEGE, BHIWANDI INS

PRACTICAL-3
1.RailFence

Code:

package rail;

public class Rail {

public void encrypt(){

StringBuffer ctxt=new StringBuffer();

char[] plaintext={'w','e','b','s','e','t'};

for(int i=0;i<plaintext.length;i=i+2) {

ctxt.append(plaintext[i]);

for(int j=1;j<plaintext.length;j=j+2){

ctxt.append(plaintext[j]);

System.out.println("Encrypted Text:"+ctxt);

public void decrypt(){

StringBuffer ptxt=new StringBuffer();

char[] cipher={'w','e','b','s','e','t'};

int halflen=(cipher.length)%2;

for(int i=0;i<6;i++) {

ptxt.append(cipher[i]);

int x=i+6;

for(int j=x;j<cipher.length;j++) {

TYCS 9
BNN COLLEGE, BHIWANDI INS

ptxt.append(cipher[j]);

break;

System.out.println("Decrypted text:"+ptxt);

public static void main(String[] args) {

Rail cc=new Rail();

cc.encrypt();

cc.decrypt();

OUTPUT:

TYCS 10
BNN COLLEGE, BHIWANDI INS

2.Simple Columnar

package simplecolumnar;

public class SimpleColumnar {

String key ="4213";

StringBuffer plainText= new StringBuffer("come home tomorrow");

StringBuffer reKey= new StringBuffer();

int row,col;

char[][] matrix;

StringBuffer encodedText= new StringBuffer();

StringBuffer decodedText= new StringBuffer();

public void prepareReKey(){

for(int i=1;i<=key.length();i++){

for(int j=0;j<key.length();j++){

int check=Character.getNumericValue(key.charAt(j));

if(i==check){

reKey.append(j+1); }

System.out.println("This is Rekey:" + reKey);

public void prepareMatrix(){

plainText.append("-");

col= key.length();

row= plainText.length()/col;

if ((plainText.length()%col)!= 0)

TYCS 11
BNN COLLEGE, BHIWANDI INS

row=row+1;

matrix=new char[row][col];

for(int i=0;i<row;i++) {

int a=i*col;

for(int j=0;j<col;j++){

if(plainText.charAt(a)=='-'){

matrix[i][j]=' ';

if(j==1){

matrix[i][j+1]=' ';

matrix[i][j+2]=' ';

break;

if(j==2){

matrix[i][j+1]=' ';

break;

matrix[i][j]=plainText.charAt(a);

a++;

for(int i=0;i<row;i++) {

for(int j=0;j<col;j++){

TYCS 12
BNN COLLEGE, BHIWANDI INS

System.out.print(matrix[i][j]);

System.out.print(" ");

System.out.println("");}

public void encryptText()

for(int i=0;i<row;i++) {

for(int j=0;j<col;j++){

int keyIndex=Character.getNumericValue((key.charAt(j)));

encodedText.append(matrix[i][keyIndex-1]); }

System.out.println("Cipher text is:"+ encodedText); }

public void secondMatrix(){

for(int i=0;i<row;i++) {

int a=i*col;

for(int j=0;j<col;j++){

matrix[i][j]=encodedText.charAt(a);

a++; }

for(int i=0;i<row;i++) {

for(int j=0;j<col;j++){

System.out.print(matrix[i][j]);

System.out.print(" ");

TYCS 13
BNN COLLEGE, BHIWANDI INS

System.out.println("");}

public void decryptText(){

for(int i=0;i<row;i++) {

for(int j=0;j<col;j++){

int keyIndex=Character.getNumericValue((reKey.charAt(j)));

decodedText.append(matrix[i][keyIndex-1]); }

System.out.println("Plain Text is:"+ decodedText); }

public static void main(String[] args) {

SimpleColumnar sc = new SimpleColumnar();

sc.prepareMatrix();

sc.encryptText();

sc.prepareReKey();

sc.secondMatrix();

sc.decryptText(); }

OUTPUT:

TYCS 14
BNN COLLEGE, BHIWANDI INS

PRACTICAL-4
1.DES Algorithm

Code:

package des;

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");

SecretKey sk=kg.generateKey();

Cipher cipher=Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, sk);

String inputText=JOptionPane.showInputDialog("Give Input");

byte[] encrypted=cipher.doFinal(inputText.getBytes());

System.out.println(encrypted);

cipher.init(Cipher.DECRYPT_MODE, sk);

byte[] decrypted=cipher.doFinal(encrypted);

JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted:"+ne
w String(encrypted)+"\n"+"decrypted:"+new String(decrypted));

System.exit(0);

TYCS 15
BNN COLLEGE, BHIWANDI INS

OUTPUT:

TYCS 16
BNN COLLEGE, BHIWANDI INS

2.AES Algorithm

Code:

package aes;

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 cipher=Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, sk);

String inputText=JOptionPane.showInputDialog("Give Input");

byte[] encrypted=cipher.doFinal(inputText.getBytes());

System.out.println(encrypted);

cipher.init(Cipher.DECRYPT_MODE, sk);

byte[] decrypted=cipher.doFinal(encrypted);

JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted:"+ne
w String(encrypted)+"\n"+"decrypted:"+new String(decrypted));

System.exit(0);

TYCS 17
BNN COLLEGE, BHIWANDI INS

OUTPUT:

TYCS 18
BNN COLLEGE, BHIWANDI INS

PRACTICAL-5
Code RSA:

package rsa;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.math.BigInteger;

import java.util.Random;

public class RSA {

public RSA(BigInteger PT)

BigInteger p;

BigInteger q;

BigInteger N;

BigInteger phi;

BigInteger e;

BigInteger d;

int len=32;

Random r=new Random();

p=BigInteger.probablePrime(len, r);

q=BigInteger.probablePrime(len, r);

N=p.multiply(q);

phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));

e=BigInteger.probablePrime(len, r);

while(phi.gcd(e).compareTo(BigInteger.ONE)>0 && e.compareTo(phi)<0)

TYCS 19
BNN COLLEGE, BHIWANDI INS

e.add(BigInteger.ONE);

d=e.modInverse(phi);

BigInteger CT=PT.modPow(e, N);

System.out.println("The Cipher Text:"+CT);

BigInteger PT1=CT.modPow(d, N);

System.out.println("The Plain text:"+PT1);

public static void main(String args[]) throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number to encrypt:");

BigInteger P=new BigInteger(br.readLine());

RSA rsa=new RSA(P);

OUTPUT:

TYCS 20
BNN COLLEGE, BHIWANDI INS

PRACTICAL-6
Code Deffie-Hellman:

package defhell;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.math.BigInteger;

public class Defhell

public static void main(String[] args)throws Exception

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter prime no P:");

BigInteger p=new BigInteger(br.readLine());

System.out.println("Enter primitive root of"+p+"(g)");

BigInteger g=new BigInteger(br.readLine());

System.out.println("Enter value of x less than"+p+":");

BigInteger x=new BigInteger(br.readLine());

BigInteger r1=g.modPow(x,p);

System.out.println("R1:"+r1);

System.out.println("Enter value of y less than"+p+":");

BigInteger y=new BigInteger(br.readLine());

BigInteger r2=g.modPow(y, p);

System.out.println("R2:"+r2);

BigInteger k1=r2.modPow(x, p);

System.out.println("Key calculated for A:"+k1);

TYCS 21
BNN COLLEGE, BHIWANDI INS

BigInteger k2=r1.modPow(y, p);

System.out.println("Key calculated for B:"+k2);

OUTPUT:

TYCS 22
BNN COLLEGE, BHIWANDI INS

PRACTICAL-7
Code MD5:

package md5;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.math.BigInteger;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5

public static void main(String[] args)throws IOException

System.out.println("Enter the text:");

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

String t=br.readLine();

System.out.println("The Hash Code for"+t+"is:"+MD5(t));

public static String MD5(String input)

String MD5=null;

if(null==input)

return null;

try

TYCS 23
BNN COLLEGE, BHIWANDI INS

MessageDigest digest=MessageDigest.getInstance("MD5");

digest.update(input.getBytes(),0,input.length());

MD5=new BigInteger(1,digest.digest()).toString(16);

catch(NoSuchAlgorithmException e)

e.printStackTrace();

return MD5;

OUTPUT:

TYCS 24
BNN COLLEGE, BHIWANDI INS

PRACTICAL-8
Code HMAC-SHA1:

package hmac;

import java.io.UnsupportedEncodingException;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

public class hmac {

public static void main(String[] args)throws Exception {

System.out.println(hmacDigest("The quick brown fox jumps over the lazy


dog","key","HmacSHA1"));

public static String hmacDigest(String msg, String keyString, String algo) throws
UnsupportedEncodingException, NoSuchAlgorithmException {

String digest;

digest = null;

try

SecretKeySpec key=new SecretKeySpec((keyString).getBytes("UTF-8"),algo);

Mac mac=Mac.getInstance(algo);

mac.init(key);

byte[] bytes=mac.doFinal(msg.getBytes("ASCII"));

StringBuffer hash=new StringBuffer();

for(int i=0;i<bytes.length;i++){

String hex=Integer.toHexString(0xFF&bytes[i]);

TYCS 25
BNN COLLEGE, BHIWANDI INS

if(hex.length()==1){

hash.append('0');

hash.append(hex);

digest=hash.toString();

catch(InvalidKeyException e){

return digest;

OUTPUT:

TYCS 26

You might also like