0

Javascript:

function decRot13(str) {

    var decode = '';

        for(var i = 0; i < str.length; i++){
            if(str.charCodeAt(i) + 13 > 90)
            //make calculations to reset from 65 to find new charCode value
                decode += String.fromCharCode(13 - (90 - str.charCodeAt(i)) + 64);
            else if(str.charCodeAt(i) + 13 <= 90 && str.charCodeAt(i) >= 65)    
              //if value is between 65 and 90 add 13 to charCode value
                decode += String.fromCharCode(str.charCodeAt(i) + 13);
            else if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90)
                //if value is less than 65 or greater than 90 add same value
                decode += String.fromCharCode(str.charCodeAt(i));
        }

        return decode;
    }

    function encRot13(str) {

    var encode = '';

        for(var i = 0; i < str.length; i++){

            if(str.charCodeAt(i) - 13 < 65)
                encode += String.fromCharCode(90 - (64 - (str.charCodeAt(i) - 13)));
            else if(str.charCodeAt(i) - 13 <= 90 && str.charCodeAt(i) >= 65)
                encode += String.fromCharCode(str.charCodeAt(i) - 13);
            else if(str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90)
                encode += String.fromCharCode(str.charCodeAt(i));
        }

        return encode;
    }

    alert(encRot13('LMNOP!'));

decRot13(); 函数正在产生我想要的输出,解密密码并忽略空格和感叹号等特殊字符。(基本上任何非字母的)。

我颠倒了程序,但由于某种原因,它产生了加密的字母,但改变了特殊字符(我想被忽略。)

我该如何解决?

4

0 回答 0