你是如何处理在Javascript中恺撒密码的转变? [英] How do you handle the shift on a Caesar cipher in Javascript?

查看:159
本文介绍了你是如何处理在Javascript中恺撒密码的转变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript创建一个恺撒密码,并遇到了麻烦搞清楚如何做到的转变。这可能是因为我的做法是复杂的,但我想知道如何设置添加数字的最大值,但有另外继续从最小编号开始。这里是code我有:

I'm trying to create a caesar cipher in Javascript and having trouble figuring out how to do the shift. It may be that my approach is complicating it, but I'm trying to understand how to set a max value for adding numbers, but have the addition continue starting from the minimum number. Here is the code I have:

$(document).ready(function(){
var alpha = [];
var encoded = [];
alpha = {
    '1': 'a',
    '2': 'b',
    '3': 'c',
    '4': 'd',
    '5': 'e',
    '6': 'f',
    '7': 'g',
    '8': 'h',
    '9': 'i',
    '10': 'j',
    '11': 'k',
    '12': 'l',
    '13': 'm',
    '14': 'n',
    '15': 'o',
    '16': 'p',
    '17': 'q',
    '18': 'r',
    '19': 's',
    '20': 't',
    '21': 'u',
    '22': 'v',
    '23': 'w',
    '24': 'x',
    '25': 'y',
    '26': 'z'
};
$('#cipher_form').submit(function(event){
    event.preventDefault();
    var input = $('#cipher_input').val();
    var key = $('#cipher_key').val();
    var chars = input.split('');
    for(var i = 0; i<chars.length; i++){
        console.log(chars[i]);
        for(var index in alpha){
            if(alpha[index] == input[i]){
                encoded.push(index);
            }
        }
    }

    for(var i=0; i<encoded.length; i++){
        alert(encoded[i]);

    }

});
});

所以当表单提交,我抓住了输入值,并把它变成一个字符数组。然后,我遍历我的对象,并发现该字符匹配的键值对,推动关键另一个数组。我的计划是遍历我的钥匙的阵列和CIPHER_KEY添加到该号码。

So when the form submits, I'm grabbing the input value, and turning it into an array of characters. Then I loop through my object and find the key value pair that matches the character and push the key to another array. My plan was to loop through my array of keys and add the cipher_key to that number.

因此​​,如果字母'O'是'15'在我的对象,我CIPHER_KEY是5,这将增加15 + 5和20这将是'T'结束。我看到的问题是,如果我的信是'Z',并加入26 + 5,你最终会与31,它并不在我的对象存在。

So if the letter 'o' is '15' in my object, and my cipher_key is '5', it would add 15+5 and end up with '20' which would be 't'. The problem I see is if my letter is 'z' and you add 26 + 5, you'll end up with 31 which doesn't exist in my object.

我的挑战是找出如何在CIPHER_KEY添加到我的号一直到26,但是,如果它超过26,复位从1开始,并继续增加。因此,如果字母是Z和CIPHER_KEY是5,将采取26 + 5和'5'端为字母E。

My challenge is finding out how to add the cipher_key to my number all the way to 26, but if it goes over 26, reset and start at 1 and continue the addition. So if the letter is 'z' and the cipher_key is '5' it would take 26+5 and end up at '5' for the letter 'e'.

这是可能的吗?或者是我的方法完全错误的吗?

Is this possible to do? Or is my approach completely wrong on this?

推荐答案

只是在简单的数组的数字,但为了。结果
VAR阿尔法= ['一','B','C',......'Z'] 结果
获取其数值为 VAR值= alpha.indexOf(the_number); 结果
然后,不过去的量使用添加密钥和prevent。结果
VAR newNumValue =(值+键)%26;
我想这就是它。它涵盖了问题。除此之外, - 你的方法还是不错的,是的。结果
不同的方法结果
此外 - 如果你的文字中可能包含一些特殊符号,我会建议采取从 ASCII表的初始值。它将覆盖所有的符号和不同小写和大写字母。而你并不需要存储阵列。结果
VAR值= yourChar.char $ C $猫(0); 结果
为了得到符号从ASCII code回来,去 VAR符号= value.fromChar code(); 结果
希望这有助于。

Just have the numbers in simple array, but in order.
var alpha = ['a', 'b', 'c', ... 'z']
Get its number value as var value = alpha.indexOf(the_number);
Then add the cipher key and prevent from going over your amount by using %.
var newNumValue = (value + key) % 26; I guess that's it. It covers the question. Apart from that - your approach was good, yes.
Different approach
Also - if your text might contain some special symbols, i would suggest taking the initial value from ASCII table. It will cover all the symbols plus differ lowercase and uppercase letters. And you don't need to store that array.
var value = yourChar.charCodeAt(0);
To get symbol back from the ascii code, go var symbol = value.fromCharCode();
Hope this helps.

这篇关于你是如何处理在Javascript中恺撒密码的转变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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