JavaScript Decrypt Function
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);
}
ne开发者_如何学JAVAwStr= newStr.join("");
alert(newStr);
}
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).
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); })
精彩评论