JavaScript 解密函数

发布于 2024-11-02 06:39:21 字数 783 浏览 1 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-11-09 06:39:21

我假设您拥有的功能仅适用于 ROT13。如果字母的偏移量只是 +1,则可以使用 for 循环,每次获取之前的输出并一次又一次地传递它。

这是我能想到的最短、最优雅的编码方式:

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
function nextLetter(letter) {
    var index = alphabet.indexOf(letter)
    return alphabet[(index+1) % 26]
}

function caesarShiftBy1(text) {
    return text.split('').map(nextLetter).join('')
}

function allCaesarShifts(text) {
    var temp = text.toLowerCase();
    for (var i=0; i<26; i++) {
        console.log(temp);
        temp = caesarShiftBy1(temp);
    }
}

结果:

allCaesarShifts('abcdefghijklmnopqrstuvwxyz')
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
efghijklmnopqrstuvwxyzabcd
fghijklmnopqrstuvwxyzabcde
ghijklmnopqrstuvwxyzabcdef
hijklmnopqrstuvwxyzabcdefg
ijklmnopqrstuvwxyzabcdefgh
jklmnopqrstuvwxyzabcdefghi
klmnopqrstuvwxyzabcdefghij
lmnopqrstuvwxyzabcdefghijk
mnopqrstuvwxyzabcdefghijkl
nopqrstuvwxyzabcdefghijklm
opqrstuvwxyzabcdefghijklmn
pqrstuvwxyzabcdefghijklmno
qrstuvwxyzabcdefghijklmnop
rstuvwxyzabcdefghijklmnopq
stuvwxyzabcdefghijklmnopqr
tuvwxyzabcdefghijklmnopqrs
uvwxyzabcdefghijklmnopqrst
vwxyzabcdefghijklmnopqrstu
wxyzabcdefghijklmnopqrstuv
xyzabcdefghijklmnopqrstuvw
yzabcdefghijklmnopqrstuvwx
zabcdefghijklmnopqrstuvwxy

编辑:现在按请求递归:

function allCaesarShifts(text) {
    var toReturn = [];
    function helper(text, offset) {
        toReturn +=[ caesarShift(text,offset) ];
        if (offset>0)
            helper(text, offset-1);
    }
    helper(text, 26);
    return toReturn;
}

更优雅的是创建一个函数 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:

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('')
function nextLetter(letter) {
    var index = alphabet.indexOf(letter)
    return alphabet[(index+1) % 26]
}

function caesarShiftBy1(text) {
    return text.split('').map(nextLetter).join('')
}

function allCaesarShifts(text) {
    var temp = text.toLowerCase();
    for (var i=0; i<26; i++) {
        console.log(temp);
        temp = caesarShiftBy1(temp);
    }
}

Resulting in:

allCaesarShifts('abcdefghijklmnopqrstuvwxyz')
abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyza
cdefghijklmnopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc
efghijklmnopqrstuvwxyzabcd
fghijklmnopqrstuvwxyzabcde
ghijklmnopqrstuvwxyzabcdef
hijklmnopqrstuvwxyzabcdefg
ijklmnopqrstuvwxyzabcdefgh
jklmnopqrstuvwxyzabcdefghi
klmnopqrstuvwxyzabcdefghij
lmnopqrstuvwxyzabcdefghijk
mnopqrstuvwxyzabcdefghijkl
nopqrstuvwxyzabcdefghijklm
opqrstuvwxyzabcdefghijklmn
pqrstuvwxyzabcdefghijklmno
qrstuvwxyzabcdefghijklmnop
rstuvwxyzabcdefghijklmnopq
stuvwxyzabcdefghijklmnopqr
tuvwxyzabcdefghijklmnopqrs
uvwxyzabcdefghijklmnopqrst
vwxyzabcdefghijklmnopqrstu
wxyzabcdefghijklmnopqrstuv
xyzabcdefghijklmnopqrstuvw
yzabcdefghijklmnopqrstuvwx
zabcdefghijklmnopqrstuvwxy

edit: now recursive by request:

function allCaesarShifts(text) {
    var toReturn = [];
    function helper(text, offset) {
        toReturn +=[ caesarShift(text,offset) ];
        if (offset>0)
            helper(text, offset-1);
    }
    helper(text, 26);
    return toReturn;
}

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).

瞳孔里扚悲伤 2024-11-09 06:39:21

要将字符串中的所有数字字符实体转换为其等效字符,可以执行以下操作:

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); })

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文