2016-04-29 6 views
0

私はシーザー暗号をベースにした私のユニ割り当てのための暗号化装置&を作成しようとしています。シーザー暗号の文字列の長さの問題

私はきちんとしたスタートをしましたが、文字列の長さを返そうとするたびに、各文字のために暗号化器をループできるようにするたびにnullとして返します。

は、ここに私のコード

EDIT:私は私がインラインCSSを使用しています知っている

<!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> 

を作品完成したコードが含まれている、これは単なる一時的なものであり、ここで

答えて

0

があなたの固定され関数:

function enrypt() { 
    var text, additon, encrypted, numb; 
    text, encrypted = ""; 

    text  = document.getElementById('txt').value; 
    addition = Math.floor(Math.random() * (122 - 97 + 1)) + 97; 

    window.alert('Your encryption key is: ' + addition); 
    window.alert('Your original word is: ' + text); 

    for (i = 0; i < text.length; i++) { 
    numb = text.charCodeAt(i); 
    key = addition + numb; 
    encrypted += String.fromCharCode(key) 
    } 

    window.alert('Your encrypted word is: ' + encrypted); 
} 

旧答え:

あなたが初期化されていない変数txtの値を読み取ろうとしている。

letter = document.getElementById(txt.value); 

あなたはおそらく最初にそれを初期化したい:

txt = document.getElementById("txt"); 
letter = document.getElementById(txt.value); 

また、をチェックDOMエレメントではなく、入力テキストの長さの長さ:

while (i < txt.value.length) { 
+0

ありがとうございました。まだ定義されていない文字列の長さを取得する – jkb114

+0

私はあなたのコードをさらに見て、私はあなたが文字列の長さをチェックしていないことに気づいたが、DOM要素。 'txt.value.length'をチェックしてください。私の答えを更新しました。 – Uzbekjon

+0

もう一度ありがとうございます。今ではnullのプロパティ 'charCodeAt'を読み取ることはできません。私は値0で変数 "i"を設定しているので、最初の位置0でそれを読み取るはずなので、わかりません。右? – jkb114

関連する問題