Repository navigation
Expand file tree
/
Copy pathEncrypt.java
More file actions
121 lines (102 loc) · 4.55 KB
/
Copy pathEncrypt.java
File metadata and controls
121 lines (102 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Class to perform Playfair cipher encryption
public class Encrypt {
// 5x5 matrix used for Playfair cipher key square
private char[][] keySquare;
// Constructor that initializes the key square using the given key
public Encrypt(String key) {
keySquare = generateKeySquare(key);
}
// Generates the 5x5 matrix (key square) based on the given key
private char[][] generateKeySquare(String key) {
boolean[] used = new boolean[26]; // To keep track of letters already used
key = key.toUpperCase().replace("J", "I"); // Convert to uppercase and replace J with I
char[][] matrix = new char[5][5]; // Declare the 5x5 matrix
int index = 0; // Tracks current matrix index (row * 5 + col)
// Insert characters from the key into the matrix
for (char c : key.toCharArray()) {
if (c >= 'A' && c <= 'Z' && !used[c - 'A']) { // Only use valid, unused letters
matrix[index / 5][index % 5] = c; // Compute row and col from index
used[c - 'A'] = true; // Mark character as used
index++; // Move to next position in the matrix
}
}
// Fill remaining matrix with unused letters A-Z (excluding J)
for (char c = 'A'; c <= 'Z'; c++) {
if (c != 'J' && !used[c - 'A']) {
matrix[index / 5][index % 5] = c;
used[c - 'A'] = true;
index++;
}
}
return matrix; // Return the filled key square
}
// Getter method to access the key square
public char[][] getKeySquare() {
return keySquare;
}
// Alternate getter, same as getKeySquare()
public char[][] getMatrix() {
return keySquare;
}
// Encrypts the given text using the Playfair cipher
public String encrypt(String text) {
text = prepareText(text); // Preprocess the input text
StringBuilder encryptedText = new StringBuilder();
// Loop through the text two characters at a time
for (int i = 0; i < text.length(); i += 2) {
char a = text.charAt(i); // First letter of the pair
char b = text.charAt(i + 1); // Second letter of the pair
int[] posA = findPosition(a); // Find position of letter A in the matrix
int[] posB = findPosition(b); // Find position of letter B in the matrix
// If both letters are in the same row
if (posA[0] == posB[0]) {
encryptedText.append(keySquare[posA[0]][(posA[1] + 1) % 5]); // Shift right
encryptedText.append(keySquare[posB[0]][(posB[1] + 1) % 5]);
}
// If both letters are in the same column
else if (posA[1] == posB[1]) {
encryptedText.append(keySquare[(posA[0] + 1) % 5][posA[1]]); // Shift down
encryptedText.append(keySquare[(posB[0] + 1) % 5][posB[1]]);
}
// Letters form a rectangle: swap columns
else {
encryptedText.append(keySquare[posA[0]][posB[1]]);
encryptedText.append(keySquare[posB[0]][posA[1]]);
}
}
return encryptedText.toString(); // Return the final encrypted text
}
// Finds the row and column of a given character in the matrix
private int[] findPosition(char c) {
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (keySquare[row][col] == c) {
return new int[]{row, col}; // Return the position as [row, col]
}
}
}
return null; // Should not happen if input is clean
}
// Prepares the plaintext for encryption:
// - Converts to uppercase
// - Replaces J with I
// - Removes non-letter characters
// - Adds X between duplicate letters in a pair
// - Adds X at the end if the length is odd
private String prepareText(String text) {
text = text.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", ""); // Sanitize text
StringBuilder sb = new StringBuilder();
// Insert X between repeated characters
for (int i = 0; i < text.length(); i++) {
sb.append(text.charAt(i));
if (i < text.length() - 1 && text.charAt(i) == text.charAt(i + 1)) {
sb.append('X'); // Insert 'X' between duplicate letters
}
}
// If the result has odd length, pad with X
if (sb.length() % 2 != 0) {
sb.append('X');
}
return sb.toString(); // Return processed text
}
}