Javascript 几个简单函数中的加密
嗨,我写了这个程序,但是当我按下按钮时什么也没发生,你知道为什么吗Javascript 几个简单函数中的加密,javascript,Javascript,嗨,我写了这个程序,但是当我按下按钮时什么也没发生,你知道为什么吗 <HTML> <HEAD> <TITLE> Alberti's Disks </TITLE> <SCRIPT LANGUAGE = "JavaScript"> var plainArray = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
<HTML>
<HEAD>
<TITLE>
Alberti's Disks
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
var plainArray = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
var cipherArray = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
//this function shifts all elements of the array
function right(letterArray)
{
var rightShiftArray = new Array (26);
for (var count = 0; count < 25; count++)
{
rightShiftArray[count + 1] = letterArray[count];
}
rightShiftArray[0] = letterArray[25];
return rightShiftArray;
}
//this function rotates cipher alphabet
function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
var rotateArray = new Array (26);
rotateArray = cipherAlphabet;
while (rotateArray[0] != indexCharacter)
{
rotateArray = right(rotateArray);
}
return rotateArray;
}
//this function encrypts given word using index character
function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
var encryptedString = signalCharacter;
//i is what will hold the results of the encrpytion until it can be appended to encryptedString
var i;
// rotate array to signal character position
var rotateArray = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);
for (var count = 0; count < plainText.length; count++)
{
var singleLetter = plainText.charAt(count);
i = cipherAlphabet[singleLetter];
encryptedString = encryptedString + rotateArray[i];
}
return encryptedString;
}
function testEncryption()
{
// encrypts word JAVASCRIPT using given index characters
var codeText;
codeText = encrypt('JAVASCRIPT', 'X', 'n', plainArray, cipherArray);
window.alert(codeText);
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME = "Form">
<P>
<INPUT TYPE = "button" NAME = "testButton" VALUE ="Test Encryption"
ONCLICK = " testEncryption() ;">
</P>
</FORM>
</BODY>
</HTML>
阿尔贝蒂氏盘
var plainArray=['A'、'B'、'C'、'D'、'E'、'F'、'G'、'H'、'I'、'J'、'K'、'L'、'M'、'N'、'O'、'P'、'Q'、'R'、'S'、'T'、'U'、'V'、'W'、'X'、'Y'、'Z'];
变量Cipherray=['a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'n'、'o'、'p'、'q'、'r'、's'、't'、'u'、'v'、'w'、'x'、'y'、'z'];
//此函数用于移动数组的所有元素
函数右(字母数组)
{
var rightShiftArray=新数组(26);
对于(变量计数=0;计数<25;计数++)
{
rightShiftArray[count+1]=letterArray[count];
}
rightShiftArray[0]=字母数组[25];
返回右移位tarray;
}
//此函数用于旋转密码字母表
函数旋转位置(信号字符、索引字符、普通字母、密码)
{
var rotatarray=新阵列(26);
rotateArray=环磷酰胺;
while(rotateArray[0]!=indexCharacter)
{
rotateArray=右侧(rotateArray);
}
返回旋转耳道;
}
//此函数使用索引字符加密给定的字
函数加密(明文、信号字符、索引字符、明文、密码)
{
var encryptedString=信号字符;
//i将保存加密的结果,直到它可以附加到encryptedString
var i;
//将数组旋转到信号字符位置
var ROTATERRAY=旋转位置(信号字符、索引字符、普通字母、密码字符);
对于(var count=0;count
您在testEncryption
中没有右大括号(}
)。这是一个开始。您缺少testEncryption()
上的右大括号
函数测试加密()
{
//使用给定的索引字符加密word JAVASCRIPT
var代码文本;
codeText=encrypt('JAVASCRIPT','X','n',plainArray,cipherray);
window.alert(代码文本);
}//我可以在代码中看到两个明显的错误。由于这可能是家庭作业,我将只提供检查的指针:
确保声明的变量名与用于访问它们的变量名匹配。我至少可以看到一个不匹配
在访问数组成员时要非常小心。我至少可以看到一个位置,其中数组中的索引无法访问您认为它将访问的内容
还有一条评论——在代码运行之后:逐个旋转数组直到它正确为止,这是获得所需内容的一种非常缓慢的方法。您可能希望实现“按n旋转”功能。当然,您还需要编写一个函数来计算n
值。为testEncryption
函数添加右括号}
可能是一个开始。那么,您想做什么呢?什么有效,什么无效?什么错误消息(如果有)?跨浏览器还是特定于浏览器?“它坏了,哈尔普!”对这个问题不是一个很有用的描述。好的,我在测试加密后添加了右括号,但我现在得到了一个错误:XundefinedDundDefinedDundDefinedDundDefinedDundDefinedDundDefinedDundDefinedDundDefinedDundDefinedDundDefinedAnyone?我真的卡住了!谢谢
function testEncryption()
{
// encrypts word JAVASCRIPT using given index characters
var codeText;
codeText = encrypt('JAVASCRIPT', 'X', 'n', plainArray, cipherArray);
window.alert(codeText);
} // <-- See here