我正在创建一个创建完全随机的加密系统的系统。我遇到的唯一问题是让解密工作。请查看下面的代码段。
RandomCryption = function() {
var uppercase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
var lowercase = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var specChars = [
"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "{",
"}", "|", ":", '"', "<", ">", "/", "`", "-", "=", "[", "]", "\\", ";",
'"', ",", ".", "/"
];
var RandomValue = function() {
var randomValue = "";
var length = Math.floor(Math.random() * 5);
while (randomValue.length <= length) {
var charType = Math.floor(Math.random() * 4);
var char = null;
if (charType === 0) {
char = uppercase[Math.floor(Math.random() * uppercase.length)];
} else if (charType === 1) {
char = lowercase[Math.floor(Math.random() * lowercase.length)];
} else if (charType === 2) {
char = numbers[Math.floor(Math.random() * numbers.length)];
} else if (charType === 3) {
char = specChars[Math.floor(Math.random() * specChars.length)];
}
randomValue += char;
}
return randomValue;
};
var cryption = {
A: RandomValue(), a: RandomValue(),
B: RandomValue(), b: RandomValue(),
C: RandomValue(), c: RandomValue(),
D: RandomValue(), d: RandomValue(),
E: RandomValue(), e: RandomValue(),
F: RandomValue(), f: RandomValue(),
G: RandomValue(), g: RandomValue(),
H: RandomValue(), h: RandomValue(),
I: RandomValue(), i: RandomValue(),
J: RandomValue(), j: RandomValue(),
K: RandomValue(), k: RandomValue(),
L: RandomValue(), l: RandomValue(),
M: RandomValue(), m: RandomValue(),
N: RandomValue(), n: RandomValue(),
O: RandomValue(), o: RandomValue(),
P: RandomValue(), p: RandomValue(),
Q: RandomValue(), q: RandomValue(),
R: RandomValue(), r: RandomValue(),
S: RandomValue(), s: RandomValue(),
T: RandomValue(), t: RandomValue(),
U: RandomValue(), u: RandomValue(),
V: RandomValue(), v: RandomValue(),
W: RandomValue(), w: RandomValue(),
X: RandomValue(), x: RandomValue(),
Y: RandomValue(), y: RandomValue(),
Z: RandomValue(), z: RandomValue(),
Zero: RandomValue(), One: RandomValue(),
Two: RandomValue(), Three: RandomValue(),
Four: RandomValue(), Five: RandomValue(),
Six: RandomValue(), Seven: RandomValue(),
Eight: RandomValue(), Nine: RandomValue()
};
this.Encrypt = function(string) {
var encryptedString = "";
for (var c=0; c<string.length; c++) {
var char = string[c];
if (cryption[char]) {
encryptedString += cryption[char];
} else {
switch(char) {
case "0":
encryptedString += cryption.Zero;
break;
case "1":
encryptedString += cryption.One;
break;
case "2":
encryptedString += cryption.Two;
break;
case "3":
encryptedString += cryption.Three;
break;
case "4":
encryptedString += cryption.Four;
break;
case "5":
encryptedString += cryption.Five;
break;
case "6":
encryptedString += cryption.Six;
break;
case "7":
encryptedString += cryption.Seven;
break;
case "8":
encryptedString += cryption.Eight;
break;
case "9":
encryptedString += cryption.Nine;
break;
}
}
}
return encryptedString;
};
this.Decrypt = function(string) {
var decryptedString = "";
var splitString = string.split(" ", string.length);
var keys = Object.keys(cryption);
for (var w=0; w<splitString.length; w++) {
var word = splitString[w];
for (var k=0; k<keys.length; k++) {
var key = cryption[keys[k]];
word = word.replace(key, keys[k]);
}
console.log(word);
}
return decryptedString;
};
};
var cryption = new RandomCryption();
var decryptedText = document.getElementById("dt");
var encryptedText = document.getElementById("et");
function NewCryption() {
cryption = new RandomCryption();
decryptedText.value = "";
encryptedText.value = "";
}
decryptedText.addEventListener("input", function(e) {
encryptedText.value = cryption.Encrypt(decryptedText.value);
});
encryptedText.addEventListener("input", function(e) {
decryptedText.value = cryption.Decrypt(encryptedText.value);
});
<!DOCTYPE html>
<html>
<head>
<title>RandomCryption.js</title>
</head>
<body>
<input placeholder="Decrypted Text" id="dt">
<br/>
<input placeholder="Encrypted Text" id="et">
<br/>
<button onclick="NewCryption()">Create New Cryption</button>
</body>
</html>
问题是解密无法正常读取。有时它可以工作,但会将加密变量中的块添加到解密文本中。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
this.Decrypt
说:var splitString = string.split(" ", string.length);
。但是在加密字符串时,我没有看到任何添加空格的地方。因此,不是单独处理每个空格分隔的部分(对应于原始未加密字符串中的单个字符),this.Decrypt
必须仅使用word = string
进行一次for循环。因此,您需要在加密时添加空格。
然后你有了word = word.replace(key, keys[k]);
,尽管缺少空格问题,它仍会用正确的字符替换一些加密的部分。但是,这是不可靠的,因为它可能也会错误地替换某些字符。找到匹配项后,您要执行的操作是将解密后的字符添加到decryptedString
。换句话说,请使用以下内容替换word = word.replace(key, keys[k]);
:
if (word === key) {
decryptedString += keys[k];
break;
}
但是,您的加密模糊不清:某些字符将加密为相同的字符串。主要是因为(平均)大约20个不同的字符将被加密为空字符串。因此,您需要以某种方式更改加密,以防止cryption
中的重复值。