功能(ascii,a,b,c)ascii代码拼图

时间:2016-11-02 16:37:46

标签: javascript encryption

我正在处理使用以下脚本的代码拼图:

    function(ascii,a,b,c) {
        for(i=0;i<ascii.length;i++) {
            if(i%3==0){ascii[i]=(ascii[i]+a)%256;}
            if(i%3==1){ascii[i]=(ascii[i]+b)%256;}
            if(i%3==2){ascii[i]=(ascii[i]+c)%256;}
        }
        return ascii;
    }

我认为它说:&#39;对于消息中的每个字符(或者如果消息是数组,对于数组中的每个元素i),计算其位置mod3,并根据结果将a,b或c添加到字符的ascii值,并且然后返回那个值mod 256.这是正确的解释吗?

1 个答案:

答案 0 :(得分:1)

似乎是encoding function,其中array代表characters (0-255)。然后按字符a,b, and c by modding by 3指定的长度移动字符。然后输出为modded by 255,因此它仍然是有效的字符值。最后返回结果编码array of numbers representing characters

以下是代码:

function encodeIt(ascii,a,b,c) {
    for(i=0;i<ascii.length;i++) {
        if(i%3==0){ascii[i]=(ascii[i]+a)%256;}
        if(i%3==1){ascii[i]=(ascii[i]+b)%256;}
        if(i%3==2){ascii[i]=(ascii[i]+c)%256;}
    }
    return ascii;
}

function encodeString(myString,myAdjustments)
{
    var myArray = [];
    for(var i=0;i<myString.length;i++)
    {
        myArray.push(myString.charCodeAt(i));
    }
    var myArray = encodeIt(myArray,myAdjustments[0],myAdjustments[1],myAdjustments[2]);
    var myEncodedString = "";
    for(var i=0;i<myArray.length;i++)
    {
        myEncodedString+=String.fromCharCode(myArray[i]);
    }
    return myEncodedString;
}

var encodedString = encodeString("Hello World!",[30,80,-13]);
alert(encodedString);
var decodedString = encodeString(encodedString,[-30,-80,13]);
alert(decodedString);

小提琴:

https://jsfiddle.net/3p79bxv2/