0% found this document useful (0 votes)
9 views8 pages

Network Record

The document contains implementations of four different encryption algorithms: Caesar Cipher, Playfair Cipher, Hill Cipher, and Columnar Transposition Cipher. Each section includes a Java program that demonstrates how to encrypt and decrypt messages using the respective cipher, along with sample outputs. The programs utilize various techniques such as matrix manipulation and character shifting to achieve encryption and decryption.

Uploaded by

20bel513
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)
9 views8 pages

Network Record

The document contains implementations of four different encryption algorithms: Caesar Cipher, Playfair Cipher, Hill Cipher, and Columnar Transposition Cipher. Each section includes a Java program that demonstrates how to encrypt and decrypt messages using the respective cipher, along with sample outputs. The programs utilize various techniques such as matrix manipulation and character shifting to achieve encryption and decryption.

Uploaded by

20bel513
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/ 8

EX.

NO:1 CEASAR CIPHER

PROGRAM
import java.util.Scanner;
public class CaesarCipher {
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) ((c - base + shift) % 26 + base));
} else {
result.append(c);
}
}
return result.toString();
}

public static String decrypt(String text, int shift) {


return encrypt(text, 26 - shift);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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


String text = scanner.nextLine();
System.out.println("Enter the key:");
int shift = scanner.nextInt();
String encryptedText = encrypt(text, shift);
System.out.println("Cipher text: " + encryptedText);
String decryptedText = decrypt(encryptedText, shift);
System.out.println("Decrypted text: " + decryptedText);
scanner.close();
}
}

OUTPUT
Enter the Plain text: plain
Enter the key: 4
Cipher text: tpemr
Decrypted text: plain
EX.NO:2 PLAYFAIR CIPHER

PROGRAM
import java.util.Scanner;
import java.util.HashSet;
public class PlayfairCipher {
public static char[][] generatePlayfairMatrix(String keyword) {
keyword = keyword.toUpperCase().replace('J', 'I').replace(" ", "");
HashSet<Character> used = new HashSet<>();
char[] matrix = new char[25];
int index = 0;
for (char ch : keyword.toCharArray()) {
if (!used.contains(ch)) {
matrix[index++] = ch;
used.add(ch);
}
}
for (char ch = 'A'; ch <= 'Z'; ch++) {
if (ch == 'J') continue;
if (!used.contains(ch)) {
matrix[index++] = ch;
used.add(ch);
}
}

char[][] playfairMatrix = new char[5][5];


index = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
playfairMatrix[i][j] = matrix[index++];
}
}

return playfairMatrix;
}

public static void printMatrix(char[][] matrix) {


System.out.println("Playfair Cipher Matrix:");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}

public static String preparePlaintext(String plaintext) {


plaintext = plaintext.toUpperCase().replace('J', 'I').replace(" ",
"");
StringBuilder preparedText = new StringBuilder();

int i = 0;
while (i < plaintext.length()) {
char a = plaintext.charAt(i);
char b = (i + 1 < plaintext.length()) ? plaintext.charAt(i + 1)
: 'Z';
if (a == b) {
preparedText.append(a).append('Z');
i++;
} else {
preparedText.append(a).append(b);
i += 2;
}
}
if (preparedText.length() % 2 != 0) {
preparedText.append('Z');
}

return preparedText.toString();
}

public static int[] findPosition(char[][] matrix, char ch) {


for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (matrix[row][col] == ch) {
return new int[]{row, col};
}
}
}
return null;
}

public static String encrypt(String plaintext, char[][] matrix) {


StringBuilder ciphertext = new StringBuilder();

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


char a = plaintext.charAt(i);
char b = plaintext.charAt(i + 1);

int[] posA = findPosition(matrix, a);


int[] posB = findPosition(matrix, b);

if (posA[0] == posB[0]) {
ciphertext.append(matrix[posA[0]][(posA[1] + 1) % 5]);
ciphertext.append(matrix[posB[0]][(posB[1] + 1) % 5]);
} else if (posA[1] == posB[1]) {
ciphertext.append(matrix[(posA[0] + 1) % 5][posA[1]]);
ciphertext.append(matrix[(posB[0] + 1) % 5][posB[1]]);
} else {
ciphertext.append(matrix[posA[0]][posB[1]]);
ciphertext.append(matrix[posB[0]][posA[1]]);
}
}

return ciphertext.toString();
}

public static String decrypt(String ciphertext, char[][] matrix) {


StringBuilder plaintext = new StringBuilder();

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


char a = ciphertext.charAt(i);
char b = ciphertext.charAt(i + 1);

int[] posA = findPosition(matrix, a);


int[] posB = findPosition(matrix, b);
if (posA[0] == posB[0]) {
plaintext.append(matrix[posA[0]][(posA[1] - 1 + 5) % 5]);
plaintext.append(matrix[posB[0]][(posB[1] - 1 + 5) % 5]);
} else if (posA[1] == posB[1]) {
plaintext.append(matrix[(posA[0] - 1 + 5) % 5][posA[1]]);
plaintext.append(matrix[(posB[0] - 1 + 5) % 5][posB[1]]);
} else {
plaintext.append(matrix[posA[0]][posB[1]]);
plaintext.append(matrix[posB[0]][posA[1]]);
}
}

return plaintext.toString();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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


String keyword = scanner.nextLine();
char[][] matrix = generatePlayfairMatrix(keyword);

System.out.print("Enter the plaintext: ");


String plaintext = scanner.nextLine();
plaintext = preparePlaintext(plaintext);
System.out.println("Prepared plaintext: " + plaintext);

String ciphertext = encrypt(plaintext, matrix);


System.out.println("Cipher text: " + ciphertext);

String decryptedText = decrypt(ciphertext, matrix);


System.out.println("Decrypted message: " + decryptedText);

scanner.close();
}
}

OUTPUT
Enter a keyword: hello
Enter the plaintext: world
Prepared plaintext: WORLDZ
Cipher text: YESEGX
Decrypted message: WORLDZ
EX.NO:3 HILL CIPHER

PROGRAM
import java.util.Scanner;

public class HillCipher {


private static int[][] keyMatrix;
private static int[] messageVector;
private static int[] cipherMatrix;
private static int[][] inverseKeyMatrix;

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the key matrix :");
int n = sc.nextInt();
keyMatrix = new int[n][n];

System.out.println("Enter the " + n + "x" + n + " key matrix:");


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
keyMatrix[i][j] = sc.nextInt();
}
}

public static void encrypt(String message, int n) {


int length = message.length();
cipherMatrix = new int[length];

for (int k = 0; k < length; k += n) {


messageVector = new int[n];
for (int i = 0; i < n; i++) {
messageVector[i] = message.charAt(k + i) - 'A';
}

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


cipherMatrix[k + i] = 0;
for (int j = 0; j < n; j++) {
cipherMatrix[k + i] += keyMatrix[i][j] *
messageVector[j];
}
cipherMatrix[k + i] %= 26;
}
}

System.out.print("Encrypted Message: ");


for (int i = 0; i < length; i++) {
System.out.print((char) (cipherMatrix[i] + 'A'));
}
System.out.println();
}

public static void decrypt(int n) {


findInverseKeyMatrix(n);
int length = cipherMatrix.length;
int[] decryptedMatrix = new int[length];

System.out.print("Decrypted Message: ");


for (int i = 0; i < length; i++) {
System.out.print((char) (decryptedMatrix[i] + 'A'));
}
System.out.println();
}

public static void findInverseKeyMatrix(int n) {


inverseKeyMatrix = new int[n][n];
int determinant = 0;

if (n == 2) {
determinant = (keyMatrix[0][0] * keyMatrix[1][1]) -
(keyMatrix[0][1] * keyMatrix[1][0]);
}
determinant %= 26;
if (determinant < 0) {
determinant += 26;
}

int inverseDeterminant = -1;


for (int i = 0; i < 26; i++) {
if ((determinant * i) % 26 == 1) {
inverseDeterminant = i;
break;
}
}

if (n == 2) {
inverseKeyMatrix[0][0] = keyMatrix[1][1] * inverseDeterminant
% 26;
inverseKeyMatrix[1][1] = keyMatrix[0][0] * inverseDeterminant
% 26;
inverseKeyMatrix[0][1] = (26 - keyMatrix[0][1]) *
inverseDeterminant % 26;
inverseKeyMatrix[1][0] = (26 - keyMatrix[1][0]) *
inverseDeterminant % 26;
}
}
}

OUTPUT
EX.NO:4 COLUMNAR TRANSPOSITION CIPHER

PROGRAM
import java.util.Arrays;
import java.util.Scanner;

public class ColumnarTranspositionCipher {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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


String key = sc.nextLine();

System.out.print("Enter the message: ");


String message = sc.nextLine().toUpperCase().replaceAll("\\s",
"");

while (message.length() % key.length() != 0) {


message += 'X';
}

String encryptedMessage = encrypt(message, key);


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

String decryptedMessage = decrypt(encryptedMessage, key);


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

public static String encrypt(String message, String key) {


int[] keyOrder = getKeyOrder(key);
int columns = key.length();
int rows = message.length() / columns;
char[][] grid = new char[rows][columns];

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


grid[i / columns][i % columns] = message.charAt(i);
}

StringBuilder encryptedMessage = new StringBuilder();


for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
encryptedMessage.append(grid[j][keyOrder[i]]);
}
}

return encryptedMessage.toString();
}

public static String decrypt(String message, String key) {


int[] keyOrder = getKeyOrder(key);
int columns = key.length();
int rows = message.length() / columns;
char[][] grid = new char[rows][columns];

int index = 0;
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
grid[j][keyOrder[i]] = message.charAt(index++);
}
}

StringBuilder decryptedMessage = new StringBuilder();


for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
decryptedMessage.append(grid[i][j]);
}
}
while (decryptedMessage.length() > 0 &&
decryptedMessage.charAt(decryptedMessage.length() - 1) == 'X') {
decryptedMessage.setLength(decryptedMessage.length() - 1);
}

return decryptedMessage.toString();
}

public static int[] getKeyOrder(String key) {


int length = key.length();
int[] order = new int[length];
Character[] keyArray = new Character[length];

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


keyArray[i] = key.charAt(i);
}

Character[] sortedKeyArray = keyArray.clone();


Arrays.sort(sortedKeyArray);

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


order[i] =
Arrays.asList(sortedKeyArray).indexOf(keyArray[i]);
}

return order;
}
}

OUTPUT

Enter the key: EQUAL


Enter the message: WORLD
Encrypted Message: OLDWR
Decrypted Message: WORLD

You might also like