简体   繁体   English

用 JavaScript 混淆和替换字母

[英]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'];例如,我想替换这个数组(第二个数组)中的所有字母: ['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". ["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"]所以如果我输入的字母是 "abc",那么 output 字母就是 "zyx"。

It looks like you already found an answer yourself.看来您自己已经找到了答案。 Here's an option for anyone else looking for a basic substitution cypher:这是寻找基本替代 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$$'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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