我最近制作了一个简单的程序,可以接受地图中定义的任何字母,并将它们替换为分配给它们的任何字母,反之亦然。例如,如果您在我的代码中输入welcome,它将输出为:'®¬Δ©'˙Δ©¬ΔΔ≈'
代码是:
<html>
<head>
<script type="text/javascript">
var map = {
"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" : "˜",
" " : " "
}
function main() {
var input = prompt("Enter a string!");
var encrypted = "";
for(var i = 0; i < input.length; i++){
if(map[input[i]]){
encrypted += map[input[i]];
}
}
document.write(encrypted);
}
main()
</script>
</head>
<body>
</body>
</html>
它可以输出我想要的字符串,但它只能输出a,b,c ... x,y和z。
理想情况下,为了使它更容易,我想让它检测更长的随机字符串,如Mfdshs,所以我不必担心重复字母。如何让它检测到国会大厦字母然后检测所有小写字母,并找到分配给哪个字母?不只是一个字母替换。
我的意思是,地图是这样的:
var map = {
"a" : "Wgbf",
"b" : "Gvhs",
"c" : "Bgty",
"d" : "Dafd",
"e" : "Ihkj",
"f" : "Hjuk",
"g" : "Byfg",
"h" : "Hynk",
"i" : "Afds",
"j" : "Njio",
"k" : "Yipl",
"l" : "Wetv",
"m" : "Obth",
"n" : "Cbhg",
"o" : "Kolp",
"p" : "Pnya",
"q" : "Fnpd",
"r" : "Buip",
"s" : "Gknp",
"t" : "Lnug",
"u" : "Fuih",
"v" : "Vylp",
"w" : "Fnng",
"x" : "Bhyh",
"y" : "Kkgh",
"z" : "Xnvx",
" " : "Rgyb"
}
反之亦然。
答案 0 :(得分:0)
如果替换的长度都相同,则可以将字符串拆分为4个字符的子字符串并循环生成的数组。在StackOverflow上查找实现是留给读者的练习。我保证会在那里。
您还可以将字符串转换为ArrayBuffer并将其作为二进制数据(这是所有真正的加密函数所做的)操作,这将允许您将4个字节作为单个数字读取:
cypherInput = "IhkjBhyhWgbfObthPnyaWetvIhkj"
buffer = new TextEncoder().encode(cypherInput).buffer
words = new Uint32Array(buffer) // reinterpret the buffer
cleartext = words.map(word => /* map each possible 32-bit word to a character */)
答案 1 :(得分:0)
如果替换不总是包含4个字符的单词,那么理想情况下,您应该使用类似Finite-state transducer的内容来执行此操作。 JavaScript只为您执行有限状态自动机(即RegEx),所以让我们使用它(假设您只在代码字中使用ASCII字母):
cypherInput.match(/[A-Z][a-z]+/g)
将返回包含所有代码字的数组。然后有几种方法可以生成明文,其中一种方法是使用带有映射函数的replace
作为第二个参数,而不是显式循环匹配:
cypherInput.replace(/[A-Z][a-z]+/g, word => /* your mapping function */)
如果你想在你的代码中使用非ASCII,那么在RegEx中表达一组首字母可能会很快就会有很多工作。
答案 2 :(得分:0)
最简单的解决方案显示在下面的代码中,但它有点慢。为了使其工作,对象map
(键)的值不能相互包含。具有不同长度的密钥字符串也应该正常工作。
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var map = {
"a": "Wgbf",
"b": "Gvhs",
"c": "Bgty",
"d": "Dafd",
"e": "Ihkj",
"f": "Hjuk",
"g": "Byfg",
"h": "Hynk",
"i": "Afds",
"j": "Njio",
"k": "Yipl",
"l": "Wetv",
"m": "Obth",
"n": "Cbhg",
"o": "Kolp",
"p": "Pnya",
"q": "Fnpd",
"r": "Buip",
"s": "Gknp",
"t": "Lnug",
"u": "Fuih",
"v": "Vylp",
"w": "Fnng",
"x": "Bhyh",
"y": "Kkgh",
"z": "Xnvx",
" ": "Rgyb"
}
function encode(input){
var encrypted = "";
for(var i = 0; i < input.length; i++){
if(map[input[i]]){
encrypted += map[input[i]];
}
}
return encrypted;
}
function decode(input) {
var enc = ''+input;
var result = '';
do {
for (const key of Object.keys(map)) {
var value = map[key];
if (enc.startsWith(value)) {
result += key;
enc = enc.substr(value.length);
break;
}
}
} while (enc.length > 0);
return result;
}
function encClick(){
var e = encode(document.getElementById("plain").value);
document.getElementById("encodedRes").innerHTML = e;
}
function decClick(){
var e = decode(document.getElementById("encoded").value);
document.getElementById("decodedRes").innerHTML = e;
}
function encPrompt(){
var input = prompt("Enter text", "work ing");
var e = encode(input);
document.getElementById("promptResult").innerHTML = e;
}
function decPrompt(){
var input = prompt("Enter cipher", "FnngKolpBuipYiplRgybAfdsCbhgByfg");
var e = decode(input);
document.getElementById("promptResult").innerHTML = e;
}
</script>
</head>
<body>
<span id="promptResult"></span><br>
<button onclick="encPrompt()">Prompt encode</button><button onclick="decPrompt()">Prompt decode</button><br><br><br>
<input id="plain" type="text" name="" value="work ing" />
<button onclick="encClick()">Encode</button><span id="encodedRes"></span><br><br>
<input id="encoded" type="text" name="" value="FnngKolpBuipYiplRgybAfdsCbhgByfg" />
<button onclick="decClick()">Decode</button><span id="decodedRes"></span>
</body>
</html>
&#13;