How to encrypt rail fence
Given text = REPEAT ATTACK TONIGHT
KEY = 4
Key is represent the row. (4 rows)
Length of text is represent the column (21 column).
Now, list the text into matrix 4 by 21 with zig zag pattern
Ex:
R . . . . . . . . . . K . . . . . G . .
. E . . . T . A . . . C . . . . I . H .
. . P . A . . . T . A . . . T . N . . . T
. . . E . . . . . T . . . . . O . . . . .
If you can see, there is null space. You can replace it with any alphabet or just remove the null space
before entering the matrix. In this case I replace it with ‘X’
R . . . . . X . . . . . K . . . . . G . .
. E . . . T . A . . . C . X . . . I . H .
. . P . A . . . T . A . . . T . N . . . T
. . . E . . . . . T . . . . . O . . . . .
Now, list above matrix row by row: RXKGETACXIHPATATNTETO done!!
How to decrypt rail fence
What we need to know is how to arrange back the encrypted message into plain text.
1. IF HAVE KEY
Key = 4. (4 rows)
Pattern
Distance between R -> X is 6. (1st row)
Distance between E -> T -> A is 4 and 2. (2nd row)
Distance between P -> A -> T is 2 and 4. (3rd row)
Distance between E -> T is 6. (4th row)
R6X6K6G
E4T2A4C2X4I2H
P2A4T2A4T2N4T
E6T6O
Arrange the pattern.
R . . . . . X . . . . . K . . . . . G . .
. E . . . T . A . . . C . X . . . I . H .
. . P . A . . . T . A . . . T . N . . . T
. . . E . . . . . T . . . . . O . . . . .
Note:
Key = 4
pattern
6 6 6..
4 2 4..
2 4 2..
6 6 6 ..
Key = 5
pattern
8 8 8..
6 2 6..
4 4 4..
2 6 2..
8 8 8..
Calculation : 2(key) – 2
/*
authors by hafiq
Rail Fence cipher and decrypt with key and no key
any expert can advise me to improve this code...tq..
*/
import java.util.*;
public class RFCipher {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = System.getProperty("line.separator");
scan.useDelimiter(line);
System.out.print("1. Encrypt 2. Decrypt :");
int option = scan.nextInt();
switch (option) {
case 1:
System.out.print("Enter Plain Text: ");
String text = scan.next();
System.out.print("Enter Key Level [more than 1] :");
int key = scan.nextInt();
if (key > 1) {
String enc = RFEncryptionWork(key, text);
System.out.println("Encrypted :" + enc);
} else
System.out.println("invalid level");
break;
case 2:
System.out.print("Enter Cipher text: ");
String enc = scan.next();
System.out.print("1. No key 2. With Key: ");
int k = scan.nextInt();
if(k==1){
String[] dec = RFDecryptionWork(enc);
for (String a: dec) {
System.out.println(a);
}
}
else{
System.out.print("Enter Key Level [more than 1] :");
key = scan.nextInt();
String dec = RFDecryptionWork(enc,key);
System.out.println("Decrypted :"+dec);
}
break;
default:
break;
}
}
static String RFEncryptionWork(int key, String text) {
int move = 1;
int count = 0;
String[][] rfp = new String[key][text.length()];
// arrange dot fence
for (int x = 0; x < rfp.length; x++) {
for (int y = 0; y < rfp[x].length; y++) {
rfp[x][y] = ".";
}
}
// formatting according fence rails
for (int i = 0; i < text.length(); i++) {
if ((move % 2) != 0) {
rfp[count][i] = "" + text.charAt(i);
if (count == (key - 1)) {
move = 2;
count = (key - 2);
} else
count++;
} else if ((move % 2) == 0) {
rfp[count][i] = "" + text.charAt(i);
if (count == 0) {
move = 1;
count = 1;
} else
count--;
}
//replace any white space with X or random
for (int x = 0; x < rfp.length; x++) {
for (int y = 0; y < rfp[x].length; y++) {
if (rfp[x][y].equals(" "))
rfp[x][y] = "X";
}
}
// display
System.out.println();
for (int i = 0; i < rfp.length; i++) {
for (int u = 0; u < rfp[i].length; u++) {
System.out.print(rfp[i][u] + " ");
}
System.out.println();
}
System.out.println();
StringBuilder cb = new StringBuilder();
//encode string from fence
for (int i = 0; i < rfp.length; i++) {
for (int u = 0; u < rfp[i].length; u++) {
if (!".".equals(rfp[i][u])) {
cb.append(rfp[i][u]);
}
}
}
return "" + cb;
}
static String[] RFDecryptionWork(String text) {
String[] ans = new String[text.length() - 2];
for (int z = 2; z < text.length(); z++) {
int key = z;
String[][] rfp = new String[key][text.length()];
for (int x = 0; x < rfp.length; x++) {
for (int y = 0; y < rfp[x].length; y++) {
rfp[x][y] = ".";
}
}
// arrange accroding to fence rail
int count = 0;
int c = 1;
int a = 0, b = 0;
int init = (2 * key) - 2;
a = init - 2;
b = 2;
for (int i = 0; i < rfp.length; i++) {
c = 0;
for (int u = i; u < rfp[i].length;) {
if (count != text.length()) {
if (i == 0 || i == key - 1) {
rfp[i][u] = "" + text.charAt(count);
u = u + init;
} else {
rfp[i][u] = "" + text.charAt(count);
if (c % 2 == 0)
u = u + a;
else if (c % 2 == 1)
u = u + b;
c++;
}
count++;
} else
break;
}
if (i != 0 && i != key - 1) {
a = a - 2;
b = b + 2;
}
}
int move = 1;
count = 0;
String sb = "";
for (int i = 0; i < text.length(); i++) {
if ((move % 2) != 0) {
sb = sb + rfp[count][i];
if (count == (key - 1)) {
move = 2;
count = (key - 2);
} else
count++;
} else if ((move % 2) == 0) {
sb = sb + rfp[count][i];
if (count == 0) {
move = 1;
count = 1;
} else
count--;
}
ans[z - 2] = sb;
}
return ans;
}
static String RFDecryptionWork(String text, int key) {
String[][] rfp = new String[key][text.length()];
for (int x = 0; x < rfp.length; x++) {
for (int y = 0; y < rfp[x].length; y++) {
rfp[x][y] = ".";
}
}
// arrange accroding to fence rail
int count = 0;
int c = 1;
int a = 0, b = 0;
int init = (2 * key) - 2;
a = init - 2;
b = 2;
for (int i = 0; i < rfp.length; i++) {
c = 0;
for (int u = i; u < rfp[i].length;) {
if (count != text.length()) {
if (i == 0 || i == key - 1) {
rfp[i][u] = "" + text.charAt(count);
u = u + init;
} else {
rfp[i][u] = "" + text.charAt(count);
if (c % 2 == 0)
u = u + a;
else if (c % 2 == 1)
u = u + b;
c++;
}
count++;
} else
break;
}
if (i != 0 && i != key - 1) {
a = a - 2;
b = b + 2;
}
}
//display
System.out.println("\n\nDecrypting..list into table");
for (int i = 0; i < rfp.length; i++) {
for (int u = 0; u < rfp[i].length; u++) {
System.out.print(rfp[i][u] + " ");
}
System.out.println();
}
int move = 1;
count = 0;
String sb = "";
for (int i = 0; i < text.length(); i++) {
if ((move % 2) != 0) {
sb = sb + rfp[count][i];
if (count == (key - 1)) {
move = 2;
count = (key - 2);
} else
count++;
} else if ((move % 2) == 0) {
sb = sb + rfp[count][i];
if (count == 0) {
move = 1;
count = 1;
} else
count--;
}
return sb;
}
}