CS22611 – CRYPTOGRAPHY AND NETWORK SECURITY LABORATORY
EXP NO: 2(c) Substitution Ciphers
DATE: Hill Cipher
AIM:
To write a java program to implement the Hill Cipher.
ALGORITHM:
Convert the plaintext into numerical values
Assign A = 0, B = 1, ..., Z = 25.
If the plaintext length is not a multiple of the matrix size, pad it with 'X'.
Divide the plaintext into blocks
The length of each block should match the size of the key matrix (n×n).
Multiply each block (vector) with the key matrix
Perform matrix multiplication: C=K×Pmod 26
Where:
C = Ciphertext vector
K = Key matrix
P = Plaintext vector
Convert the result back to characters
Map numbers 0-25 back to A-Z.
PROGRAM:
import java.util.Scanner;
public class HillCipher {
private static int charToNum(char c) {
return c - 'A';
}
private static char numToChar(int num) {
return (char) (num + 'A');
}
private static int[] matrixMultiply(int[][] keyMatrix, int[] vector) {
int n = keyMatrix.length;
Reg. no: 2127220501101 Page no:
CS22611 – CRYPTOGRAPHY AND NETWORK SECURITY LABORATORY
int[] result = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result[i] += keyMatrix[i][j] * vector[j];
}
result[i] %= 26; // Modulo operation to keep values in range
}
return result;
}
private static int determinant(int[][] matrix) {
return (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) % 26;
}
private static int modInverse(int num, int mod) {
num = (num % mod + mod) % mod; // Ensure positive num
for (int x = 1; x < mod; x++) {
if ((num * x) % mod == 1) return x;
}
return -1; // No modular inverse exists
}
private static int[][] inverseMatrix(int[][] matrix) {
int det = determinant(matrix);
int detInverse = modInverse(det, 26);
if (detInverse == -1) {
System.out.println("Error: Key matrix is not invertible modulo 26!");
return null;
}
int[][] inverse = {
Reg. no: 2127220501101 Page no:
CS22611 – CRYPTOGRAPHY AND NETWORK SECURITY LABORATORY
{ matrix[1][1] * detInverse % 26, (-matrix[0][1] + 26) * detInverse % 26 },
{ (-matrix[1][0] + 26) * detInverse % 26, matrix[0][0] * detInverse % 26 }
};
return inverse;
}
public static String encryptText(String plaintext, int[][] key) {
int n = key.length;
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", "");
if (plaintext.length() % n != 0) {
plaintext += "X".repeat(n - (plaintext.length() % n)); // Pad with 'X'
}
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i < plaintext.length(); i += n) {
int[] vector = new int[n];
for (int j = 0; j < n; j++) {
vector[j] = charToNum(plaintext.charAt(i + j));
}
int[] encryptedVector = matrixMultiply(key, vector);
for (int j = 0; j < n; j++) {
ciphertext.append(numToChar(encryptedVector[j]));
}
}
return ciphertext.toString();
Reg. no: 2127220501101 Page no:
CS22611 – CRYPTOGRAPHY AND NETWORK SECURITY LABORATORY
}
public static String decryptText(String ciphertext, int[][] key) {
int[][] inverseKey = inverseMatrix(key);
if (inverseKey == null) return "Decryption not possible";
return encryptText(ciphertext, inverseKey);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Example 2x2 Key Matrix
int[][] key = {
{3, 3},
{2, 5}
};
System.out.print("Enter plaintext: ");
String plaintext = scanner.nextLine();
String ciphertext = encryptText(plaintext, key);
System.out.println("Encrypted Text: " + ciphertext);
String decryptedText = decryptText(ciphertext, key);
System.out.println("Decrypted Text: " + decryptedText);
scanner.close();
}
}
Reg. no: 2127220501101 Page no:
CS22611 – CRYPTOGRAPHY AND NETWORK SECURITY LABORATORY
SAMPLE INPUT AND OUTPUT:
INFERENCE:
Encrypts multiple letters at once, making frequency analysis harder.
Unlike Caesar Cipher, which encrypts one letter at a time, Hill Cipher transforms
blocks of text.
Uses matrix multiplication and modular arithmetic, making it a structured
cryptographic technique.
The key matrix introduces complex transformations, unlike simple shift ciphers
RESULT:
Thus the java program "to implement Hill Cipher " is executed and the output is
verified successfully
Reg. no: 2127220501101 Page no: