[英]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.