-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrowtransposition.java
More file actions
133 lines (91 loc) · 4.01 KB
/
rowtransposition.java
File metadata and controls
133 lines (91 loc) · 4.01 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
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Scanner;
class RowTranspositionCipher {
private int columns;
RowTranspositionCipher(int keyLength) {
this.columns = keyLength;
}
public String encrypt(String plaintext, String key) {
int rows = (int) Math.ceil((double) plaintext.length() / columns);
char[][] grid = new char[rows][columns];
int k = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (k < plaintext.length()) {
grid[i][j] = plaintext.charAt(k++);
} else {
grid[i][j] = 'X';
}
}
}
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i < columns; i++) {
int col = Character.getNumericValue(key.charAt(i)) - 1;
if (col < 0 || col >= columns) {
throw new IllegalArgumentException("Invalid key. The key contains out-of-bound indices.");
}
for (int j = 0; j < rows; j++) {
ciphertext.append(grid[j][col]);
}
}
return ciphertext.toString();
}
public String decrypt(String ciphertext, String key) {
int rows = (int) Math.ceil((double) ciphertext.length() / columns);
char[][] grid = new char[rows][columns];
int k = 0;
for (int i = 0; i < columns; i++) {
int col = Character.getNumericValue(key.charAt(i)) - 1;
if (col < 0 || col >= columns) {
throw new IllegalArgumentException("Invalid key. The key contains out-of-bound indices.");
}
for (int j = 0; j < rows; j++) {
grid[j][col] = ciphertext.charAt(k++);
}
}
StringBuilder plaintext = new StringBuilder();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
plaintext.append(grid[i][j]);
}
}
return plaintext.toString().replaceAll("X+$", "");
}
}
public class rowtransposition {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("Select an option:");
System.out.println("1. Encryption");
System.out.println("2. Decryption");
int choice = sc.nextInt();
sc.nextLine();
if (choice == 1) {
System.out.print("Enter the plaintext: ");
String plaintext = sc.nextLine().replaceAll("\\s", "").toUpperCase();
System.out.print("Enter the key: ");
String key = sc.nextLine();
if (key.length() != key.chars().distinct().count()) {
System.out.println("The key should have unique digits.");
return;
}
RowTranspositionCipher cipher = new RowTranspositionCipher(key.length());
String encryptedMessage = cipher.encrypt(plaintext, key);
System.out.println("Encrypted message: " + encryptedMessage);
} else if (choice == 2) {
System.out.print("Enter the ciphertext: ");
String ciphertext = sc.nextLine().replaceAll("\\s", "").toUpperCase();
System.out.print("Enter the key: ");
String key = sc.nextLine();
if (key.length() != key.chars().distinct().count()) {
System.out.println("The key should have unique digits.");
return;
}
RowTranspositionCipher cipher = new RowTranspositionCipher(key.length());
String decryptedMessage = cipher.decrypt(ciphertext, key);
System.out.println("Decrypted message: " + decryptedMessage);
} else {
System.out.println("Invalid choice. Please select 1 for encryption or 2 for decryption.");
}
}
}