-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRailFenceReal.java
75 lines (72 loc) · 2.38 KB
/
RailFenceReal.java
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
import java.util.*;
public class RailFenceReal {
public static void main(String[] args) {
System.out.println("----Rail Fence----");
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Input String : ");
String inputText = scan.next();
System.out.println("Enter the Key Value: ");
int key = scan.nextInt();
int r = key;
int len = inputText.length();
int c = len / (key-1);
char[][] mat = new char[r][c];
int k = 1;
mat[0][0] = inputText.charAt(0);
boolean reverse = false;
System.out.println("C : "+c);
for (int i = 0; i < c; i++) {
if (!reverse) {
for (int j = 1; j < r; j++) {
if (k != len) {
mat[j][i] = inputText.charAt(k);
k++;
} else {
mat[j][i] = 'X';
}
}
} else {
for (int j = key - 2; j >= 0; j--) {
if (k != len) {
mat[j][i] = inputText.charAt(k);
k++;
} else {
mat[j][i] = 'X';
}
}
}
reverse = !reverse;
}
String encryptedString = "";
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
if ((int) mat[i][j] != 0) {
encryptedString+=mat[i][j];
}
}
}
System.out.println(encryptedString);
char[] decrypted = new char[encryptedString.length()];
int n = 0;
for(k = 0 ; k < key; k ++) {
int index = k;
boolean down = true;
while(index < encryptedString.length() ) {
decrypted[index] = encryptedString.charAt(n++);
if(k == 0 || k == key - 1) {
index = index + 2 * (key - 1);
}
else if(down) {
index = index + 2 * (key - k - 1);
down = !down;
}
else {
index = index + 2 * k;
down = !down;
}
}
}
System.out.println( new String(decrypted));
scan.close();
}
}