【问题标题】:Problems with Caesar Cipher Formula involving ASCII Conversion涉及 ASCII 转换的凯撒密码公式问题
【发布时间】:2019-06-10 00:39:42
【问题描述】:

使用上一个问题的修改后,我使用凯撒密码的公式遇到了另一个问题: if 字母 x 和 shift n:

En(x) = (x + n) 模。 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>

【问题讨论】:

    标签: javascript ascii formula caesar-cipher


    【解决方案1】:

    您的最后一个 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];
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      • 1970-01-01
      相关资源
      最近更新 更多