我正在寻找一种在JavaScript中混淆和解混字符串的方法;我所说的加密和解密是指在安全性不成问题的情况下进行加密和解密。理想情况下,JS原生的东西(如PHP中的base64_encode()和base64_decode() )可以“将字符串转换为其他东西,然后再转换回来”,而不必编写函数。
欢迎任何建议!
发布于 2013-01-22 20:43:22
发布于 2017-08-01 11:15:49
值得注意的是,
(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]
计算结果为字符串"fail“,但看起来不像字符串。认真地说,把它输入到node中,你会感到惊讶的。你可以通过疯狂拼写JavaScript中的任何东西。
发布于 2016-12-31 00:00:36
我显然来不及给出答案,但我只是在为这个问题寻找另一个解决方案,而base64似乎太弱了。
它是这样工作的:
"abc;123!".obfs(13) // => "nopH>?@."
"nopH>?@.".defs(13) // => "abc;123!"/**
* Obfuscate a plaintext string with a simple rotation algorithm similar to
* the rot13 cipher.
* @param {[type]} key rotation index between 0 and n
* @param {Number} n maximum char that will be affected by the algorithm
* @return {[type]} obfuscated string
*/
String.prototype.obfs = function(key, n = 126) {
// return String itself if the given parameters are invalid
if (!(typeof(key) === 'number' && key % 1 === 0)
|| !(typeof(key) === 'number' && key % 1 === 0)) {
return this.toString();
}
var chars = this.toString().split('');
for (var i = 0; i < chars.length; i++) {
var c = chars[i].charCodeAt(0);
if (c <= n) {
chars[i] = String.fromCharCode((chars[i].charCodeAt(0) + key) % n);
}
}
return chars.join('');
};
/**
* De-obfuscate an obfuscated string with the method above.
* @param {[type]} key rotation index between 0 and n
* @param {Number} n same number that was used for obfuscation
* @return {[type]} plaintext string
*/
String.prototype.defs = function(key, n = 126) {
// return String itself if the given parameters are invalid
if (!(typeof(key) === 'number' && key % 1 === 0)
|| !(typeof(key) === 'number' && key % 1 === 0)) {
return this.toString();
}
return this.toString().obfs(n - key);
};https://stackoverflow.com/questions/14458819
复制相似问题