Given a plain-text message and a numeric key, cipher/de-cipher the given text using Rail Fence algorithm.
The rail fence cipher (also called a zigzag cipher) is a form of transposition cipher. It derives its name from the way in which it is encoded.
Encryption Input : "GeeksforGeeks " Key = 3 Output : GsGsekfrek eoe Decryption Input : GsGsekfrek eoe Key = 3 Output : "GeeksforGeeks " Encryption Input : "defend the east wall" Key = 3 Output : dnhaweedtees alf tl Decryption Input : dnhaweedtees alf tl Key = 3 Output : defend the east wall Encryption Input : "attack at once" Key = 2 Output : atc toctaka ne Decryption Input : "atc toctaka ne" Key = 2 Output : attack at once
In a transposition cipher, the order of the alphabets is re-arranged to obtain the cipher-text.
- In the rail fence cipher, the plain-text is written downwards and diagonally on successive rails of an imaginary fence.
- When we reach the bottom rail, we traverse upwards moving diagonally, after reaching the top rail, the direction is changed again. Thus the alphabets of the message are written in a zig-zag manner.
- After each alphabet has been written, the individual rows are combined to obtain the cipher-text.
As we’ve seen earlier, the number of columns in rail fence cipher remains equal to the length of plain-text message. And the key corresponds to the number of rails.
- Hence, rail matrix can be constructed accordingly. Once we’ve got the matrix we can figure-out the spots where texts should be placed (using the same way of moving diagonally up and down alternatively ).
- Then, we fill the cipher-text row wise. After filling it, we traverse the matrix in zig-zag manner to obtain the original text.
Let cipher-text = “GsGsekfrek eoe” , and Key = 3
- Number of columns in matrix = len(cipher-text) = 12
- Number of rows = key = 3
Hence original matrix will be of 3*12 , now marking places with text as ‘*’ we get
* _ _ _ * _ _ _ * _ _ _ * _ * _ * _ * _ * _ * _ * _ _ * _ _ _ * _ _ _ * _
Below is a program to encrypt/decrypt the message using the above algorithm.
atc toctaka ne GsGsekfrek eoe dnhaweedtees alf tl GeeksforGeeks attack at once delendfthe east wal
This article is contributed by Ashutosh Kumar If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between Monoalphabetic Cipher and Polyalphabetic Cipher
- Weak RSA decryption with Chinese-remainder theorem
- Encryption, Its Algorithms And Its Future
- Difference Between Symmetric and Asymmetric Key Encryption
- XOR Encryption by Shifting Plaintext
- Data encryption standard (DES) | Set 1
- Evolution of Malwares from Encryption to Metamorphism
- ElGamal Encryption Algorithm
- Simplified International Data Encryption Algorithm (IDEA)
- Strength of Data encryption standard (DES)
- Knapsack Encryption Algorithm in Cryptography
- What is RC4 Encryption?
- Symmetric Encryption Cryptography in Java
- Asymmetric Encryption Cryptography in Java
- Caesar Cipher in Cryptography
- Vigenère Cipher
- XOR Cipher
- Implementation of Affine Cipher
- Columnar Transposition Cipher
- ROT13 cipher