在JavaScript中对字符串进行模糊处理和反混淆的最简单方法

Ric*_*nks 35 javascript encryption obfuscation

我正在寻找一种在JavaScript中混淆和反混淆字符串的方法; 当安全性不是问题时,我的意思是加密和解密.理想的情况是一些原产于JS(像base64_encode()base64_decode()在PHP)到"把字符串转换成别的,然后再返回的东西",而无需编写一个函数.

欢迎任何建议!

Min*_*hev 67

您可以使用btoa()atob().btoa()是喜欢base64_encode()atob()喜欢base64_decode().

这是一个例子:

btoa('Some text'); // U29tZSB0ZXh0
atob('U29tZSB0ZXh0'); // Some text
Run Code Online (Sandbox Code Playgroud)

请记住,这不是保密的安全方法.Base64是一种二进制到文本编码方案,通过将其转换为基数-64表示,以ASCII字符串格式表示二进制数据.

  • 值得注意的是,编码后的字符串将比未编码的字符串大。 (3认同)
  • 似乎适用于大多数现代浏览器,因此非常适合我的要求.谢谢! (2认同)

Gan*_*Man 19

值得注意的是

(![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]

评估字符串"fail"而不看起来像字符串.说真的,把它输入节点并感到惊讶.你可以疯狂地用JavaScript拼写任何东西.

  • 有趣!让它与大多数字母一起工作!https://jsfiddle.net/pg07yf87/2/编辑:这个网站也为你做了http://www.jsfuck.com/ (8认同)
  • 进入节点并将其分解为多个部分。它开始变得有些病态的意义。 (2认同)

enz*_*ian 11

我显然已经来不及回答,但我正在研究解决这个问题的另一个解决方案,而base64似乎很弱.

它的工作原理如下:

"abc;123!".obfs(13) // => "nopH>?@."
"nopH>?@.".defs(13) // => "abc;123!"
Run Code Online (Sandbox Code Playgroud)

代码:

/**
 * 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);
};
Run Code Online (Sandbox Code Playgroud)

  • 这段代码破坏了 `for...in` 字符串枚举,不要在生产中使用它。 (3认同)
  • 您有一个不错的小混淆器,但它会在全局范围内公开,任何人都可以在 String 原型上看到。有点超出了目的。 (2认同)