Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

arrays - Obfuscating and replacing the Alphabets with JavaScript

I'm looking for a way to obfuscate the alphabet. What I specifically mean by "obfuscating the alphabet" is replacing the input letters with the second array letters. For example, I want to replace all letters in this array (the second array): ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; with this array:

["z", "y", "x", "w", "v", "u", "t", "s", "r", "q", "p", "o", "n", "m", "l", "k", "j", "i", "h", "g", "f", "e", "d", "c", "b", "a"] so like if my input letter is "a b c", the output letter would be " z y x".

question from:https://stackoverflow.com/questions/65830254/obfuscating-and-replacing-the-alphabets-with-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like you already found an answer yourself. Here's an option for anyone else looking for a basic substitution cypher:

const substitutionCypher = (input) => {
  // input should be a string; do your own validation
  
  const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  const cypherbet = 'zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'.split('');
  
  const resultArray = input.split('').map(char => {
    return (alphabet.includes(char)) ? cypherbet[alphabet.indexOf(char)] : char;
  });
  
  return resultArray.join('');
}

// Example usage:
console.log(substitutionCypher('hello !@#5638 WORLD! 83929$$'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...