-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailFence.java
More file actions
110 lines (106 loc) · 3.01 KB
/
Copy pathRailFence.java
File metadata and controls
110 lines (106 loc) · 3.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
public class RailFence
{
/**
* Encrypts the string using the specified number of rails
* @param m the message to encrypt
* @param key the number of rails to use
* @return the encrypted string. If the key <= 1 or >= m.length(), the original message m is returned
*/
public static String encrypt (String m, int key)
{
if (key <= 1 || key >= m.length())
return m;
String[] rails = new String[key];
for (int i = 0; i < rails.length; i++)
rails[i] = "";
int r = 0, dir = 1;
for (int i = 0; i < m.length(); i++)
{
rails[r] += m.charAt(i);
if (r == key - 1 && dir == 1 || r == 0 && dir == -1)
dir *= -1;
r += dir;
}
String c = "";
for (int i = 0; i < rails.length; i++)
c += rails[i];
return c;
}
/**
* Decrypts the string using the specified number of rails
* @param c the ciphertext to decrypt
* @param key the number of rails to use
* @return the decrypted string. If the key <= 1 or >= m.length(), the original message m is returned
*/
public static String decrypt (String c, int key)
{
if (key <= 1 || key >= c.length())
return c;
String[] rails = new String[key];
for (int i = 0; i < rails.length; i++)
rails[i] = "";
int[] lengths = new int[rails.length];
lengths[0] = (int) Math.ceil((double) c.length() / (2 * (key - 1)));
int extras = c.length() % (2 * (key - 1));
for (int r = 1; r < lengths.length - 1; r++)
{
lengths[r] = 2 * (c.length() / (2 * (key - 1)));
if (r < extras)
lengths[r]++;
if (2 * (key - 1) - r < extras)
lengths[r]++;
}
int index = 0;
for (int i = 0; i < lengths.length - 1; index += lengths[i], i++)
rails[i] = c.substring(index, index + lengths[i]);
rails[lengths.length - 1] = c.substring(index);
String m = "";
int r = 0, dir = 1;
for (int i = 0; i < c.length(); i++)
{
m += rails[r].charAt(0);
rails[r] = rails[r].substring(1);
if (r == key - 1 && dir == 1 || r == 0 && dir == -1)
dir *= -1;
r += dir;
}
return m;
}
/**
* Attempts to crack the cipher. Brute forces all possible keys [1, c.length()-1]
* @param c the ciphertext to crack.
* @return the most likely decryption
*/
public static String crack (String c)
{
double maxScore = Cipher.getQuadScore(c);
String m = c;
for (int key = 2; key < c.length(); key++)
{
String dec = decrypt(c, key);
double score = Cipher.getQuadScore(dec);
if (score > maxScore)
{
maxScore = score;
m = dec;
}
}
return m;
}
public static void demo ()
{
System.out.println("----------------Rail Fence---------------------");
String m = Cipher.getPlaintext();
int key = (int) (Math.random() * m.replaceAll("[^A-za-z ]", "").length() / 2.0);
String c = encrypt(m, key);
System.out.println("Ciphertext: " + c + "\n");
String dec = crack(c);
System.out.println("Decryption: " + dec + "\n");
if (dec.equals(m))
System.out.println("Decryption successful.");
else
System.out.println("Decryption failed");
System.out.println("*******************************************");
System.out.println();
}
}