Created
February 28, 2017 21:59
-
-
Save kacpak/cf0ca7dc52aad0d1e41a243442af1cae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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