-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailFence.java
More file actions
151 lines (124 loc) · 3.91 KB
/
RailFence.java
File metadata and controls
151 lines (124 loc) · 3.91 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
/**
*
* @author swa
*/
import java.util.Scanner;
public class RailFence {
static void display(char matrix[][],int r,int c)
{
System.out.println("The rail fence: ");
for(int i = 0;i<r;i++)
{
for(int j = 0;j<c;j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println(" ");
}
}
static String readRailsHorizontally(char matrix[][],int r,int c)
{
String ciphertext = "";
for(int i = 0;i<r;i++)
{
for(int j = 0;j<c;j++){
if(Character.isLetter(matrix[i][j]))
ciphertext += matrix[i][j];
}
}
return ciphertext;
}
static String encrypt(String plaintext,int n)
{
char rails[][] = new char[n][plaintext.length()];
int row = 0,col = 0,up=0,down=1;
for(int i = 0;i<plaintext.length();i++ )
{
if(row<=n-1 && down == 1)
{
rails[row][col] = plaintext.charAt(i);
row++;col++;
}
else
{
if(down == 1){
down = 0;
row--;
}
row--;
rails[row][col] = plaintext.charAt(i);
col++;
if(row == 0){
down = 1;
row++;
}
}
}
display(rails,n,plaintext.length());
String ciphertext = readRailsHorizontally(rails,n,plaintext.length());
System.out.println("The ciphertext is :"+ciphertext);
return ciphertext;
}
static String decrypt(String cipher,int n)
{
String message = "";
char rails[][] = new char[n][cipher.length()];
int row = 0,col = 0, up = 0, down = 1;
//Fill up the rail matrix again for decryption with the ciphertext
for (int i=0; i < cipher.length(); i++)
{
// check the direction of flow
if (row == 0)
down = 1;
if (row == n-1)
down = 0;
// place the marker
rails[row][col++] = '*';
// find the next row using direction flag
if(down == 1)
row++;
else
row--;
}
// now we can construct the fill the rail matrix
int index = 0;
for (int i=0; i<n; i++)
for (int j=0; j<cipher.length(); j++)
if (rails[i][j] == '*' && index<cipher.length())
rails[i][j] = cipher.charAt(index++);
display(rails,n,cipher.length());
row = 0;col = 0; up = 0; down = 1;
for(int i = 0;i<cipher.length();i++ )
{
if(row<=n-1 && down == 1)
{
message += rails[row][col];
row++;col++;
}
else
{
if(down == 1){
down = 0;
row--;
}
row--;
message += rails[row][col];
col++;
if(row == 0){
down = 1;
row++;
}
}
}
return message;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the plaintext :");
String plaintext = s.nextLine();
System.out.println("Enter the number of rails :");
int n = s.nextInt();
String ciphertext = encrypt(plaintext,n);
String message = decrypt(ciphertext,n);
System.out.println("The decrypted ciphertext :"+message);
}
}