简体   繁体   English

简单的JavaScript加密功能

[英]Simple JavaScript encryption function

I have a function which has to change characters from one array to the characters from another. 我有一个功能,必须将一个数组中的字符更改为另一个数组中的字符。 It is kind of simple encryption. 这是一种简单的加密。 I have: 我有:

var plainArray = ['A','B','C',...,'Z'];
var cipherArray = ['a','b','c',...,'z'];
function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)

already working. 已经工作了。 Now I have to write a function which will change given word into encrypted word. 现在,我必须编写一个函数,将给定的单词更改为加密的单词。

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++)
   {
       plainAlphabet = plainText.charAt(count);
       i = cipherAlphabet[plainAlphabet];
       encryptedString = encryptedString + rotateArray[i];
   }

   return encryptedString;
}

This function returns signal character and then a string of errors. 此函数返回信号字符,然后返回一串错误。 Do you know what is wrong? 你知道出什么事了吗

You are overwriting plainAlphabet with one character, thus discarding the alphabet. 您正在用一个字符覆盖plainAlphabet ,从而丢弃了字母。 I guess that's not what you want. 我想那不是你想要的。

However, you only posted the signature of rotateToPosition and not the actual code of it, so I cannot test my solution. 但是,您仅张贴了rotateToPosition的签名,而不是其实际代码,因此我无法测试我的解决方案。

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 plainLetter = plainText.charAt(count);
       i = cipherAlphabet[plainLetter];
       encryptedString = encryptedString + rotateArray[i];
   }

   return encryptedString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM