在 JavaScript 中混淆和反混淆字符串的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14458819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simplest way to obfuscate and deobfuscate a string in JavaScript
提问by Rich Jenks
I'm looking for a way to obfuscate and deobfuscate a string in JavaScript; by which I mean encryption and decryption when security is not an issue. Ideally something native to JS (like base64_encode()and base64_decode()in PHP) to "turn a string into something else and back again" without having to write a function.
我正在寻找一种在 JavaScript 中混淆和反混淆字符串的方法;我的意思是当安全不是问题时加密和解密。理想的情况是一些原产于JS(像base64_encode()和base64_decode()在PHP)到“把字符串转换成别的东西,然后再返回”,而无需编写一个函数。
Any suggestions welcome!
欢迎任何建议!
回答by Minko Gechev
You can use btoa()and atob(). btoa()is like base64_encode()and atob()like base64_decode().
您可以使用btoa()和atob()。btoa()是喜欢base64_encode()又atob()喜欢base64_decode()。
Here is an example:
下面是一个例子:
btoa('Some text'); // U29tZSB0ZXh0
atob('U29tZSB0ZXh0'); // Some text
Keep in mind that this is not a secure way to keep secrets. Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation.
请记住,这不是一种安全的保密方式。Base64 是一种二进制到文本的编码方案,它通过将二进制数据转换为基数 64 表示来表示 ASCII 字符串格式的二进制数据。
回答by Gant Laborde
It's worth noting that
值得注意的是
(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]
(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]
evaluates to the string "fail" without ever looking like a string. Seriously, enter it into node and be amazed. You can spell anything in JavaScript by being crazy.
计算结果为字符串“fail”,但看起来不像字符串。认真地,将其输入节点并感到惊讶。你可以疯狂地拼写 JavaScript 中的任何东西。
回答by enzian
I'm obviously too late for an answer, but I was just working on another solution for the problem and base64 seemed to be to weak.
我的答案显然为时已晚,但我只是在研究该问题的另一个解决方案,而 base64 似乎很弱。
It works like this:
它是这样工作的:
"abc;123!".obfs(13) // => "nopH>?@."
"nopH>?@.".defs(13) // => "abc;123!"
Code:
代码:
/**
* 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);
};

