Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const alphabet = ' _ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');
- function encrypt(plainText, key){
- const substitutionFunction = (index, key) => (index + key) % alphabet.length;
- return substituteAllLetters(substitutionFunction)(plainText, key);
- }
- function decrypt(cypherText, key){
- const substitutionFunction = (index, key) => (alphabet.length + (index - key) % alphabet.length) % alphabet.length;
- return substituteAllLetters(substitutionFunction)(cypherText, key);
- }
- function substituteAllLetters(substitutionFunction){
- return function(cypherText, key){
- return cypherText
- .toUpperCase()
- .split('')
- .map(function(letter){
- var index = alphabet.indexOf(letter);
- var newIndex = substitutionFunction(index, key);
- return alphabet[newIndex];
- })
- .join('');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement