How to Read a Rail Fence Cipher
Given a plainly-text message and a numeric key, cipher/de-cipher the given text using Rails Debate algorithm.
The track debate cipher (also called a zigzag aught) is a form of transposition zilch. It derives its name from the way in which information technology is encoded.
Examples:
Encryption Input : "GeeksforGeeks " Key = three Output : GsGsekfrek eoe Decryption Input : GsGsekfrek eoe Primal = 3 Output : "GeeksforGeeks " Encryption Input : "defend the east wall" Central = 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 = ii Output : set on at once
Encryption
In a transposition nothing, the order of the alphabets is re-arranged to obtain the cipher-text.
- In the rail fence zero, the apparently-text is written downwards and diagonally on successive track of an imaginary fence.
- When nosotros reach the lesser rail, nosotros traverse upward moving diagonally, after reaching the top runway, the management is changed again. Thus the alphabets of the bulletin are written in a zig-zag manner.
- Later on each alphabet has been written, the individual rows are combined to obtain the nix-text.
For case, if the message is "GeeksforGeeks" and the number of runway = three then cipher is prepared as:
Decryption
As we've seen before, the number of columns in track 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 tin figure-out the spots where texts should exist placed (using the same way of moving diagonally up and downward alternatively ).
- So, we make full the naught-text row wise. Afterward filling it, nosotros traverse the matrix in zig-zag mode to obtain the original text.
Implementation:
Permit cipher-text = "GsGsekfrek eoe" , and Key = iii
- Number of columns in matrix = len(cipher-text) = 13
- Number of rows = key = iii
Hence original matrix volition be of 3*13 , now marking places with text as '*' we get
* _ _ _ * _ _ _ * _ _ _ * _ * _ * _ * _ * _ * _ * _ _ * _ _ _ * _ _ _ * _
Below is a plan to encrypt/decrypt the message using the above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
string encryptRailFence(string text, int central)
{
char rail[cardinal][(text.length())];
for ( int i=0; i < key; i++)
for ( int j = 0; j < text.length(); j++)
rail[i][j] = '\n' ;
bool dir_down = false ;
int row = 0, col = 0;
for ( int i=0; i < text.length(); i++)
{
if (row == 0 || row == key-1)
dir_down = !dir_down;
rails[row][col++] = text[i];
dir_down?row++ : row--;
}
cord result;
for ( int i=0; i < key; i++)
for ( int j=0; j < text.length(); j++)
if (rail[i][j]!= '\n' )
upshot.push_back(rail[i][j]);
render result;
}
string decryptRailFence(string cipher, int primal)
{
char rail[central][nil.length()];
for ( int i=0; i < central; i++)
for ( int j=0; j < goose egg.length(); j++)
rail[i][j] = '\n' ;
bool dir_down;
int row = 0, col = 0;
for ( int i=0; i < cipher.length(); i++)
{
if (row == 0)
dir_down = true ;
if (row == cardinal-1)
dir_down = faux ;
rail[row][col++] = '*' ;
dir_down?row++ : row--;
}
int index = 0;
for ( int i=0; i<primal; i++)
for ( int j=0; j<cipher.length(); j++)
if (rail[i][j] == '*' && index<cipher.length())
rail[i][j] = cipher[alphabetize++];
string consequence;
row = 0, col = 0;
for ( int i=0; i< cipher.length(); i++)
{
if (row == 0)
dir_down = true ;
if (row == key-1)
dir_down = false ;
if (rails[row][col] != '*' )
result.push_back(rail[row][col++]);
dir_down?row++: row--;
}
return result;
}
int chief()
{
cout << encryptRailFence( "attack at one time" , 2) << endl;
cout << encryptRailFence( "GeeksforGeeks " , 3) << endl;
cout << encryptRailFence( "defend the east wall" , 3) << endl;
cout << decryptRailFence( "GsGsekfrek eoe" ,3) << endl;
cout << decryptRailFence( "atc toctaka ne" ,2) << endl;
cout << decryptRailFence( "dnhaweedtees alf tl" ,3) << endl;
return 0;
}
Python3
def encryptRailFence(text, fundamental):
rail = [[ '\n' for i in range ( len (text))]
for j in range (key)]
dir_down = False
row, col = 0 , 0
for i in range ( len (text)):
if (row = = 0 ) or (row = = central - 1 ):
dir_down = not dir_down
rail[row][col] = text[i]
col + = 1
if dir_down:
row + = 1
else :
row - = 1
result = []
for i in range (central):
for j in range ( len (text)):
if rail[i][j] ! = '\n' :
consequence.append(rail[i][j])
render ("" . join(result))
def decryptRailFence(cipher, cardinal):
track = [[ '\north' for i in range ( len (nothing))]
for j in range (key)]
dir_down = None
row, col = 0 , 0
for i in range ( len (null)):
if row = = 0 :
dir_down = True
if row = = cardinal - one :
dir_down = False
rails[row][col] = '*'
col + = 1
if dir_down:
row + = one
else :
row - = one
index = 0
for i in range (key):
for j in range ( len (cipher)):
if ((rail[i][j] = = '*' ) and
(index < len (cipher))):
rail[i][j] = zippo[alphabetize]
index + = ane
result = []
row, col = 0 , 0
for i in range ( len (nix)):
if row = = 0 :
dir_down = True
if row = = key - 1 :
dir_down = False
if (track[row][col] ! = '*' ):
result.suspend(rail[row][col])
col + = i
if dir_down:
row + = 1
else :
row - = ane
return ("".join(result))
if __name__ = = "__main__" :
print (encryptRailFence( "set on at once" , 2 ))
print (encryptRailFence( "GeeksforGeeks " , 3 ))
print (encryptRailFence( "defend the east wall" , 3 ))
print (decryptRailFence( "GsGsekfrek eoe" , iii ))
print (decryptRailFence( "atc toctaka ne" , 2 ))
impress (decryptRailFence( "dnhaweedtees alf tl" , 3 ))
Output:
atc toctaka ne GsGsekfrek eoe dnhaweedtees alf tl GeeksforGeeks assault at once delendfthe due east wal
Fourth dimension Complexity: O(row * col)
Auxiliary Space: O(row * col)
References:
https://en.wikipedia.org/wiki/Rail_fence_cipher
This article is contributed by Ashutosh Kumar If you similar GeeksforGeeks and would like to contribute, y'all can too write an commodity using write.geeksforgeeks.org or mail service your article to review-team@geeksforgeeks.org. Meet your commodity appearing on the GeeksforGeeks primary page and help other Geeks.
Please write comments if yous discover anything incorrect, or you want to share more than information near the topic discussed in a higher place.
guimondhathapasse86.blogspot.com
Source: https://www.geeksforgeeks.org/rail-fence-cipher-encryption-decryption/
0 Response to "How to Read a Rail Fence Cipher"
Post a Comment