-->

解释V @ genere加密功能(Explaining functions of Vigenère

2019-10-28 20:39发布

我发现下面的代码http://rosettacode.org的V @ genere加密和我想它已了解更好。

有人能解释我什么代码在单一线路function ordA(a)function(a)吗?

function ordA(a) {
  return a.charCodeAt(0) - 65;
}

// vigenere
function vigenere2(text, key, decode) {
  var i = 0, b;
  key = key.toUpperCase().replace(/[^A-Z]/g, '');
  return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, function(a) {
    b = key[i++ % key.length];
    return String.fromCharCode(((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65));
  });
}

Answer 1:

我不知道这应该是示例代码,但它主要是显示了如何不编程。 聪明的决定, 正在作出,但很明显的问题分解,变量的命名和文档很不理想了很多。 重复的代码,错综复杂的线条,不明原因的代码片段,这样的例子不胜枚举。 解码是布尔值,但加密的相反的是不解密解码。 此代码是为了明白是怎么回事; 它所做的罗塞塔网站是令人难以置信的在这方面。


返回英文字母或ABC的指数,假设大写字符,0到25,而不是1到26(因为你可以用一个基于索引做模块化计算零索引,不)

return a.charCodeAt(0) - 65;

函数定义,需要一个明文或密文,其可以是比所述明文和布尔小,以指示如果我们编码或解码的一个关键

function vigenere2(text, key, decode) 

指数在明文和变量b,其将保持所述键的字符的索引

var i = 0, b;

转换的关键,大写字母,大写字母不会删除所有字符,以及

key = key.toUpperCase().replace(/[^A-Z]/g, '');

这条线过长明显; 其转换为大写的文本,再除去非字母字符

然后它替换使用的第二个参数中定义的功能的字符串中的字符replace

return text.toUpperCase().replace(/[^A-Z]/g, '').replace(/[A-Z]/g, function(a) {

采取循环方式的关键的下一个字符,使用模运算符,之后更新索引

b = key[i++ % key.length];

太多的事情就到这里,非常糟糕的计划分解; 在执行顺序:

  • (decode ? 26 - ordA(b) : ordA(b))在更新的明文字符的索引的范围内计算的数; 使用用于解密的相反值(错误地称为“解码”在这里)
  • (ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26执行加法与所计算的数量,减少到0至25(即,当到达Ž继续A和反之亦然)
  • ((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65)添加65因此指数被转换回的大写字符的ASCII索引,使用两个完全杂散括号
  • 最后,返回从一个字符码结果的字符串,否则+将除了代替级联
return String.fromCharCode(((ordA(a) + (decode ? 26 - ordA(b) : ordA(b))) % 26 + 65));

好了,它需要结束

  });
}

让我们显示该节目的另一种方式,利用公命名变量,函数重用代码和迫切需要一个名字来解释他们所做的事情正则表达式。

 var ALPHABET_SIZE = 'Z'.charCodeAt(0) - 'A'.charCodeAt(0) + 1; var encrypted = vigenere(false, "B", "Zaphod Breeblebox"); document.body.append('<div>' + encrypted + '</div>'); var decrypted = vigenere(true, "B", encrypted); document.body.append('<div>' + decrypted + '</div>'); function vigenere(decrypt, key, text) { key = toJustUppercase(key); text = toJustUppercase(text); var textOffset = 0; // iterate over all characters, performing the function on each of them return text.replace(/[AZ]/g, function(textChar) { var keyChar = key[textOffset++ % key.length]; var cryptedChar = substituteCharacter(decrypt, keyChar, textChar); return cryptedChar; }); } function substituteCharacter(decrypt, keyChar, textChar) { var keyIndex = charToABCIndex(keyChar); if (decrypt) { // create the opposite of the encryption key index keyIndex = ALPHABET_SIZE - keyIndex; } var textIndex = charToABCIndex(textChar); // the actual Vigenere substitution, the rest is just indexing and conversion var substitutedIndex = (textIndex + keyIndex) % ALPHABET_SIZE; var substitutedChar = abcIndexToChar(substitutedIndex); return substitutedChar; } function toJustUppercase(text) { return text.toUpperCase().replace(/[^AZ]/g, '') } function charToABCIndex(charValue) { return charValue.charCodeAt(0) - 'A'.charCodeAt(0); } function abcIndexToChar(index) { return String.fromCharCode(index + 'A'.charCodeAt(0)); } 

太多的功能,你说什么? 不是真的,我没有实现的ordchr ,或vigenereEncryptviginereDecrypt ,使其更易于阅读。



文章来源: Explaining functions of Vigenère cipher