0% found this document useful (0 votes)
28 views12 pages

Lab Da1

The document contains Java code implementations of 3 classical cryptography algorithms: 1. Caesar cipher for encryption and decryption of text using a shift key. 2. Playfair cipher for encryption of plaintext into ciphertext using a generated 5x5 matrix based on a keyword. 3. Hill cipher for encryption and decryption of text using matrix multiplication modulo 26, with an example implementation using a 3x3 matrix.
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)
28 views12 pages

Lab Da1

The document contains Java code implementations of 3 classical cryptography algorithms: 1. Caesar cipher for encryption and decryption of text using a shift key. 2. Playfair cipher for encryption of plaintext into ciphertext using a generated 5x5 matrix based on a keyword. 3. Hill cipher for encryption and decryption of text using matrix multiplication modulo 26, with an example implementation using a 3x3 matrix.
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/ 12

CRYPTOGRAPHY AND NETWORK SECURITY LAB

DIGITAL ASSIGNMENT – 01

NAME: HARISH K

REG No: 21BCI0037


1. Ceasar Cipher
Code:

import java.util.Scanner;

public class caesarCipher {

public static String encryption(String plainText, int key) {


String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String encryptedText = "";

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


char currentChar = plainText.charAt(i);
int j = text.indexOf(currentChar);
if (plainText.charAt(i) == text.charAt(j)) {
encryptedText += text.charAt((key + j) % 26);
}
}
return encryptedText;
}

public static String decryption(String encryptedText, int key) {


String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String decryptedText = "";

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


char currentChar = encryptedText.charAt(i);
int j = text.indexOf(currentChar);
if (encryptedText.charAt(i) == text.charAt(j)) {
encryptedText += text.charAt((j - key) % 26);
}
}
return decryptedText;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);

System.out.println("Enter\n1. Encryption\n2. Decryption");


int n = sc.nextInt();

switch (n) {
case 1:
System.out.println("Enter plainText");
String plainText = sc.next().toUpperCase();

System.out.println("Enter key");
int key = sc.nextInt();

String encryptedText = encryption(plainText, key);


System.out.println("Encrypted text: " + encryptedText);

break;
case 2:
System.out.println("Enter encryptedText");
encryptedText = sc.next().toUpperCase();

System.out.println("Enter key");
key = sc.nextInt();

String decryptedText = encryption(encryptedText, key);


System.out.println("Encrypted text: " + decryptedText);

break;
default:
System.out.println("Invalid input");
}
}
}

Input/Output:

2. Play Fair Cipher


Code:

import java.util.Scanner;
import java.util.HashSet;

public class playFairCipher {


private static final String ALPHABET = "abcdefghiklmnopqrstuvwxyz"; // 'j' is omitted
in Playfair Cipher

public void table(String key, String[][] matrix) {


HashSet<Character> usedChars = new HashSet<>();
int rowIndex = 0, colIndex = 0;

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


char currentChar = key.charAt(i);
if (!usedChars.contains(currentChar)) {
matrix[rowIndex][colIndex] = String.valueOf(currentChar);
usedChars.add(currentChar);
colIndex++;

if (colIndex == 5) {
colIndex = 0;
rowIndex++;
}
}
}

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


if (!usedChars.contains(c)) {
matrix[rowIndex][colIndex] = String.valueOf(c);
usedChars.add(c);
colIndex++;

if (colIndex == 5) {
colIndex = 0;
rowIndex++;
}
}
}
}

public String encrypt(String plainText, String[][] matrix) {


StringBuilder encryptedText = new StringBuilder();

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


char firstChar = plainText.charAt(i);
char secondChar = (i + 1 < plainText.length()) ? plainText.charAt(i + 1) :
'x';

int[] firstCharPos = findCharPosition(matrix, firstChar);


int[] secondCharPos = findCharPosition(matrix, secondChar);

if (firstCharPos[0] == secondCharPos[0]) { // Same row


encryptedText.append(matrix[firstCharPos[0]][(firstCharPos[1] + 1) % 5]);
encryptedText.append(matrix[secondCharPos[0]][(secondCharPos[1] + 1) %
5]);
} else if (firstCharPos[1] == secondCharPos[1]) { // Same column
encryptedText.append(matrix[(firstCharPos[0] + 1) % 5][firstCharPos[1]]);
encryptedText.append(matrix[(secondCharPos[0] + 1) %
5][secondCharPos[1]]);
} else { // Different row and column
encryptedText.append(matrix[firstCharPos[0]][secondCharPos[1]]);
encryptedText.append(matrix[secondCharPos[0]][firstCharPos[1]]);
}
}

return encryptedText.toString();
}
private int[] findCharPosition(String[][] matrix, char c) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i][j].charAt(0) == c) {
return new int[]{i, j};
}
}
}
return new int[]{-1, -1};
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

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


String plainText = sc.next().toLowerCase().replaceAll("[^a-z]", "");

// You can add more input validation for the key if needed
System.out.println("Enter key");
String key = sc.next().toLowerCase().replaceAll("[^a-z]", "");

String matrix[][] = new String[5][5];


playFairCipher playfair = new playFairCipher();
playfair.table(key, matrix);

System.out.println("Generated Playfair Matrix:");


for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}

String encryptedText = playfair.encrypt(plainText, matrix);


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

sc.close();
}
}

Input/Output
3. Hill Cipher
Code:

import java.util.Scanner;

public class hillCipher {

private static final int MATRIX_SIZE = 3;

private static int[][] keyMatrix = new int[MATRIX_SIZE][MATRIX_SIZE];

private static void initializeKeyMatrix(String key) {


int k = 0;
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
keyMatrix[i][j] = key.charAt(k) - 'A';
k = (k + 1) % key.length();
}
}
}

public static String encrypt(String message) {


StringBuilder encryptedMessage = new StringBuilder();

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


message += "X";
}

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


String block = message.substring(i, i + MATRIX_SIZE);
int[] blockVector = new int[MATRIX_SIZE];

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


blockVector[j] = block.charAt(j) - 'A';
}

int[] resultVector = multiplyMatrix(keyMatrix, blockVector);

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


encryptedMessage.append((char) (resultVector[j] + 'A'));
}
}
return encryptedMessage.toString();
}

public static String decrypt(String encryptedMessage) {


StringBuilder decryptedMessage = new StringBuilder();

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


String block = encryptedMessage.substring(i, i + MATRIX_SIZE);
int[] blockVector = new int[MATRIX_SIZE];

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


blockVector[j] = block.charAt(j) - 'A';
}

int[][] inverseKeyMatrix = getInverse(keyMatrix);

int[] resultVector = multiplyMatrix(inverseKeyMatrix, blockVector);

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


decryptedMessage.append((char) (resultVector[j] + 'A'));
}
}

return decryptedMessage.toString();
}

private static int[] multiplyMatrix(int[][] matrix, int[] vector) {


int[] result = new int[MATRIX_SIZE];

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


for (int j = 0; j < MATRIX_SIZE; j++) {
result[i] += matrix[i][j] * vector[j];
}
result[i] %= 26;
}

return result;
}

private static int[][] getInverse(int[][] matrix) {


int[][] inverse = new int[MATRIX_SIZE][MATRIX_SIZE];

int det = determinant(matrix);


int detInverse = modInverse(det, 26);

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


for (int j = 0; j < MATRIX_SIZE; j++) {
inverse[i][j] = ((matrix[(j + 1) % MATRIX_SIZE][(i + 1) % MATRIX_SIZE] *
matrix[(j + 2) % MATRIX_SIZE][(i + 2) % MATRIX_SIZE]) -
(matrix[(j + 1) % MATRIX_SIZE][(i + 2) % MATRIX_SIZE] * matrix[(j
+ 2) % MATRIX_SIZE][(i + 1) % MATRIX_SIZE])) % 26;
if (inverse[i][j] < 0) {
inverse[i][j] += 26;
}
}
}

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


for (int j = 0; j < MATRIX_SIZE; j++) {
inverse[i][j] = (inverse[i][j] * detInverse) % 26;
}
}

return inverse;
}

private static int determinant(int[][] matrix) {


return matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2])
- matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] *
matrix[2][0])
+ matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] *
matrix[2][0]);
}

private static int modInverse(int a, int m) {


a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) {
return x;
}
}
return 1;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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


String key = scanner.nextLine().toUpperCase();
initializeKeyMatrix(key);

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


String message = scanner.nextLine().toUpperCase();

String encryptedMessage = encrypt(message);


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

String decryptedMessage = decrypt(encryptedMessage);


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

scanner.close();
}
}

Input/Output:
4. Vigenere Cipher
Code:

import java.util.Scanner;

public class VigenereCipher {

// Encryption method
public static String encrypt(String plainText, String key) {
StringBuilder encryptedText = new StringBuilder();
plainText = plainText.toUpperCase();
key = key.toUpperCase();

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


char c = plainText.charAt(i);
if (Character.isLetter(c)) {
int shift = key.charAt(j) - 'A';
c = (char)('A' + (c - 'A' + shift) % 26);
j = (j + 1) % key.length();
}
encryptedText.append(c);
}

return encryptedText.toString();
}

// Decryption method
public static String decrypt(String encryptedText, String key) {

StringBuilder decryptedText = new StringBuilder();


encryptedText = encryptedText.toUpperCase();
key = key.toUpperCase();

for (int i = 0, j = 0; i < encryptedText.length(); i++) {


char c = encryptedText.charAt(i);
if (Character.isLetter(c)) {
int shift = key.charAt(j) - 'A';
c = (char)('A' + (c - 'A' - shift + 26) % 26);
j = (j + 1) % key.length();
}
decryptedText.append(c);
}

return decryptedText.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter plainText");
String plainText = sc.next();
System.out.println("Enter Key");
String key = sc.next();

// Encrypt the plain text


String encryptedText = encrypt(plainText, key);
System.out.println("Encrypted text: " + encryptedText);

// Decrypt the encrypted text


String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted text: " + decryptedText);
}
}

Input/Output:

5. Euclidean Algorithm
Code:

import java.util.Scanner;

public class EuclideanAlgorithm {

public static int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter num1 ");


int num1 = sc.nextInt();
System.out.println("Enter num2");
int num2 = sc.nextInt();

int gcdValue = gcd(num1, num2);


System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcdValue);
}
}

Input/Output:

6. Extended Euclidean Algorithm


Code:

import java.util.Scanner;

public class ExtendedEuclideanAlgorithm {

public static int[] extendedGCD(int a, int b) {


if (b == 0) {
return new int[] { a, 1, 0 };
} else {
int[] values = extendedGCD(b, a % b);
int gcd = values[0];
int x = values[1];
int y = values[2];
int temp = x;
x = y;
y = temp - (a / b) * y;
return new int[] { gcd, x, y };
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("Enter num1");
int a = sc.nextInt();
System.out.println("Enter num2");
int b = sc.nextInt();

int[] result = extendedGCD(a, b);


int gcd = result[0];

System.out.println("GCD is " + gcd );


}
}
Input/Output:

You might also like