为什么我的解密功能不起作用?

Why is my decryption function not working?

我创建了一个函数来加密和解密消息。加密工作正常。但是当我尝试记录加密 Hello World! 它只是记录 H.

const chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".split("");
const numbs = "0123456789".split("");
const symbols = "!@#$%^&*()_+-=[]{}|;':,./<>?\" ".split("");

function encrypt(message) {
  message = message.split("")
  var output = []
  message.forEach(element => {
    if (chars.includes(element)) {
      output.push("c" + chars.indexOf(element))
    } else if (numbs.includes(element)) {
      output.push("n" + numbs.indexOf(element))
    } else if (symbols.includes(element)) {
      output.push("s" + symbols.indexOf(element))
    } else {
      console.log(element)
      throw new Error(`Unknown character`)
    }
  });
  return output.join("")
}

function decrypt(message) {
  message = message.split("");
  var output = [];
  var prevDeter;
  var prevNumbs = [];
  message.forEach(element => {
    if (element == "c") {
      prevDeter = "c"
      if (prevNumbs.length > 0) {
        output.push(chars[parseInt(prevNumbs.join(""))])
      }
    } else if (element == "n") {
      prevDeter = "n"
      if (prevNumbs.length > 0) {
        output.push(numbs[parseInt(prevNumbs.join(""))])
      }
    } else if (element == "s") {
      prevDeter = "s"
      if (prevNumbs.length > 0) {
        output.push(symbols[parseInt(prevNumbs.join(""))])
      }
    } else {
      prevNumbs.push(element)
    }
  });
  return output.join("")
}

//expected to log Hello World! but logs H and when starting the message with a symbol or number it just logs nothing
console.log(decrypt(encrypt("Hello World!")))

已修复它,我编辑了编码系统以在字符和解码系统之间放置一个 - 以将消息拆分为 - 并检查元素是否以 c n 或 s 开头。然后我只是使用子字符串来获取数字并解密它

const chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".split("");
const numbs = "0123456789".split("");
const symbols = "!@#$%^&*()_+-=[]{}|;':,./<>?\" ".split("");

function encrypt(message) {
    message = message.split("");
    var output = [];
    message.forEach(element => {
        if(chars.includes(element)) {
            output.push("-c" + chars.indexOf(element));
        }else if(numbs.includes(element)) {
            output.push("-n" + numbs.indexOf(element));
        }else if(symbols.includes(element)) {
            output.push("-s" + symbols.indexOf(element));
        }else{
            console.log(element);
            throw new Error(`Unknown character`);
        };
    });
    return output.join("");
};

function decrypt(message) {
    message = message.split("-");
    console.log(message)
    var output = [];
    message.forEach(element => {
        if(element.startsWith("c")) {
            output.push(chars[element.substring(1)]);
        }else if(element.startsWith("n")) {
            output.push(numbs[element.substring(1)]);
        }else if(element.startsWith("s")) {
            output.push(symbols[element.substring(1)]);
        }else if(element.length < 1){
            
        }else{
            throw new Error(`Invalid message`);
        }
    });
    return output.join("");
};

console.log(decrypt(encrypt("Hello World!")));

您需要根据 set/index 对拆分编码字符串。使用 look-ahead 正则表达式并在 cns 之前拆分很容易。 /(?=[cns])/

const chars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".split("");
const numbs = "0123456789".split("");
const symbols = "!@#$%^&*()_+-=[]{}|;':,./<>?\" ".split("");

function encrypt(message) {
  message = message.split("")
  var output = []
  message.forEach(element => {
    if (chars.includes(element)) {
      output.push("c" + chars.indexOf(element))
    } else if (numbs.includes(element)) {
      output.push("n" + numbs.indexOf(element))
    } else if (symbols.includes(element)) {
      output.push("s" + symbols.indexOf(element))
    } else {
      console.log(element)
      throw new Error(`Unknown character`)
    }
  });
  return output.join("")
}

function decrypt(message) {
  message = message.split(/(?=[cns])/);
  var output = [];
  message.forEach(element => {
    let set;
    switch(element[0]){
      case 'c':
        set = chars;
        break;
      case 'n':
        set = numbs;
        break;
      case 's':
        set = symbols;
        break;
    }
    const index = parseInt(element.substring(1));
    output.push(set[index]);
  });
  return output.join('');
}
const encrypted = encrypt("Hello World!");
console.log(encrypted);
//expected to log Hello World! but logs H and when starting the message with a symbol or number it just logs nothing
console.log(decrypt(encrypted));