0% found this document useful (0 votes)
6 views14 pages

1 Exp

The document provides implementations of four classical encryption techniques: Caesar Cipher, Playfair Cipher, Hill Cipher, and Vigenère Cipher in Java. Each cipher includes methods for encryption and decryption, with user input for plaintext and keys. The code demonstrates how to handle character manipulation and maintain the integrity of non-alphabet characters during the encryption process.
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)
6 views14 pages

1 Exp

The document provides implementations of four classical encryption techniques: Caesar Cipher, Playfair Cipher, Hill Cipher, and Vigenère Cipher in Java. Each cipher includes methods for encryption and decryption, with user input for plaintext and keys. The code demonstrates how to handle character manipulation and maintain the integrity of non-alphabet characters during the encryption process.
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/ 14

1.

Implement the following SUBSTITUTION and TRANSPOSITION


TECHNIQUES concepts

a) Caesar Cipher
import java.u l.Scanner;

public class CaesarCipher {

public sta c final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

public sta c String encrypt(String plainText, int shi Key) {

plainText = plainText.toLowerCase();

String cipherText = "";

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

char currentChar = plainText.charAt(i);

int charPosi on = ALPHABET.indexOf(currentChar);

if (charPosi on != -1) { // Check if the character is in the alphabet

int keyVal = (shi Key + charPosi on) % 26;

char replaceVal = ALPHABET.charAt(keyVal);

cipherText += replaceVal;

} else {

cipherText += currentChar; // Keep non-alphabet characters unchanged

return cipherText;

public sta c String decrypt(String cipherText, int shi Key) {

cipherText = cipherText.toLowerCase();

String plainText = "";

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

char currentChar = cipherText.charAt(i);

int charPosi on = ALPHABET.indexOf(currentChar);

if (charPosi on != -1) { // Check if the character is in the alphabet

int keyVal = (charPosi on - shi Key) % 26;

if (keyVal < 0) {

keyVal += ALPHABET.length(); // Handle nega ve key value


}

char replaceVal = ALPHABET.charAt(keyVal);

plainText += replaceVal;

} else {

plainText += currentChar; // Keep non-alphabet characters unchanged

return plainText;

public sta c void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Plain text for Encryp on: ");

String message = sc.nextLine(); // Use nextLine() to capture spaces

int shi Key = 3; // You can make this dynamic if needed

String encryptedMessage = encrypt(message, shi Key);

System.out.println("Encrypted message: Cipher Text = " + encryptedMessage);

String decryptedMessage = decrypt(encryptedMessage, shi Key);

System.out.println("Decrypted message: Plain Text = " + decryptedMessage);

sc.close();

Output:
b) Playfair Cipher
import java.util.Scanner;

public class Playfair {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter keyword: ");

String key = in.nextLine();

System.out.print("Enter message to encrypt: ");

String msg = in.nextLine();

PFEncryption pfEncryption = new PFEncryption();

pfEncryption.makeArray(key);

System.out.println("\nPlayfair Cipher Grid:");

pfEncryption.displayGrid();

msg = pfEncryption.manageMessage(msg);

System.out.println("Encrypting ... ");

pfEncryption.doPlayFair(msg, "Encrypt");

String encryptedText = pfEncryption.getEncrypted();

System.out.println("The encrypted text is: " + encryptedText);

System.out.println("==================================");

System.out.println("\nDecrypting ... ");

pfEncryption.doPlayFair(encryptedText, "Decrypt");

System.out.println("The decrypted text is: " + pfEncryption.getDecrypted());

class PFEncryption {

private char[][] alphabets = new char[5][5];

private char[] uniqueChar = new char[26];


private String ch = "ABCDEFGHIKLMNOPQRSTUVWXYZ";

private String encrypted = "";

private String decrypted = "";

void makeArray(String keyword) {

keyword = keyword.toUpperCase().replace("J", "I");

boolean present;

int val = 0;

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

present = false;

char currentChar = keyword.charAt(i);

if (currentChar != ' ') {

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

if (uniqueChar[j] == currentChar) {

present = true;

break;

if (!present && val < uniqueChar.length) {

uniqueChar[val] = currentChar;

val++;

StringBuilder remainingChars = new StringBuilder();

for (char c : ch.toCharArray()) {

boolean isPresent = false;

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


if (uniqueChar[j] == c) {

isPresent = true;

break;

if (!isPresent) {

remainingChars.append(c);

for (int i = 0; i < remainingChars.length() && val < uniqueChar.length; i++) {

uniqueChar[val] = remainingChars.charAt(i);

val++;

val = 0;

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

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

alphabets[i][j] = uniqueChar[val];

val++;

void displayGrid() {

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

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

System.out.print(alphabets[i][j] + "\t");

System.out.println();
}

String manageMessage(String msg) {

msg = msg.toUpperCase().replaceAll("J", "I").replaceAll(" ", "");

StringBuilder newTxt = new StringBuilder();

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

if (i + 1 < msg.length() && msg.charAt(i) == msg.charAt(i + 1)) {

newTxt.append(msg.charAt(i)).append('X');

i--;

} else {

newTxt.append(msg.charAt(i));

if (i + 1 < msg.length()) {

newTxt.append(msg.charAt(i + 1));

if (newTxt.length() % 2 != 0) {

newTxt.append('X');

return newTxt.toString();

void doPlayFair(String msg, String tag) {

if (tag.equals("Encrypt")) encrypted = ""; // Reset encrypted string for fresh encryption

else decrypted = ""; // Reset decrypted string for fresh decryption

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

searchAndEncryptOrDecrypt(msg.substring(i, i + 2), tag);


}

void searchAndEncryptOrDecrypt(String doublyCh, String tag) {

char ch1 = doublyCh.charAt(0);

char ch2 = doublyCh.charAt(1);

int row1 = 0, col1 = 0, row2 = 0, col2 = 0;

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

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

if (alphabets[i][j] == ch1) {

row1 = i;

col1 = j;

} else if (alphabets[i][j] == ch2) {

row2 = i;

col2 = j;

if (tag.equals("Encrypt")) {

encrypt(row1, col1, row2, col2);

} else if (tag.equals("Decrypt")) {

decrypt(row1, col1, row2, col2);

void encrypt(int row1, int col1, int row2, int col2) {

if (row1 == row2) {

col1 = (col1 + 1) % 5;

col2 = (col2 + 1) % 5;

encrypted += Character.toString(alphabets[row1][col1]) + alphabets[row1][col2];


} } else if (col1 == col2) {
row1 = (row1 + 1) % 5;

row2 = (row2 + 1) % 5;

encrypted += Character.toString(alphabets[row1][col1]) + alphabets[row2][col1];

} else {

encrypted += Character.toString(alphabets[row1][col2]) + alphabets[row2][col1];

void decrypt(int row1, int col1, int row2, int col2) {

if (row1 == row2) {

col1 = (col1 - 1 + 5) % 5;

col2 = (col2 - 1 + 5) % 5;

decrypted += Character.toString(alphabets[row1][col1]) + alphabets[row1][col2];

} else if (col1 == col2) {

row1 = (row1 - 1 + 5) % 5;

row2 = (row2 - 1 + 5) % 5;

decrypted += Character.toString(alphabets[row1][col1]) + alphabets[row2][col1];

} else {

decrypted += Character.toString(alphabets[row1][col2]) + alphabets[row2][col1];

String getEncrypted() {

return encrypted;

String getDecrypted() {

return decrypted;
}
}
c) Hill Cipher
import java.util.Scanner;

import javax.swing.JOptionPane;

public class hillcipher {

public static int[][] keymat = new int[][] {

{ 1, 2, 1 },

{ 2, 3, 2 },

{ 2, 2, 1 }

};

public static int[][] invkeymat = new int[][] {

{ -1, 0, 1 },

{ 2, -1, 0 },

{ -2, 2, -1 }

};

public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static void main(String[] args) {

String text, outtext = "", outtext1 = "";

int ch, n;

Scanner sc = new Scanner(System.in);

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

text = sc.next();

text = text.toUpperCase();

text = text.replaceAll("\\s+", "");

n = text.length() % 3;

if (n != 0) {

for (int i = 1; i <= (3 - n); i++)

text += "X";
}

System.out.println("Padded Text:" + text);

char[] ptextchars1 = text.toCharArray();

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

outtext += encrypt(ptextchars1[i], ptextchars1[i + 1], ptextchars1[i + 2]);

System.out.println("Encrypted Message: " + outtext);

char[] ptextchars2 = outtext.toCharArray();

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

outtext1 += decrypt(ptextchars2[i], ptextchars2[i + 1], ptextchars2[i + 2]);

System.out.println("Decrypted Message: " + outtext1);

private static String encrypt(char a, char b, char c) {

String ret = "";

int x, y, z;

int posa = (int) a - 65;

int posb = (int) b - 65;

int posc = (int) c - 65;

x = posa * keymat[0][0] + posb * keymat[0][1] + posc * keymat[0][2];

y = posa * keymat[1][0] + posb * keymat[1][1] + posc * keymat[1][2];

z = posa * keymat[2][0] + posb * keymat[2][1] + posc * keymat[2][2];

a = key.charAt((x % 26 + 26) % 26);

b = key.charAt((y % 26 + 26) % 26);

c = key.charAt((z % 26 + 26) % 26);

ret = "" + a + b + c;

return ret;
}

private static String decrypt(char a, char b, char c) {

String ret = "";

int x, y, z;

int posa = (int) a - 65;

int posb = (int) b - 65;

int posc = (int) c - 65;

x = posa * invkeymat[0][0] + posb * invkeymat[0][1] + posc * invkeymat[0][2];

y = posa * invkeymat[1][0] + posb * invkeymat[1][1] + posc * invkeymat[1][2];

z = posa * invkeymat[2][0] + posb * invkeymat[2][1] + posc * invkeymat[2][2];

a = key.charAt((x % 26 + 26) % 26);

b = key.charAt((y % 26 + 26) % 26);

c = key.charAt((z % 26 + 26) % 26);

ret = "" + a + b + c;

return ret;

}
d) Vigener Cipher
public class vigenercipher1 {

public static String encrypt(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;

public static String decrypt(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) + 26) % 26 + 'A');

j = ++j % key.length();

return res;

}
public static void main(String[] args) {

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

String key = System.console().readLine();

System.out.println("Enter the message for encryption: ");

String message = System.console().readLine();

String encryptedMsg = encrypt(message, key);

System.out.println("String: " + message);

System.out.println("Encrypted message: Cipher Text=" + encryptedMsg);

System.out.println("Decrypted message: Plain Text=" + decrypt(encryptedMsg, key));

You might also like