Chandrakant H
Chandrakant H
AIM:
To encrypt and decrypt the given message by using Ceaser Cipher encryption algorithm.
ALGORITHM:
STEP 1: In Ceaser Cipher each letter in the plaintext is replaced by a letter some fixed number of
positions down the alphabet.
STEP 2: For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
STEP 3: The encryption can also be represented using modular arithmetic by first transforming the
letters into numbers, according to the scheme, A = 0, B = 1, Z = 25.
STEP 4: Encryption of a letter x by a shift n can be described mathematically as,
En(x) = (x + n) mod26
STEP 5: Decryption is performed similarly,
Dn (x)=(x - n) mod26
PROGRAM CODING:
class caesarCipher {
public static String encode(String enc, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : enc.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + offset) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + offset) % 26));
}
} else {
encoded.append(i);
}
}
return encoded.toString();
}
OUTPUT:
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
NAME: CHANDRAKANTH G REG NO: 710719104023
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the program for ceaser cipher encryption and decryption algorithm has been implemented and the
output verified successfully.
Ex. No : 1(b)
PLAYFAIR CIPHER
Date :
AIM:
To implement a program to encrypt a plain text and decrypt a cipher text using play fair Cipher
substitution technique.
ALGORITHM:
STEP 1: To encrypt a message, one would break the message into digrams (groups of 2 letters)
STEP 2: For example, "HelloWorld" becomes "HE LL OW OR LD".
STEP 3: These diagrams will be substituted using the key table.
STEP 4: Since encryption requires pairs of letters, messages with an odd number of characters usually
append an uncommon letter, such as "X", to complete the final digram.
STEP 5: The two letters of the diagram are considered opposite corners of a rectangle in the key table.
To perform the substitution, apply the 4 rules, in order, to each pair of letters in the plaintext.
PROGRAM CODING:
import java.awt.Point;
class playfairCipher {
private static char[][] charTable;
private static Point[] positions;
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the program for playfair cipher encryption and decryption algorithm has been implemented and the
output verified successfully.
Ex. No : 1(c)
HILL CIPHER
Date:
AIM:
To implement a program to encrypt and decrypt using the Hill cipher substitution technique
ALGORITHM:
STEP 1: In the Hill cipher Each letter is represented by a number modulo 26.
STEP 2: To encrypt a message, each block of n letters is multiplied by an invertible n x n matrix, again
modulus 26.
STEP 3: To decrypt the message, each block is multiplied by the inverse of the matrix used for
encryption.
NAME: CHANDRAKANTH G REG NO: 710719104023
STEP 4: The matrix used for encryption is the cipher key, and it should be chosen randomly from the
set of invertible n × n matrices (modulo 26).
STEP 5: The cipher can, be adapted to an alphabet with any number of letters.
STEP 6: All arithmetic just needs to be done modulo the number of letters instead of modulo 26.
PROGRAM:
class hillCipher {
/* 3x3 key matrix for 3 characters at once */
public static int[][] keymat = new int[][] { { 1, 2, 1 }, { 2, 3, 2 },
{ 2, 2, 1 } }; /* key inverse matrix */
public static int[][] invkeymat = new int[][] { { -1, 0, 1 }, { 2, -1, 0 }, { -2, 2, -1 } };
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the program for hill cipher encryption and decryption algorithm has been implemented and the
output verified successfully.
NAME: CHANDRAKANTH G REG NO: 710719104023
Ex. No : 1(d)
VIGENERE CIPHER
Date:
AIM:
To implement a program for encryption and decryption using vigenere cipher substitution
technique
ALGORITHM:
STEP 1: The Vigenere cipher is a method of encrypting alphabetic text by using a series of different
Caesar ciphers based on the letters of a keyword.
STEP 2: It is a simple form of polyalphabetic substitution.
STEP 3: To encrypt, a table of alphabets can be used, termed a Vigenere square, or Vigenere table.
STEP 4: It consists of the alphabet written out 26 times in different rows, each alphabet shifted
cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar ciphers.
STEP 5: At different points in the encryption process, the cipher uses a different alphabet from one of
the rows used.
STEP 6: The alphabet at each point depends on a repeating keyword.
PROGRAM CODING:
public class vigenereCipher {
static String encode(String text, final String key) {
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') {
continue;
}
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
j = ++j % key.length();
}
return res;
}
OUTPUT:
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
NAME: CHANDRAKANTH G REG NO: 710719104023
RESULT:
Thus the program for vigenere cipher encryption and decryption algorithm has been implemented and
the output verified successfully.
Ex. No : 2(a)
RAIL FENCE CIPHER TRANSPOSITION TECHNIQUE
Date:
AIM:
To implement a program for encryption and decryption using rail fence transposition technique.
ALGORITHM:
STEP 1:In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails"
of an imaginary fence, then moving up when we reach the bottom rail.
STEP 2:When we reach the top rail, the message is written downwards again until the whole plaintext is
written out.
STEP 3:The message is then read off in rows.
PROGRAM CODING:
class railfenceCipherHelper
{
int depth;
String encode(String msg, int depth) throws Exception {
int r = depth;
int l = msg.length();
int c = l / depth;
int k = 0;
char mat[][] = new char[r][c];
String enc = "";
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
if (k != l) {
mat[j][i] = msg.charAt(k++);
} else {
mat[j][i] = 'X';
NAME: CHANDRAKANTH G REG NO: 710719104023
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
enc += mat[i][j];
}
}
return enc;
}
class railFenceCipher {
public static void main(String[] args) throws java.lang.Exception {
railfenceCipherHelper rf = new railfenceCipherHelper();
String msg, enc, dec;
msg = "Anna University, Chennai";
int depth = 2;
enc = rf.encode(msg, depth);
dec = rf.decode(enc, depth);
System.out.println("Simulating Railfence Cipher\n-------------------------");
System.out.println("Input Message : " + msg);
System.out.println("Encrypted Message : " + enc);
System.out.printf("Decrypted Message : " + dec);
}
}
NAME: CHANDRAKANTH G REG NO: 710719104023
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the java program for Rail Fence Transposition Technique has been implemented and the output
verified successfully.
Ex. No : 2(b)
ROW AND COLUMN TRANSFORMATION TECHNIQUE
Date:
AIM:
To implement a program for encryption and decryption by using row and column transformation
technique.
ALGORITHM:
STEP 1: Consider the plain text hello world, and let us apply the simple columnar transposition
technique as shown below
h e l l
o w o r
l d
NAME: CHANDRAKANTH G REG NO: 710719104023
STEP 2: The plain text characters are placed horizontally and the cipher text is created with vertical
format as: holewdlo lr.
STEP 3: Now, the receiver has to use the same table to decrypt the cipher text to plain text.
PROGRAM CODING:
import java.util.*;
class TransCipher {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the plain text”);
String pl = sc.nextLine();
sc.close();
String s = “”;
int start = 0;
for (int I = 0; I < pl.length(); i++) {
if (pl.charAt(i) == ‘ ‘) {
s = s + pl.substring(start, i);
start = I + 1;
}
}
s = s + pl.substring(start);
System.out.print(s);
System.out.println();
// end of space deletion
int k = s.length();
int l = 0;
int col = 4;
int row = s.length() / col;
char ch[][] = new char[row][col];
for (int I = 0; I < row; i++) {
for (int j = 0; j < col; j++) {
if (l < k) {
ch[i][j] = s.charAt(l);
l++;
} else {
ch[i][j] = ‘#’;
}
}
}
NAME: CHANDRAKANTH G REG NO: 710719104023
// arranged in matrix
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the java program for Row and Column Transposition Technique has been implemented and the
output verified successfully.
Ex. No : 3
DATA ENCRYPTION STANDARD (DES) ALGORITHM
Date:
NAME: CHANDRAKANTH G REG NO: 710719104023
AIM:
To use Data Encryption Standard (DES) Algorithm for a practical application like User Message
Encryption.
ALGORITHM:
STEP 1: Create a DES Key.
STEP 2: Create a Cipher instance from Cipher class, specify the following information and separated by
a slash (/).
a. Algorithm name
b. Mode (optional)
c. Padding scheme (optional)
STEP 3: Convert String into Byte[] array format.
STEP 4: Make Cipher in encrypt mode, and encrypt it with Cipher.doFinal() method.
STEP 5: Make Cipher in decrypt mode, and decrypt it with Cipher.doFinal() method.
PROGRAM CODING:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
try{
System.out.println(“Message Encryption Using DES Algorithm\n-------");
KeyGenerator keygenerator = KeyGenerator.getInstance(“DES”);
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance(“DES/ECB/PKCS5Padding”);
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] text = “Secret Information “.getBytes();
System.out.println(“Message [Byte Format] : “ + text);
NAME: CHANDRAKANTH G REG NO: 710719104023
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
NAME: CHANDRAKANTH G REG NO: 710719104023
RESULT:
Thus the java program for DES Algorithm has been implemented and the output verified successfully.
AIM:
To use Advanced Encryption Standard (AES) Algorithm for a practical application like URL
Encryption.
ALGORITHM:
STEP 1:AES is based on a design principle known as a substitution–permutation.
STEP 2:AES does not use a Feistel network like DES, it uses variant of Rijndael.
STEP 3:It has a fixed block size of 128 bits, and a key size of 128, 192, or 256 bits.
STEP 4:AES operates on a 4 × 4 column-major order array of bytes, termed the state
PROGRAM CODING:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
OUTPUT:
DEPT OF CSE
NAME: CHANDRAKANTH G REG NO: 710719104023
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the java program for AES Algorithm has been implemented for URL Encryption and the output
verified successfully.
Ex. No : 5
RSA ALGORITHM
Date :
AIM:
To implement RSA (Rivest–Shamir–Adleman) algorithm by using HTML and Javascript.
ALGORITHM:
STEP 1:Choose two prime number p and q
STEP 2:Compute the value of n and p
STEP 3:Find the value of e (public key)
STEP 4:Compute the value of d (private key) using gcd()
STEP 5:Do the encryption and decryption
a. Encryption is given as,
c = te mod n
b. Decryption is given as,
t = cd mod n
NAME: CHANDRAKANTH G REG NO: 710719104023
PROGRAM CODING:
rsa.html
<html>
<head>
<title>RSA Encryption</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>RSA Algorithm</h1>
<h2>Implemented Using HTML & Javascript</h2>
<hr>
<table>
<tr>
<td>Enter First Prime Number:</td>
<td><input type="number" value="53" id="p"></td>
</tr>
<tr>
<td>Enter Second Prime Number:</td>
<td><input type="number" value="59" id="q"></p>
</td>
</tr>
<tr>
<td>Enter the Message(cipher text):<br>[A=1, B=2,...]</td>
<td><input type="number" value="89" id="msg"></p>
</td>
</tr>
<tr>
<td>Public Key:</td>
<td>
<p id="publickey"></p>
</td>
</tr>
<tr>
<td>Exponent:</td>
<td>
<p id="exponent"></p>
</td>
</tr>
<tr>
<td>Private Key:</td>
<td>
<p id="privatekey"></p>
</td>
NAME: CHANDRAKANTH G REG NO: 710719104023
</tr>
<tr>
<td>Cipher Text:</td>
<td>
<p id="ciphertext"></p>
</td>
</tr>
<tr>
<td><button onclick="RSA();">Apply RSA</button></td>
</tr>
</table>
</center>
</body>
<script type="text/javascript">
function RSA() {
var gcd, p, q, no, n, t, e, i, x;
gcd = function (a, b) { return (!b) ? a : gcd(b, a % b); };
p = document.getElementById('p').value;
q = document.getElementById('q').value;
no = document.getElementById('msg').value;
n = p * q;
t = (p - 1) * (q - 1);
document.getElementById('publickey').innerHTML = n;
document.getElementById('exponent').innerHTML = e;
document.getElementById('privatekey').innerHTML = d;
NAME: CHANDRAKANTH G REG NO: 710719104023
document.getElementById('ciphertext').innerHTML = ct;
}
</script>
</html>
OUTPUT:
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
NAME: CHANDRAKANTH G REG NO: 710719104023
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
RESULT:
Thus the RSA algorithm has been implemented using HTML & CSS and the output has been verified
successfully.
Ex. No : 6
DIFFIE-HELLMAN KEY EXCHANGE ALGORITHM
Date:
NAME: CHANDRAKANTH G REG NO: 710719104023
AIM:
To implement the Diffie-Hellman Key Exchange algorithm for a given problem .
ALGORITHM:
STEP 1: Alice and Bob publicly agree to use a modulus p = 23 and base g = 5 (which is a primitive root
modulo 23).
o A = 54 mod 23 = 4
o B = 53 mod 23 = 10
o s = 104 mod 23 = 18
o s = 43 mod 23 = 18
STEP 6:Alice and Bob now share a secret (the number 18).
PROGRAM CODING:
class DiffieHellman {
public static void main (String args[]) {
int p = 23; /* publicly known (prime number) */
int g = 5; /* publicly known (primitive root) */
int x = 4; /* only Alice knows this secret */
int y = 3; /* only Bob knows this secret */
double aliceSends = (Math.pow(g, x)) % p;
double bobComputes = (Math.pow(aliceSends, y)) % p;
double bobSends = (Math.pow(g, y)) % p;
double aliceComputes = (Math.pow(bobSends, x)) % p;
double sharedSecret = (Math.pow(g, (x * y))) % p;
System.out.println("simulation of Diffie-Hellman key exchange algorithm\
n---------------------------------------------");
System.out.println("Alice Sends : " + aliceSends);
System.out.println("Bob Computes : " + bobComputes);
System.out.println("Bob Sends : " + bobSends);
System.out.println("Alice Computes : " + aliceComputes);
NAME: CHANDRAKANTH G REG NO: 710719104023
OUTPUT:
NAME: CHANDRAKANTH G REG NO: 710719104023
DEPT OF CSE
PREVIVA (5)
PROGRAM (30)
POSTVIVA (5)
TOTAL (100)
FACULTY SIGN
NAME: CHANDRAKANTH G REG NO: 710719104023
RESULT:
Thus the Diffie-Hellman key exchange algorithm has been implemented using Java Program and the
output has been verified successfully.