-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrailFenceCipher.java
More file actions
73 lines (73 loc) · 1.52 KB
/
railFenceCipher.java
File metadata and controls
73 lines (73 loc) · 1.52 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
class railfenceCipherHelper
{ int depth;
String encode(String msg, int depth) throws Exception {
int r = depth;
int l =
msg.length(); int c
= l / depth;
int k = 0;
char mat[][] = new char[r]
[c]; String enc = "";
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
{
if (k != l) {
mat[j][i] = msg.charAt(k++);
} else {
mat[j][i] = 'X';
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
{
enc += mat[i][j];
}
}
return enc;
}
String decode(String encmsg, int depth) throws Exception {
int r = depth;
int l =
encmsg.length(); int c
= l / depth;
int k = 0;
char mat[][] = new char[r]
[c]; String dec = "";
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
{
mat[i][j] = encmsg.charAt(k++);
}
}
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
{
dec += mat[j][i];
}
}
return dec;
}
}
class railFenceCipher {
public static void main(String[] args) throws java.lang.Exception {
railfenceCipherHelper rf = new railfenceCipherHelper();
String msg, enc, dec;
msg = "Anna University,
Chennai"; int depth = 2;
enc = rf.encode(msg,
depth); dec = rf.decode(enc,
depth);
System.out.println("Simulating Railfence Cipher\n------------------------");
System.out.println("Input Message : " + msg);
System.out.println("Encrypted Message : " + enc);
System.out.printf("Decrypted Message : " + dec);
}
}
OUTPUT:
Simulating Railfence Cipher
-
Input Message : Anna University, Chennai
Encrypted Message : An nvriy hnanaUiest,Ceni
Decrypted Message : Anna University,
Chenn