0
我正在嘗試爲基於凱撒密碼的uni分配構建一個加密器&解密器。凱撒密碼字符串長度問題
我做了一個體面的開始,但每當我嘗試返回字符串長度,以便我可以循環每個字符的加密器時,它將返回爲空。
這裏是我的代碼
編輯:我已經包括了完成的代碼,工程
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Encryption version 0</title>
<style>
body {
margin-left: 15%;
margin-right: 15%;
}
#description {
font-style: italic;
font-size: 18px;
}
#instruction {
font-weight: bold;
color: red;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Welcome to the encryptor</h1>
<p id="description">Please type below the text you wish to encript</p>
<input name="txt" type="text" maxlength="512" id="txt" />
<p>And pick an encryption key</p>
<input name="key1" type="text" maxlength="512" id="key1" />
<button onclick= "enrypt()">Submit</button>
<p id="word"></p>
<h1>Welcome to the decrypter</h1>
<p id="description">Please type below the text you wish to decrypt</p>
<input name="txt2" type="text" maxlength="512" id="txt2" />
<p>And your encryption key</p>
<input name="key" type="text" maxlength="512" id="key" />
<button onclick= "decrypt()">Submit</button>
<p id="word2"></p>
<script type="text/javascript">
//Script 1
function enrypt() {
var text, additon, encrypted, numb;
text, encrypted = "";
text = document.getElementById('txt').value;
additon = document.getElementById('key1').value;
for (i = 0; i < text.length; i++) {
numb = text.charCodeAt(i);
key = parseFloat(additon) + parseFloat(numb);
encrypted += String.fromCharCode(key)
}
document.getElementById("word").innerHTML = 'Your encrypted word is: ' + encrypted;
}
//Script 2
function decrypt() {
var text2, additon2, encrypted2, numb2;
text2, encrypted2 = "";
text2 = document.getElementById('txt2').value;
addition2 = document.getElementById('key').value;
for (i = 0; i < text2.length; i++) {
numb2 = text2.charCodeAt(i);
key = parseFloat(numb2) - parseFloat(addition2);
encrypted2 += String.fromCharCode(key)
}
document.getElementById("word2").innerHTML = 'Your encrypted word is: ' + encrypted2;
}
</script>
</body>
</html>
我知道我使用內聯CSS,這只是暫時的
謝謝,我應該想到這一點。仍然得到未定義的字符串長度 – jkb114
我看到了,然後進一步查看你的代碼,我注意到你沒有檢查字符串的長度,而是DOM元素。檢查'txt.value.length'。更新了我的答案。 – Uzbekjon
再次感謝。現在它說無法讀取null的屬性'charCodeAt'。我不明白,因爲我將變量「i」的值設置爲0,所以它應該在第一個位置讀取它。對? – jkb114