我想添加一个循环使我的加密更难破解

时间:2015-12-02 15:09:47

标签: javascript html encryption

嘿伙计们,所以我为我工作的同事们制作了一个小型的加密和解密JavaScript程序。我想为它添加另一个级别的安全性。类似于使其更安全的钥匙。

无论如何,这是我的代码:

<head>

<style>

#Search_Text{

height:80px;
width:50%;


}
</style>
<script>

function pop(){
var text =document.getElementById('Search_Text').value;
var res = text.split("");
var element = document.getElementById("result");
for(var i=0;i<res.length;i++){

switch(res[i]) {
    case " ":
    document.getElementById("result").innerHTML+=" ";
    continue;
    case "a":
        document.getElementById("result").innerHTML+="c";
        continue;
    case "A":
        document.getElementById("result").innerHTML+="C";
        continue;
    case "b":
        document.getElementById("result").innerHTML+="d";
        continue;
    case "B":
        document.getElementById("result").innerHTML+="D";
        continue;
    case "c":
        document.getElementById("result").innerHTML+="e";
        continue;
    case "C":
        document.getElementById("result").innerHTML+="E";
        continue;
    case "d":
        document.getElementById("result").innerHTML+="f";
        continue;
    case "D":
        document.getElementById("result").innerHTML+="F";
        continue;
    case "e":
        document.getElementById("result").innerHTML+="g";
        continue;
     case "E":
        document.getElementById("result").innerHTML+="G";
        continue;
     case "f":
        document.getElementById("result").innerHTML+="h";
        continue;
     case "F":
        document.getElementById("result").innerHTML+="H";
        continue
    case "g":
        document.getElementById("result").innerHTML+="i";
        continue;
     case "G":
        document.getElementById("result").innerHTML+="I";
        continue;
    case "h":
        document.getElementById("result").innerHTML+="j";
        continue;
     case "H":
        document.getElementById("result").innerHTML+="J";
        continue;
    case "i":
        document.getElementById("result").innerHTML+="k";
        continue;
     case "I":
        document.getElementById("result").innerHTML+="K";
        continue;
    case "j":
        document.getElementById("result").innerHTML+="l";
        continue;
     case "J":
        document.getElementById("result").innerHTML+="L";
        continue;
    case "k":
        document.getElementById("result").innerHTML+="m";
        continue;
     case "K":
        document.getElementById("result").innerHTML+="M";
        continue;
    case "l":
        document.getElementById("result").innerHTML+="n";
        continue;
     case "L":
        document.getElementById("result").innerHTML+="N";
        continue;
case "m":
        document.getElementById("result").innerHTML+="o";
        continue;
     case "M":
        document.getElementById("result").innerHTML+="O";
        continue;
case "n":
        document.getElementById("result").innerHTML+="p";
        continue;
     case "N":
        document.getElementById("result").innerHTML+="P";
        continue;
case "o":
        document.getElementById("result").innerHTML+="q";
        continue;
     case "O":
        document.getElementById("result").innerHTML+="Q";
        continue;
case "p":
        document.getElementById("result").innerHTML+="r";
        continue;
     case "P":
        document.getElementById("result").innerHTML+="R";
        continue;
case "q":
        document.getElementById("result").innerHTML+="s";
        continue;
     case "Q":
        document.getElementById("result").innerHTML+="S";
        continue;
case "r":
        document.getElementById("result").innerHTML+="u";
        continue;
     case "R":
        document.getElementById("result").innerHTML+="U";
        continue;
case "s":
        document.getElementById("result").innerHTML+="t";
        continue;
     case "S":
        document.getElementById("result").innerHTML+="T";
        continue;
case "t":
        document.getElementById("result").innerHTML+="w";
        continue;
     case "T":
        document.getElementById("result").innerHTML+="W";
        continue;
case "u":
        document.getElementById("result").innerHTML+="z";
        continue;
     case "U":
        document.getElementById("result").innerHTML+="Z";
        continue;
 case "v":
        document.getElementById("result").innerHTML+="y";
        continue;
     case "V":
        document.getElementById("result").innerHTML+="y";
        continue
case "w":
        document.getElementById("result").innerHTML+="a";
        continue;
     case "W":
        document.getElementById("result").innerHTML+="A";
        continue;
case "x":
        document.getElementById("result").innerHTML+="b";
        continue;
     case "X":
        document.getElementById("result").innerHTML+="B";
        continue;
case "y":
        document.getElementById("result").innerHTML+="x";
        continue;
     case "Y":
        document.getElementById("result").innerHTML+="X";
        continue;
case "z":
        document.getElementById("result").innerHTML+="v";
        continue;
     case "Z":
        document.getElementById("result").innerHTML+="V";
        continue;
case "1":
        document.getElementById("result").innerHTML+="9";
        continue;
     case "2":
        document.getElementById("result").innerHTML+="8";
        continue;
case "3":
        document.getElementById("result").innerHTML+="7";
        continue;
     case "4":
        document.getElementById("result").innerHTML+="6";
        continue;
case "5":
        document.getElementById("result").innerHTML+="5";
        continue;
     case "6":
        document.getElementById("result").innerHTML+="3";
        continue;
case "7":
        document.getElementById("result").innerHTML+="4";
        continue;
     case "8":
        document.getElementById("result").innerHTML+="2";
        continue;
case "9":
        document.getElementById("result").innerHTML+="1";
        continue;
     case "0":
        document.getElementById("result").innerHTML+="0";
        continue;

    default:
        break;


}





}
}


function pip(){
var text =document.getElementById('Search_Text').value;
var res = text.split("");
var element = document.getElementById("result");
for(var i=0;i<res.length;i++){

switch(res[i]) {
    case " ":
    document.getElementById("result").innerHTML+=" ";
    continue;
    case "c":
        document.getElementById("result").innerHTML+="a";
        continue;
    case "C":
        document.getElementById("result").innerHTML+="A";
        continue;
    case "d":
        document.getElementById("result").innerHTML+="b";
        continue;
    case "D":
        document.getElementById("result").innerHTML+="B";
        continue;
    case "e":
        document.getElementById("result").innerHTML+="c";
        continue;
    case "E":
        document.getElementById("result").innerHTML+="C";
        continue;
    case "f":
        document.getElementById("result").innerHTML+="d";
        continue;
    case "F":
        document.getElementById("result").innerHTML+="D";
        continue;
    case "g":
        document.getElementById("result").innerHTML+="e";
        continue;
     case "G":
        document.getElementById("result").innerHTML+="E";
        continue;
     case "h":
        document.getElementById("result").innerHTML+="f";
        continue;
     case "H":
        document.getElementById("result").innerHTML+="F";
        continue
    case "i":
        document.getElementById("result").innerHTML+="g";
        continue;
     case "I":
        document.getElementById("result").innerHTML+="G";
        continue;
    case "j":
        document.getElementById("result").innerHTML+="h";
        continue;
     case "J":
        document.getElementById("result").innerHTML+="H";
        continue;
    case "k":
        document.getElementById("result").innerHTML+="i";
        continue;
     case "K":
        document.getElementById("result").innerHTML+="I";
        continue;
    case "l":
        document.getElementById("result").innerHTML+="j";
        continue;
     case "L":
        document.getElementById("result").innerHTML+="J";
        continue;
    case "m":
        document.getElementById("result").innerHTML+="k";
        continue;
     case "M":
        document.getElementById("result").innerHTML+="K";
        continue;
    case "n":
        document.getElementById("result").innerHTML+="l";
        continue;
     case "N":
        document.getElementById("result").innerHTML+="L";
        continue;
case "o":
        document.getElementById("result").innerHTML+="m";
        continue;
     case "O":
        document.getElementById("result").innerHTML+="M";
        continue;
case "p":
        document.getElementById("result").innerHTML+="n";
        continue;
     case "P":
        document.getElementById("result").innerHTML+="N";
        continue;
case "q":
        document.getElementById("result").innerHTML+="o";
        continue;
     case "Q":
        document.getElementById("result").innerHTML+="O";
        continue;
case "r":
        document.getElementById("result").innerHTML+="p";
        continue;
     case "R":
        document.getElementById("result").innerHTML+="P";
        continue;
case "s":
        document.getElementById("result").innerHTML+="q";
        continue;
     case "S":
        document.getElementById("result").innerHTML+="Q";
        continue;
case "u":
        document.getElementById("result").innerHTML+="r";
        continue;
     case "U":
        document.getElementById("result").innerHTML+="R";
        continue;
case "t":
        document.getElementById("result").innerHTML+="s";
        continue;
     case "T":
        document.getElementById("result").innerHTML+="S";
        continue;
case "w":
        document.getElementById("result").innerHTML+="t";
        continue;
     case "W":
        document.getElementById("result").innerHTML+="T";
        continue;
case "z":
        document.getElementById("result").innerHTML+="u";
        continue;
     case "Z":
        document.getElementById("result").innerHTML+="U";
        continue;
 case "y":
        document.getElementById("result").innerHTML+="v";
        continue;
     case "Y":
        document.getElementById("result").innerHTML+="V";
        continue
case "a":
        document.getElementById("result").innerHTML+="w";
        continue;
     case "A":
        document.getElementById("result").innerHTML+="W";
        continue;
case "b":
        document.getElementById("result").innerHTML+="x";
        continue;
     case "B":
        document.getElementById("result").innerHTML+="X";
        continue;
case "x":
        document.getElementById("result").innerHTML+="y";
        continue;
     case "X":
        document.getElementById("result").innerHTML+="Y";
        continue;
case "v":
        document.getElementById("result").innerHTML+="z";
        continue;
     case "V":
        document.getElementById("result").innerHTML+="Z";
        continue;
case "9":
        document.getElementById("result").innerHTML+="1";
        continue;
     case "8":
        document.getElementById("result").innerHTML+="2";
        continue;
case "7":
        document.getElementById("result").innerHTML+="3";
        continue;
     case "6":
        document.getElementById("result").innerHTML+="4";
        continue;
case "5":
        document.getElementById("result").innerHTML+="5";
        continue;
     case "3":
        document.getElementById("result").innerHTML+="6";
        continue;
case "4":
        document.getElementById("result").innerHTML+="7";
        continue;
     case "2":
        document.getElementById("result").innerHTML+="8";
        continue;
case "1":
        document.getElementById("result").innerHTML+="9";
        continue;
     case "0":
        document.getElementById("result").innerHTML+="0";
        continue;

    default:
        break;


}





}
}
</script>

</head>
<body>




<form  onsubmit="return false;">
<textarea type="text" id="Search_Text"></textarea>
<br>
<button type="submit"onclick="pop()">Encrypt</button>
<button type="submit"onclick="pip()">Decrypt</button>
</form>



<p id="result"></p>
</body>

1 个答案:

答案 0 :(得分:0)

请不要发明自己的密码学。使用像CryptoJS之类的库会好得多。创建自己的库会非常危险,因为您认为安全的内容很容易被破坏。即使你有一个好的算法,你也可能会错误地将自己暴露给其他问题。

你的算法充其量可以被认为是一种编码,比如base64,它最好是一种严重破坏的Caesar Cipher形式。

我说所有这些都不是出于侵略,而是出于对不良密码学的关注。 https://crypto.stackexchange.com/的任何主要贡献者都可能同意。

<强>更新

以下是链接

的摘录示例
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");

    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
</script>