Skip to content

Instantly share code, notes, and snippets.

@kacpak
Created February 28, 2017 21:59
Show Gist options
  • Save kacpak/cf0ca7dc52aad0d1e41a243442af1cae to your computer and use it in GitHub Desktop.
Save kacpak/cf0ca7dc52aad0d1e41a243442af1cae to your computer and use it in GitHub Desktop.
const message = 'alamaworek';
const key = 'blablabla';
function encode(message, key) {
const shift = key
.repeat(Math.ceil(message.length / key.length))
.split('')
.map(c => c.charCodeAt(0) - 'a'.charCodeAt(0));
return message
.split('')
.map(c => c.charCodeAt(0) - 'a'.charCodeAt(0))
.map((c, i) => (c + shift[i]) % 26 + 'a'.charCodeAt(0))
.map(c => String.fromCharCode(c))
.join('');
}
const encodedMsg = encode(message, key);
console.log(encodedMsg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment