JavaScript 解密函数
我正在尝试用 JavaScript 编写一个简单的解密函数,该函数将接受输入字符串并遍历 ASCII 字母表以查找代码的所有 26 种变体。我知道如何进行正常解密,但它只迭代一次,并且只给出一个变体,而不是全部 26 个变体。我该如何更改它?
var count = 0;
function inputData(buttonPress)
{
var stringData = document.getElementById("stringData").value;
var splitStr = stringData.toLowerCase();
var sendStr = (splitStr).split("");
shift= 26;
decrypt(sendStr, shift);
}
function decrypt(newStr, shift)
{
if(count < newStr.length)
{
var strAscii = newStr[count].charCodeAt(0);
strAscii=parseInt(strAscii);
var newStrAscii= ((strAscii -97 -shift) % 26) + 97;
newStr[count] = String.fromCharCode(newStrAscii);
count++;
decrypt(newString,shift-1);
}
newStr= newStr.join("");
alert(newStr);
}
I am trying to write a simple decrypt function in JavaScript that would take an input string of characters and go through the alphabet in ASCII to find all the 26 variations of the code. I know how to do normal decryption but it is only iterating through once and only giving one variation and not all 26. How do I change it?
var count = 0;
function inputData(buttonPress)
{
var stringData = document.getElementById("stringData").value;
var splitStr = stringData.toLowerCase();
var sendStr = (splitStr).split("");
shift= 26;
decrypt(sendStr, shift);
}
function decrypt(newStr, shift)
{
if(count < newStr.length)
{
var strAscii = newStr[count].charCodeAt(0);
strAscii=parseInt(strAscii);
var newStrAscii= ((strAscii -97 -shift) % 26) + 97;
newStr[count] = String.fromCharCode(newStrAscii);
count++;
decrypt(newString,shift-1);
}
newStr= newStr.join("");
alert(newStr);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您拥有的功能仅适用于 ROT13。如果字母的偏移量只是 +1,则可以使用 for 循环,每次获取之前的输出并一次又一次地传递它。
这是我能想到的最短、最优雅的编码方式:
结果:
编辑:现在按请求递归:
更优雅的是创建一个函数 shiftLetter(letter,offset=1), caesarShiftBy(text,offset=1) ,然后在 1,2,...26 范围内映射 caesarShifyBy(text=text,N) 的柯里化版本(但是没有 jquery 的 javascript 还没有很好的原语来处理这个东西)。
I will assume that the function you have only does ROT13. If it was just +1 to the offset of the letter, you could just use a for loop, where each time you take your previous output and pass it through again and again.
Here's the shortest and most elegant way I could think of to code this:
Resulting in:
edit: now recursive by request:
More elegant would be to make a function shiftLetter(letter,offset=1), caesarShiftBy(text,offset=1), and then map a curried version of caesarShifyBy(text=text,N) over the range 1,2,...26 (but javascript without jquery doesn't have nice primitives for this stuff yet).
要将字符串中的所有数字字符实体转换为其等效字符,可以执行以下操作:
str.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); } )
To convert all numerical character entities in a string to their character equivalents you can do this:
str.replace(/&#(\d+);/g, function (m, n) { return String.fromCharCode(n); })