Wednesday, October 7, 2015

The Rail Fence Cipher and how to decrypt

The Rail Fence Cipher is a type of transposition cipher. A transposition cipher involves the rearranging of the letters in the plaintext to encrypt the message. This is in contrast to a substitution cipher, in which the plaintext letters are replaced by letters from another alphabet (or by different letters from the same alphabet).

note: The rail fence cipher is not very strong; the number of practical keys (the number of rails) is small enough that a crypt analyst can try them all by hand.

In the rail fence cipher, the plaintext is written downwards and diagonally on successive "rails" of an imaginary fence, then moving up when we reach the bottom rail. When we reach the top rail, the message is written downwards again until the whole plaintext is written out. The message is then read off in rows. For example, if we have 4 "rails" and a message of 'REPEAT ATTACK TONIGHT', the cipherer writes out:

3 rails

R . . . A . . . T . . . K . . . N . . . T
. E . E . T . A . T . C . X . O . I . H .
. . P . . . X . . . A . . . T . . . G . .

Encrypted :RATKNTEETATCXOIHPXATG

4 rails

R . . . . . X . . . . . K . . . . . G . .
. E . . . T . A . . . C . X . . . I . H .
. . P . A . . . T . A . . . T . N . . . T
. . . E . . . . . T . . . . . O . . . . .

Encrypted :RXKGETACXIHPATATNTETO

Note: Space in text are replaced with any suitable random alphabet. in this case iam using 'X' as replace of white space

Download code :- RFcipher


oopss.. i miss how to decrypt without key

2. Decrypt without key

In railfence cipher, the maximun level we can make is the length of text. If the ciphertext length is 100, so the maximum level is 100. To decrypt that ciphertext, the possibility of correct message will get is 100.  Below is example of decrypt without key..



  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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
   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;
    }
}

No comments:

Post a Comment