1

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);
}
3
  • 1
    (1) Clarify. Do you have a caesar-encoded code and you want to generate all possible inputs that produced the encrypted text? (2) Post code. Commented Apr 16, 2011 at 19:21
  • Do you mean all permutations? Commented Apr 16, 2011 at 19:35
  • I edited it to show my code, and yes I would have a caesar-encoded code. Commented Apr 16, 2011 at 19:38

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is it a way to do it without using a for loop?
The function you are using is already using a for loop, inside it (you just don't see it). You can also use the function I used called map. But other than that, there is no way to do what you want. Don't worry though, as long as you execute the code once, all you need to ever type is allCaesarShifts('my encrypted text').
Oh, you could also use a recursive approach. But you will need to do one of those three things: for loops, recursion, or map.
I like the recursive approach, how would I do it that way?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.