gpt4 book ai didi

javascript - 来自 .charCodeAt() 的 ROT13 密码?

转载 作者:行者123 更新时间:2023-11-28 18:36:46 25 4
gpt4 key购买 nike

我是 javascript 新手,这是基本算法脚本的最后一门类(class)。当我从免费代码营获得前端开发证书的下一部分时,我试图理解这行代码,我想了解一切.我一直在寻找解决方案并找到了这个。我理解注释中的一些内容,但我很难理解公式,这段代码 100% 有效,但我只需要进一步理解。这是代码:

function rot13(str) {

//retCharArray is an Array of character codes for the solution
var rotCharArray = [];

//regular expression for all upper case letter from A to Z
var regEx = /[A-Z]/;

//split str into a character array
str = str.split("");

for (var x in str) { //iterate over each character in the array
//regEx.test(str[x]) will return (true or false) if it maches the regEx or not
if (regEx.test(str[x])) {
// A more general approach
// possible because of modular arithmetic
// and cyclic nature of rot13 transform

// I DON'T CLEARLY UNDERSTAND THIS CODE BELOW
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}
}
//make a string with character codes from an array of character codes
str = String.fromCharCode.apply(String, rotCharArray);
return str;
}

// Change the inputs below to test
rot13("SDASasd");

最佳答案

上面的两个答案只回答了你的问题的一半,但他们并没有真正看到你的问题,即要回答免费代码营挑战,你必须去理解凯撒密码和ROT13 算术。我也遇到了这个问题。

我们一起来看一下,一步步过一遍。 (我知道并不是每个人都在高中学习过这个 - 我没有!):

  // I DON'T CLEARLY UNDERSTAND THIS CODE BELOW
rotCharArray.push((str[x].charCodeAt() - 65 + 13) % 26 + 65);
} else {
rotCharArray.push(str[x].charCodeAt());
}

@bastos.sergio 和 @caulitomaz 的答案都有帮助,但它们仅部分解释,并没有指导您进一步研究:

@bastos.sergio 的

"Get the modulo of 26 (this ensures that the encoding will not encrypt to anything above the 'Z' letter, for instance the 'Z' letter will convert to (90-65+13) % 26 + 65) = 77 'M' Letter. Essentially, it will make the encryption algorithm loop to the beginning."

@caulitomaz 的

In the english alphabet there are 26 characters. That is why you apply the cypher rotation of 13 and then apply the modulo operation.

他们在这里指的是“模块化算术”:

模算术表明,某些数字是“全等的”,也就是说,当对它们应用模运算时,它们共享相同的余数。

例如,2 % 12、14 % 12、26 % 12 = 余数 2。这样,当您的数字必须出现在某个范围内,或者设备只显示某个范围内的数字时,您就可以工作该数字“环回”的位置。

请注意,在我的示例中,使用了 % 12 - 这是 12 小时制时钟可以显示的总小时数。因此,如果现在是凌晨 2 点,并且我想知道 12 小时后的时间,我会添加 12。但是我的时钟无法显示 14:00(不在 12 小时模式下)。 Modulo 12 给我的是真实时间。

示例:现在是凌晨 4 点。我的飞机延误了 29 小时(这是一家糟糕的航空公司)。如何算出新的起飞时间? 4 + 29 % 12 = 上午 9 点。 (如果延迟小于 24,我需要交换 am/pm,但你应该得到图片。)

ROT13

这里我们有一个特殊的情况,即 ROT 13 的旋转密码,其中应用密码两次将得到要编码的原始字符,因为字母表只有 26 个字符,而 13 正好是 26 的一半。

x = ROT13(ROT13(x))

所以实际上,我们在应用或逆向加密时不需要担心加13或减13,我们只需再次应用它即可得到原始数据。

那么,算法如何工作:

  1. 正如已经指出的,我们的大写 A-Z 字符代码的范围是 65 (A) 到 90 (Z)。但我们希望使用模运算来找到新字母落在 0-26 范围内的位置。所以从目标字母的字符代码中减去65:str[x].charCodeAt() - 65

  2. 应用密码:添加 13

    (str[x].charCodeAt() - 65 + 13)

  3. 应用模数来查找“循环”的字母。对我们来说是 % 26(字母表中的字符数):

    (str[x].charCodeAt() - 65 + 13) % 26

  4. 加回 65,这样我们就有了 65...90 范围内的字符代码

    (str[x].charCodeAt() - 65 + 13) % 26 + 65)

工作示例:

ROT13(X)

'X'.charCodeAt(0) = 88

- 65 = 23

+ 13 = 36

%26 = 10

+65 = 75

charCodeFrom(75) = K

ROT13(K)

'K'.charCodateAt(0) = 75

- 65 = 10

+ 13 = 23

%26 = 23

+65 = 88

charCodeFrom(88) = X

来源:

http://betterexplained.com/articles/fun-with-modular-arithmetic/ https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

关于javascript - 来自 .charCodeAt() 的 ROT13 密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com