function
encryptRailFence(text, key) {
let rail =
new
Array(key).fill().map(() =>
new
Array(text.length).fill(
'\n'
));
let dir_down =
false
;
let row = 0, col = 0;
for
(let i = 0; i < text.length; i++) {
if
(row == 0 || row == key - 1) dir_down = !dir_down;
rail[row][col++] = text[i];
dir_down ? row++ : row--;
}
let result = '
';
for (let i = 0; i < key; i++)
for (let j = 0; j < text.length; j++)
if (rail[i][j] != '
\n
') result += rail[i][j];
return result;
}
// function to decrypt a message
function decryptRailFence(cipher, key) {
// create the matrix to cipher plain text
// key = rows , text.length = columns
let rail = new Array(key).fill().map(() => new Array(cipher.length).fill('
\n
'));
// filling the rail matrix to mark the places with '
*
'
let dir_down = false;
let row = 0, col = 0;
for (let i = 0; i < cipher.length; i++) {
// check the direction of flow
if (row == 0) dir_down = true;
if (row == key - 1) dir_down = false;
// place the marker
rail[row][col++] = '
*
';
// find the next row using direction flag
dir_down ? row++ : row--;
}
// now we can construct the rail matrix by filling the marked places with cipher text
let index = 0;
for (let i = 0; i < key; i++)
for (let j = 0; j < cipher.length; j++)
if (rail[i][j] == '
*
' && index < cipher.length) rail[i][j] = cipher[index++];
// now read the matrix in zig-zag manner to construct the resultant text
let result = '
';
row = 0, col = 0;
for (let i = 0; i < cipher.length; i++) {
// check the direction of flow
if (row == 0) dir_down = true;
if (row == key - 1) dir_down = false;
// place the marker
if (rail[row][col] != '
*
') result += rail[row][col++];
// find the next row using direction flag
dir_down ? row++ : row--;
}
return result;
}
// driver program to check the above functions
// Encryption
console.log(encryptRailFence('
attack at once
', 2));
console.log(encryptRailFence('
GeeksforGeeks
', 3));
console.log(encryptRailFence('
defend the east wall
', 3));
// Now decryption of the same cipher-text
console.log(decryptRailFence('
GsGsekfrek eoe
', 3));
console.log(decryptRailFence('
atc toctaka ne
', 2));
console.log(decryptRailFence('
dnhaweedtees alf tl', 3));