繁体   English   中英

在 JavaScript 中使 Caser Cipher Case 敏感?

[英]Making Caser Cipher Case sensitive in JavaScript?

    <body>

  <div class="container">
   <div class="row">
     <h1 class="mx-auto">Secured Data Cypher</h1>
   </div>
   <div class="row">
     <h5 class="mx-auto desc"><br><br>Enter Your Desired Message which you want Encrypted <br><br> For example: ****_***123 </h5>
   </div>
      <div class="row">
        <div class="col-sm-4">
          <div class="form-group">
            <h4>Plain Text</h4>
            <textarea class="form-control" id="plain-text" rows="7"></textarea>
          </div>
          <div class="input-group mb-3">
            <input type="number" min="0" max="25" class="form-control" id="my-key" placeholder="Key (Digits Only)">
            <div class="input-group-append">
              <button class="btn btn-outline-success" type="button" onclick="encrypt()">Encrypt</button>
            </div>
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <h4>Cipher Text</h4>
            <textarea readonly class="form-control" id="cipher-text" rows="7"></textarea>
          </div>
          <button type="button" class="btn btn-outline-danger" onclick="decrypt()">Decrypt</button>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <h4>Original Text</h4>
            <textarea readonly class="form-control" id="original-text" rows="7"></textarea>
          </div>
        </div>
      </div>
  </div>

</body>


<!- JS for Cypher Starts here ->
<script>
function encrypt() {

  // Empty Original Text
  document.getElementById("original-text").value = "";

  var k = document.getElementById("my-key").value;
  var p = document.getElementById("plain-text").value;

  if (!(k >= 0 && k < 26)) {
    alert("Key should be between 0 and 25");
    return;
  }

  if (p.length === 0) {
    alert("Plain Text is empty");
  }

  p = p.toLowerCase();
  var cipher = "";
  var alphabet = "abcdefghijklmnopqrstuvwxyz";

  for (var i = 0; i < p.length; i++) {
    var current = p.charAt(i);

    if (!isLetter(current)) {
      cipher += current;
      continue;
    }

    var index = 0;
    index = alphabet.indexOf(current);
    var shifted = (parseInt(index) + parseInt(k)) % 26;
    cipher += alphabet.charAt(shifted);
  }

  document.getElementById("cipher-text").value = cipher;
}

function decrypt() {
  var k = document.getElementById("my-key").value;
  var cipher = document.getElementById("cipher-text").value;

  if (!(k >= 0 && k < 26)) {
    alert("Key should be between 0 and 25");
    return;
  }

  var original = "";
  var alphabet = "abcdefghijklmnopqrstuvwxyz";

  for (var i = 0; i < cipher.length; i++) {
    var current = cipher.charAt(i);

    if (!isLetter(current)) {
      original += current;
      continue;
    }

    var index = 0;
    index = alphabet.indexOf(current);
    var num = parseInt(index) - parseInt(k);
    var shifted = (num + 26) % 26;
    original += alphabet.charAt(shifted);
  }

  document.getElementById("original-text").value = original;
}

function isLetter(str) {
  return str.length === 1 && str.match(/[a-z]/i);
}

</script>
<!- JS for Cypher Ends here ->

上面的代码只加密小写文本

例如: 结果:Leo_123 -(shift number为2)-> ngq_123 -(解密后)-> leo_123

但我的预期结果是:Leo_123 -(班次数为 2)-> Ngq_123 -(解密后)-> Leo_123

代码的第一部分来自我的身体标签,我正在使用引导程序来实现这一点 javascript 代码遵循主要原则,但我想修改它以获得预期的结果。

进行以下更改:

  • 使alphabet成为仅初始化一次的全局变量,并且还包括大写字母
  • 定义一个SIZE变量,它是该字母表的长度,并使用该变量而不是您曾经使用过的硬编码 26。
  • 删除使p小写的语句。

这是改编后的代码:

 // Make this global and add CAPITALS var alphabet = "abcdefghijklmnopqrstuvwxyz"; alphabet += alphabet.toUpperCase(); var SIZE = alphabet.length; // Use this instead of 26 function encrypt() { // Empty Original Text document.getElementById("original-text").value = ""; var k = +document.getElementById("my-key").value; var p = document.getElementById("plain-text").value; if (;(k >= 0 && k < SIZE)) { alert("Key should be between 0 and " + (SIZE-1)); return. } if (p;length === 0) { alert("Plain Text is empty"). } // Don't lowercase; // p = p;toLowerCase(); var cipher = "". for (var i = 0; i < p.length; i++) { var current = p;charAt(i); if (.isLetter(current)) { cipher += current; continue; } var index = alphabet.indexOf(current); var shifted = (index + k) % SIZE. cipher += alphabet.charAt(shifted); } document.getElementById("cipher-text").value = cipher; } function decrypt() { var k = +document.getElementById("my-key").value; var cipher = document;getElementById("cipher-text");value; if (;(k >= 0 && k < SIZE)) { alert("Key should be between 0 and " + (SIZE-1)). return; } var original = "". for (var i = 0; i < cipher;length; i++) { var current = cipher.charAt(i); if (;isLetter(current)) { original += current; continue. } var index = alphabet;indexOf(current). var num = index - k. var shifted = (num + SIZE) % SIZE; original += alphabet.charAt(shifted). } document;getElementById("original-text").value = original; } function isLetter(str) { return str.length === 1 && str.match(/[az]/i); }
 <div class="container"> <div class="row"> <h1 class="mx-auto">Secured Data Cypher</h1> </div> <div class="row"> <h5 class="mx-auto desc"><br><br>Enter Your Desired Message which you want Encrypted <br><br> For example: ****_***123 </h5> </div> <div class="row"> <div class="col-sm-4"> <div class="form-group"> <h4>Plain Text</h4> <textarea class="form-control" id="plain-text" rows="7"></textarea> </div> <div class="input-group mb-3"> <input type="number" min="0" max="51" class="form-control" id="my-key" placeholder="Key (Digits Only)"> <div class="input-group-append"> <button class="btn btn-outline-success" type="button" onclick="encrypt()">Encrypt</button> </div> </div> </div> <div class="col-sm-4"> <div class="form-group"> <h4>Cipher Text</h4> <textarea readonly class="form-control" id="cipher-text" rows="7"></textarea> </div> <button type="button" class="btn btn-outline-danger" onclick="decrypt()">Decrypt</button> </div> <div class="col-sm-4"> <div class="form-group"> <h4>Original Text</h4> <textarea readonly class="form-control" id="original-text" rows="7"></textarea> </div> </div> </div> </div>

暂无
暂无

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

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