Skip to content

Instantly share code, notes, and snippets.

@teekayarezee
Last active December 11, 2018 03:50
Show Gist options
  • Save teekayarezee/62bf64bff0f7cb3d8085e185c0720711 to your computer and use it in GitHub Desktop.
Save teekayarezee/62bf64bff0f7cb3d8085e185c0720711 to your computer and use it in GitHub Desktop.
Functional JS Columnar transposition cipher
const transposeEncryptMessage = (string, col) => [...Array(col)].map((_, i) => [...Array(Math.ceil(string.length / col))].reduce((acc, _, n) => {
if (n * col + i < string.length)
acc += string[n * col + i]
return acc;
}, [])).join('');
col = 8;
string = "Cenoonommstmme oo snnio. s s c";
len = string.length;
rows = Math.ceil(len / col);
rem = rows * col - len;
[...Array(rows)].map((_, i) => [...Array(col)].reduce((acc, _, n) => {
let c = n * rows + i + (n*rows >= (len-rem)?-1:0);
if (c < len && i*n<=col*rows/2+1)
acc += string[c]
return acc;
}, [])).join('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment