Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

javascript - Setting up a for loop for an encryption scheme

As I stated before I am trying to make a specific for loop for an encryption machine (a toy machine, not one that is secure from the NSA).

When I press the encrypt button, I need the encryption to run and then produce the info encrypted in another text box. So I have a text box where the user enters the numbers to be encrypted (between 10000 and 99999) and then press encrypt and the info shows up in the second bar. I have the set up but the for loop is tricky.

These are my directions:

Add the necessary code to the “Encrypt” button to do the following:

a) Create a for loop to add 10 to the number entered on the first box and multiply the result by 3, add 20 to this result and then multiply it by 5, add 30 to this result and then multiply it by 7, etc. Follow that pattern 5 times (5 iterations).

b) After the iterations have been completed, there will be a resulting number in memory, let's say 75432179

c) Now, this number needs to be turned into characters (letters) by matching each digit to its corresponding letter of the alphabet based on the positions of the letters (0 will be matched with the 10th letter of the alphabet). For our example: the resulting letters will be: gedcbagi (g is the 7th letter of the alphabet, e is the 5th letter, d is the 4th letter, etc.)

d) The last step of the encryption process is to further scramble the letters by using the ancient Caesar's cipher: each letter replaced by another letter three positions to the right. Therefore, the final result in our example would be: jhgfedjl (Notice that you may also do steps c) and d) combined)

This is what I have so far for my script tags; please tell me what I'm doing wrong:

       <script type="text/javascript">
    q=1
for (encryptThis=1; encryptThis <=5; encryptThis++){        
        if (encryptThis>=10000 && encryptThis<=99999){
        encryptinfo=((q+2)*10+encryptThis);
        }else{
        alert("number should be between 10000 and 99999");
        }}
    </script>

then for the bottom of my table by my inputs:

<tr>
    <td>Plaintext (Plain information)</td>
    <td><input type="text"  name= "encryptThis" size="16" onchange=' '/></td>
    <td><input type="button" value=" Encrypt " onclick='
        system.out.encryptinfo.print((q+2)*10+encryptThis);


       '/></td>

and....

<tr>
    <td>Ciphertext (Encrypted information)</td>
    <td><input type="text"  name= "encryptinfo" size="16" onchange=' '/></td>
    <td></td>
</tr>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an interpretation of what I think you're trying to do:

function encrypt(num) {
    var sum = 0, str, i, result, index;
    var chars = "abcdefghijklmnop";
    var charBase = "0".charCodeAt(0);
    for (i = 0; i < 5; i++) {
        // ((2 * i) + 3) goes 3,5,7,9,11
        // ((i * 10) + 10) goes 10,20,30,40,50
        sum += (num * ((2 * i) + 3)) + ((i * 10) + 10);        
    }
    // convert num to string to get digits
    str = sum + "";
    result = "";
    for (i = 0; i < str.length; i++) {
        // get offset from "0" and add 3 for the cipher
        index = str.charCodeAt(i) - charBase + 3;
        // convert that index to a character
        result += chars.charAt(index);
    }
    return result;
}

It takes a number as an argument and it returns a string.

Working demo: http://jsfiddle.net/jfriend00/UW245/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...