-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRailFence.java
More file actions
191 lines (168 loc) · 5.72 KB
/
RailFence.java
File metadata and controls
191 lines (168 loc) · 5.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*********
-*- Made by VoxelPixel
-*- For YouTube Tutorial
-*- https://github.com/VoxelPixel
-*- Support me on Patreon: https://www.patreon.com/voxelpixel
*********/
import java.util.Scanner;
public class RailFence {
private static Scanner in;
public static void main(String[] args){
in = new Scanner(System.in);
System.out.print("1. Encryption\n2. Decryption\nChoose(1,2): ");
int choice = in.nextInt();
in.nextLine();
if (choice == 1){
System.out.println("---Encryption---");
cipherEncryption();
} else if (choice == 2){
System.out.println("---Decryption---");
cipherDecryption();
} else {
System.out.println("Incorrect Choice");
}
}
private static void cipherDecryption() {
System.out.print("Enter message: ");
String message = in.nextLine();
// removing white space from message
message = message.replaceAll("\\s","");
in.nextLine();
System.out.print("Enter key(number of rails): ");
int key = in.nextInt();
in.nextLine();
char[][] rail = new char[key][message.length()];
// matrix
for (int i = 0; i < key; i++){
for (int j = 0; j < message.length(); j++) {
rail[i][j] = '.';
}
} // for
// testing rail
// for (int i = 0; i < key; i++) {
// for (int j = 0; j < message.length(); j++) {
// System.out.print(rail[i][j]);
// }
// System.out.println();
// }
// putting letters in the matrix in zig-zag
int row = 0;
int check = 0;
for (int i = 0; i < message.length(); i++) {
if (check == 0){
rail[row][i] = message.charAt(i);
row++;
if (row == key){
check = 1;
row--;
}
} else if(check == 1){
row--;
rail[row][i] = message.charAt(i);
if (row == 0){
check = 0;
row = 1;
}
} // if-else
} // for
// changing order of rails
int ordr = 0;
for (int i = 0; i < key; i++) {
for (int j = 0; j < message.length(); j++) {
String temp = rail[i][j] + "";
if (temp.matches("\\.")){
// skipping in case of '.'
continue;
} else {
// adding cipher letters one by one diagonally
rail[i][j] = message.charAt(ordr);
ordr++;
} // if-else
} // inner for
} // for
// checking message reorder on rails
System.out.println("Reorder");
for (int i = 0; i < key; i++) {
for (int j = 0; j < message.length(); j++) {
System.out.print(rail[i][j]);
}
System.out.println();
}
String decrypText = "";
check = 0;
row = 0;
// converting rails back into a single line message
for (int i = 0; i < message.length(); i++) {
if (check == 0){
decrypText += rail[row][i];
row++;
if(row == key){
check = 1;
row--;
}
} else if (check == 1){
row--;
decrypText += rail[row][i];
if(row == 0){
check = 0;
row = 1;
}
} // if-else
} // for
System.out.println("Decrypted Text: " + decrypText);
}
private static void cipherEncryption() {
System.out.print("Enter message: ");
String message = in.nextLine();
// removing white space from message
message = message.replaceAll("\\s","");
in.nextLine();
System.out.print("Enter key(number of rails): ");
int key = in.nextInt();
in.nextLine();
char[][] rail = new char[key][message.length()];
// matrix
for (int i = 0; i < key; i++){
for (int j = 0; j < message.length(); j++) {
rail[i][j] = '.';
}
} // for
// // testing rail
// for (int i = 0; i < key; i++) {
// for (int j = 0; j < message.length(); j++) {
// System.out.print(rail[i][j]);
// }
// System.out.println();
// }
// putting letters in the matrix in zig-zag
int row = 0;
int check = 0;
for (int i = 0; i < message.length(); i++) {
if (check == 0){
rail[row][i] = message.charAt(i);
row++;
if (row == key){
check = 1;
row--;
}
} else if(check == 1){
row--;
rail[row][i] = message.charAt(i);
if (row == 0){
check = 0;
row = 1;
}
} // if-else
} // for
String encrypText = "";
for (int i = 0; i < key; i++) {
for (int j = 0; j < message.length(); j++) {
encrypText += rail[i][j];
// System.out.print(rail[i][j]);
}
// System.out.println();
}
encrypText = encrypText.replaceAll("\\.","");
System.out.println("Encrypted Message: " + encrypText);
}
}