简体   繁体   中英

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 "abc", the output letter would be " zyx".

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$$'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM