使用javascript加密和解密仿射编码的单词

时间:2022-05-02 18:23:40

I am trying to code an Affine Encryption Algorithm in JavaSCript.

我试图在JavaSCript中编写仿射加密算法。

To begin with I have inserted the alphabet inside a matrix :

首先,我在矩阵中插入了字母:

alpha =['A','B','C','Ç','D','E','Ë','F','G','H','I','J','K','L','M','N'];

The number of characters is 90 , so the length of this matrix is 90 and the module for the algorithm is mod(90).

字符数为90,因此该矩阵的长度为90,算法的模块为mod(90)。

Also I used a = 29 because (29,90)=1 and a−1 = 59.

我也使用a = 29因为(29,90)= 1而a-1 = 59。

The function of encryption includes 3 parameters on it :

加密功能包括3个参数:

 encrypt(a, b,word) 

and inside it I used a for() loop to cycle each character of the inserted word.

在其中我使用了for()循环来循环插入单词的每个字符。

So this for loop searches for the characters in the alphabet matrix, gets the index and than uses the encryption function E(x)=(a*x+b)mod(c).

因此,for循环搜索字母矩阵中的字符,获取索引,然后使用加密函数E(x)=(a * x + b)mod(c)。

function encrypt(a, b, word) {

    for (var i = 0; i < word.length; i++) {

        var alphaIndex = alpha.indexOf(word[i]);

        var troublesome = (a * alphaIndex + b) % alpha.length;

        word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
    }
    return word;
}

However this function returns a coded word but when I apply the decrytpion.

但是,当我应用解密时,此函数返回一个编码字。

function it doesn't return the deciphered word D(y) = a−1(y − b) mod(c).

函数它不返回解密字D(y)= a-1(y - b)mod(c)。

The function of the decryption is :

解密的功能是:

function decrypt(a, b, word) {

    for (var i = 0; i < word.length; i++) {
        a %= alpha.length;

        //Bruteforce the modular invert of the a

        for (var j = 1; j < alpha.length; j++) {
            if ((a * j) % alpha.length == 1)
                var invert = j;
        }

        var alphaIndex = alpha.indexOf(word[i]);

        var troublesome = (invert * (alphaIndex - b)) % alpha.length;
        if (troublesome < 0)
            troublesome += alpha.length;
        word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
    }
    return word;
}

1 个解决方案

#1


0  

There was just one mistake that i should have known since the beginning.
When i get a , b and the word via document.getElementbyId() , the value
is in a String state sot thats why i was not getting the right numbers.

从一开始我就应该知道一个错误。当我通过document.getElementbyId()获得a,b和单词时,该值处于String状态,这就是为什么我没有得到正确的数字。

a = parseInt(a);
b = parseInt(b); 

This two lines above are the solution.

以上两行是解决方案。

function encrypt(a, b, word) {

a = parseInt(a);
b = parseInt(b); 

    for (var i = 0; i < word.length; i++) {

        var alphaIndex = alpha.indexOf(word[i]);

        var troublesome = (a * alphaIndex + b) % alpha.length;

        word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
    }
    return word;
}


    function decrypt(a, b, word) {

    a = parseInt(a);
    b = parseInt(b); 

        for (var i = 0; i < word.length; i++) {
            a %= alpha.length;

            //Bruteforce the modular invert of the a

            for (var j = 1; j < alpha.length; j++) {
                if ((a * j) % alpha.length == 1)
                    var invert = j;
            }

            var alphaIndex = alpha.indexOf(word[i]);

            var troublesome = (invert * (alphaIndex - b)) % alpha.length;
            if (troublesome < 0)
                troublesome += alpha.length;
            word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
        }
        return word;
    }

#1


0  

There was just one mistake that i should have known since the beginning.
When i get a , b and the word via document.getElementbyId() , the value
is in a String state sot thats why i was not getting the right numbers.

从一开始我就应该知道一个错误。当我通过document.getElementbyId()获得a,b和单词时,该值处于String状态,这就是为什么我没有得到正确的数字。

a = parseInt(a);
b = parseInt(b); 

This two lines above are the solution.

以上两行是解决方案。

function encrypt(a, b, word) {

a = parseInt(a);
b = parseInt(b); 

    for (var i = 0; i < word.length; i++) {

        var alphaIndex = alpha.indexOf(word[i]);

        var troublesome = (a * alphaIndex + b) % alpha.length;

        word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
    }
    return word;
}


    function decrypt(a, b, word) {

    a = parseInt(a);
    b = parseInt(b); 

        for (var i = 0; i < word.length; i++) {
            a %= alpha.length;

            //Bruteforce the modular invert of the a

            for (var j = 1; j < alpha.length; j++) {
                if ((a * j) % alpha.length == 1)
                    var invert = j;
            }

            var alphaIndex = alpha.indexOf(word[i]);

            var troublesome = (invert * (alphaIndex - b)) % alpha.length;
            if (troublesome < 0)
                troublesome += alpha.length;
            word = word.substring(0, i) + alpha[troublesome] + word.substring(i + 1);
        }
        return word;
    }