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