2009-02-06 105 views
16

我正在尋找使用JavaScript混淆器。什麼是一些最受歡迎的產品,它們對性能有什麼影響?什麼是最好的JavaScript混淆器?

+1

雖然羅伯特·哈維提出的問題可能更多或更少的真實,這裏的答案提供混淆器的列表,非常有用。此外,哈維先生暗示,這個專家網站只能回答簡單的問題,專家的「意見」和擴大的討論在某種程度上是否定的。如果問題是「我能得到一個混淆器列表嗎?」這個問題是可以接受的嗎?問候,Dave H. – DHorse 2013-07-15 02:24:35

+0

** DUPLICATE的** - >>> ** http://stackoverflow.com/questions/194397/how-i-i-ob-fuscateprotect-javascript ** – 2014-06-12 10:05:55

+0

你可以使用http:// jsobfuscator .byethost7.com/ – 2016-05-03 12:31:17

回答

12

雅虎有一個很不錯的。它在技術上是一個縮小器,但它在這個過程中做了一個很好的混淆處理。

YUI Compressor

+0

通過YUI Compressor免費在線壓縮和混淆:http://refresh-sf.com/yui/ – NexusRex 2012-12-11 07:13:58

+0

鏈接不再有效 – 2017-11-23 08:40:21

3

好吧,谷歌提出了這個作爲第一個鏈接:

http://www.javascriptobfuscator.com

但我不知道的JavaScript良好的混淆一樣。無論你在JavaScript中做什麼需要混淆,都應該在服務器端完成,對嗎?

+0

在我看到的所有內容中,這可能是最好的混淆器。雖然我還沒有試過JScrambler。 – AStackOverflowUser 2014-05-23 03:22:16

2

我從來沒有在生產中使用混淆器,但我測試過JavaScript Utility,似乎很不錯。

至於性能,混淆代碼必須在每次加載頁面時,在每次加載頁面時動態解壓縮。對於小腳本可能不是問題,但拆包時間對於更大的文件會很重要。另一方面,縮小的代碼可以直接由瀏覽器執行。

某些混淆器可能會產生不能在較舊或較不常見的瀏覽器中運行的輸出。您應該仔細測試您計劃支持的瀏覽器。

+1

混淆器不需要任何代碼解包。其他用於最小化代碼的方案可能需要這樣做,但本身並沒有產生障礙。 – 2009-09-07 09:28:45

6

測試8個不同的模糊處理(除www.javascriptobfuscator.com),並通過他們都多少吸驚訝。最終用正則表達式編寫自己的混淆器。享受:

static Dictionary<string, string> names = new Dictionary<string, string>(); 
static bool testing = false; 
static string[] files1 = 
    @"a.js,b.js,c.js" 
    .Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
static string[] ignore_names = 
    @"sin,cos,order,min,max,join,round,pow,abs,PI,floor,random,index,http, 
    __defineGetter__,__defineSetter__,indexOf,isPrototypeOf,length,clone,toString,split,clear,erase 
    RECT,SIZE,Vect,VectInt,vectint,vect,int,double,canvasElement,text1,text2,text3,textSizeTester,target,Number 
    number,TimeStep,images,solid,white,default,cursive,fantasy,". 
    Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
string[] extra_names = @"a,b,c".Split(new string[] { Environment.NewLine, " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); 
string src = @"C:\temp"; 
string dest1 = src + "\\all1.js"; 
string dest2 = src + "\\all2.js"; 

static void Main() 
{ 
    File.Delete(dest1); 
    File.Delete(dest2); 
    foreach (string s in files1) 
    File.AppendAllText(dest1, File.ReadAllText(src + "\\" + s) + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine, Encoding.UTF8); 

    string all = File.ReadAllText(dest1); 
    int free_index = 0; 

    foreach (string s in extra_names) 
    { 
    free_index++; 
    string free_name = "" + (char)('A' + (free_index % 25)) + (char)('A' + ((free_index/25) % 25)); 
    Debug.Assert(free_name != "AA"); 
    names.Add(s, free_name); 
    } 

    Regex reg1 = new Regex("(var |function |\\.prototype\\.)([a-zA-Z0-9_]+)"); 

    int startat = 0; 
    while (startat < all.Length) 
    { 
    Match match = reg1.Match(all, startat); 
    if (!match.Success) 
     break; 

    string key = all.Substring(match.Groups[2].Index, match.Groups[2].Length); 
    if (!ignore_names.Contains(key)) 
    { 
     free_index++; 
     string free_name = "" + (char)('A' + (free_index % 25)) + (char)('A' + ((free_index/25) % 25)); 
     Debug.Assert(free_name != "AA"); 
     if (!names.ContainsKey(key)) 
     names.Add(key, testing ? key + free_name : free_name); 
    } 
    startat = match.Groups[0].Index + match.Groups[0].Length; 
    } 

    Regex reg2 = new Regex(@"/\*.*\*/", RegexOptions.Multiline); 
    Regex reg3 = new Regex("([^:\\\\])//.*\r\n"); 
    Regex reg4 = new Regex("([a-zA-Z0-9_]+)"); 
    Regex reg5 = new Regex("(\r\n)*[ \t]+"); 
    Regex reg6 = new Regex("(\r\n)+"); 
    all = reg2.Replace(all, eval2); 
    all = reg3.Replace(all, eval3); 
    all = reg4.Replace(all, eval4); 
    all = reg5.Replace(all, eval5); 
    all = reg6.Replace(all, eval6); 
    File.WriteAllText(dest2, all); 
} 

public static string eval4(Match match) 
{ 
    return names.ContainsKey(match.Groups[1].Value) ? names[match.Groups[1].Value] : match.Groups[0].Value; 
} 
public static string eval5(Match match) 
{ 
    return string.IsNullOrEmpty(match.Groups[1].Value) ? " " : Environment.NewLine; 
} 
public static string eval6(Match match) 
{ 
    return Environment.NewLine; 
} 
public static string eval2(Match match) 
{ 
    return " "; 
} 
public static string eval3(Match match) 
{ 
    return match.Groups[1].Value + Environment.NewLine; 
}