建立一个for循环的加密方案 [英] Setting up a for loop for an encryption scheme

查看:173
本文介绍了建立一个for循环的加密方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我 我试图做具体<$如前所述C $ C>为回路加密机(玩具机器,而不是一个是安全与NSA)。

当我preSS的加密按钮,我需要的加密运行,然后产生另一个文本框加密的信息。所以我有用户在其中输入(10000和99999之间),要加密的数字的文本框,然后preSS加密和信息在第二栏中显示出来。我已经设定了,但for循环是棘手的。

这是我的路线:

添加必要的code将加密按钮,执行以下操作:

a)创建一个用于循环要添加10到所述第一框中输入的号码和乘以
由3造成,加入20到这一结果,然后乘以5,添加30到这一结果,然后
7乘以等按照这种模式5次(5次迭代)。

b)该迭代已经完成后,将有一个所得数
记忆,让我们说75432179

c)现在,这个数字需要通过每个数字匹配变成字符(字母)
到基于字母的位置的字母表中的相应的字母(0将
与字母表的字母10匹配)。在我们的例子:生成的字母
将是:gedcbagi(g是字母表中的7号信令,e为第五字,d为第四
信等)

d)该加密过程​​的最后一步是通过使用以进一步加扰信
古老的凯撒密码:每个字母被另一个字母替代,三个位置的
对。因此,最后的结果在我们的例子是:jhgfedjl(请注意您
还可以做的步骤c)和d)联合)

这是我至今对我的脚本标记;请告诉我,我在做什么错了:

 &LT;脚本类型=文/ JavaScript的&GT;
    Q = 1
为(encryptThis = 1; encryptThis&下; = 5; encryptThis ++){
        如果(encryptThis&GT; = 10000&放大器;&安培; encryptThis&LT; = 99999){
        encryptinfo =((Q + 2)* 10 + encryptThis);
        }其他{
        警报(编号应为10000和99999之间);
        }}
    &LT; / SCRIPT&GT;

然后我的表在我投入的底部:

 &LT; TR&GT;
    &LT; TD&GT;明文(普通信息)LT; / TD&GT;
    &LT; TD&GT;&LT;输入类型=文本名称=encryptThis大小=16的onchange =''/&GT;&LT; / TD&GT;
    &LT; TD&GT;&LT;输入类型=按钮值=加密的onclick ='
        system.out.encryptinfo.print((Q + 2)* 10 + encryptThis);
       /&GT;&LT; / TD&GT;

和....

 &LT; TR&GT;
    &LT; TD&GT;密文(加密信息)LT; / TD&GT;
    &LT; TD&GT;&LT;输入类型=文本名称=encryptinfo大小=16的onchange =''/&GT;&LT; / TD&GT;
    &LT; TD&GT;&LT; / TD&GT;
&LT; / TR&GT;


解决方案

下面是什么,我认为你试图做一个跨pretation:

 函数加密(NUM){
    VAR总和= 0,STR,我,因此,指数;
    VAR字符=abcdefghijklmnop;
    VAR charBase =0.char $ C $猫(0);
    对于(I = 0; I&小于5;我++){
        //((2 * I)+ 3)变为3,5,7,9,11
        //((ⅰ* 10)+ 10)变为10,20,30,40,50
        总和+ =(NUM *((2 * I)+ 3))+((I * 10)+ 10);
    }
    // NUM转换成字符串得到的数字
    海峡=总和+;
    结果=;
    对于(i = 0; I&LT; str.length;我++){
        //从0偏移量和密码加3
        指数= str.char $ C $猫(I) - charBase + 3;
        //该索引转换为字符
        结果+ = chars.charAt(指数);
    }
    返回结果;
}

这需要一个数字作为参数,并返回一个字符串。

工作演示: http://jsfiddle.net/jfriend00/UW245/

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>

解决方案

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/

这篇关于建立一个for循环的加密方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆