Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const alphabet = ' _ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split('');
  2.  
  3. function encrypt(plainText, key){
  4. const substitutionFunction = (index, key) => (index + key) % alphabet.length;
  5. return substituteAllLetters(substitutionFunction)(plainText, key);
  6. }
  7. function decrypt(cypherText, key){
  8. const substitutionFunction = (index, key) => (alphabet.length + (index - key) % alphabet.length) % alphabet.length;
  9. return substituteAllLetters(substitutionFunction)(cypherText, key);
  10. }
  11.  
  12. function substituteAllLetters(substitutionFunction){
  13. return function(cypherText, key){
  14. return cypherText
  15. .toUpperCase()
  16. .split('')
  17. .map(function(letter){
  18. var index = alphabet.indexOf(letter);
  19. var newIndex = substitutionFunction(index, key);
  20. return alphabet[newIndex];
  21. })
  22. .join('');
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement