涉及ASCII转换的凯撒密码公式的问题

时间:2019-01-15 23:37:33

标签: javascript ascii formula caesar-cipher

使用上一个问题的修改后,我遇到了另一个使用凯撒密码的公式的问题: 如果字母 x 并移位 n

En(x)=(x + n)mod。 26;

但是,当我尝试从以前的完全防虫的python程序中转移它时,它没有产生相同的结果。代替abcdefghijklmnopqrstuvwxyz-> defghijklmnopqrstuvwxyzabc,它生成abcdefghijklmnopqrstuvwxyz-> defghijklmnopklmnopqrstuvw。 JS代码有什么问题?

var shfBox = document.getElementById("shfBox");
var strBox = document.getElementById("strBox");
var button = document.getElementById("button");

button.addEventListener("click", function(){
  var orgstr = String(strBox.value);
  var orgshf = +Number(shfBox.value);
  var str = orgstr;
  var shf = orgshf;
  var output = "";

  for (var i=0; i < str.length; i++) {
    var asciiValue = str[i].charCodeAt();
    if (asciiValue >= 65 && asciiValue <= 77) {
      output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

    } else if (asciiValue >= 78 && asciiValue <= 90) {
      output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

    } else if (asciiValue >= 97 && asciiValue <= 109) {
      output += String.fromCharCode((asciiValue + shf - 97) % 26 + 97);

    } else if (asciiValue >= 110 && asciiValue <= 122) {
      output += String.fromCharCode((asciiValue - shf - 97) % 26 + 97);

    } else {
      output += str[i];
    }
  }
  
  document.getElementById("output").innerHTML = output;
  return output;
  return str;
});
*{
  box-sizing: border-box;
  margin: 0px;
  padding: 0px;
}


body {
  font-family: Courier;
}


#mainNav {
  font-family: Courier;
  display: flex;
  justify-content: space-between;
  padding: 0px 16px;
  position: fixed;
  width: 100%;
  top: 0px;
  background: #eee;
}

section:nth-of-type(1) {
  margin-top: 84px;
  font-family: Courier;
  height: 220px;
  padding: 16px;
  font-size: 17px;
}

section:nth-of-type(2) {
  font-family: Courier;
  height: 110px;
  padding: 16px;
  font-size: 17px;

}


section:nth-of-type(3) {
  font-family: Courier;
  height: 300px;
  padding: 16px;
  font-size: 17px;
}


footer {
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: darkslategray;
  color: white;
}

#mainNav ul {
  list-style: none;
  display: flex;
  align-items: center;
  width: 400px;
  justify-content: space-around;

}

.button {
  background-color: lightblue;
  font-family: Courier;
  border: none;
  color: white;
  padding: 15px 25px;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
}

.button:hover {
  background-color: skyblue;
}



#mainNav ul li a {
  text-decoration: none;
  font-weight: 500;
  color: #333;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <nav id="mainNav">
      <div class="logo">
        <header style="padding: 20px; font-size: 20px;">
          < Caesar Cipher >
        </header>
      </div>

      <ul>
        <li><a href="https://caesarcipherwebdesign--shiftcipher.repl.co/">Home</a></li>
        <li><a href="https://caesarcipher-about--shiftcipher.repl.co/">About</a></li>
        <li><a href="https://caesarcipher-process--shiftcipher.repl,co/">Process</a></li>
        <li><a href="https://caesarcipher-code--shiftcipher.repl.co/">Code</a></li>
      </ul>
    </nav>


    <section>
      a. Encrypted Phrase
      <pre>
      
      <textarea id="strBox" rows="10;" cols="50"></textarea>
      
      </pre>
    </section>

    <section>
      b. Shift Number
      <pre>
      
      <textarea id="shfBox" rows="2" cols="50"></textarea>
      
      </pre>
    </section>

    <section>
      c. Original Message
      <pre>
        
      <button class="button" id="button">Decipher</button>
      
      <p>Deciphered Code:</p>

      <p id="output"></p>
      </pre>
    </section>

    <footer>This is a flawed website.</footer>
    
    
    <script src="script.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

在最后一个-语句中,您使用的是+而不是else if。另外,我不太确定为什么会有四个不同的案例,而不是两个,因为它们似乎做同样的事情。运作方式如下:

for (var i=0; i < str.length; i++) {
  var asciiValue = str[i].charCodeAt();
  if (asciiValue >= 65 && asciiValue <= 90) {
    output += String.fromCharCode((asciiValue + shf - 65) % 26 + 65);

  } else if (asciiValue >= 97 && asciiValue <= 122) {
    output += String.fromCharCode((asciiValue + shf - 97) % 26 + 97);

  } else {
    output += str[i];
  }
}