如何从客户端隐藏或保护 javascript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5167853/
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
How to hide or secure javascript code from client side
提问by KillerFish
How to secure or hide javascript code on client side. Is there any way to doing so.
如何在客户端保护或隐藏 javascript 代码。有什么办法可以这样做。
Thank You
谢谢你
回答by ThiefMaster
Short answer:You can't/don't.
简短回答:你不能/不。
Longer answer:You cannot hide it at all. It runs on the client and it cannot be compiled to machine code. However, you could minify it - that's basically obfuscating it by shortening variable names, removing whitespace, etc. While it's usually used to save bandwidth it also makes the code less readable. Note that all but the changed variable names and removed comments can be easily undone by something like jsbeautufier.. but for a large application it's very hard to understand the code without any meaningful variable/function names or comments.
更长的答案:你根本无法隐藏它。它在客户端运行,无法编译为机器码。但是,您可以缩小它 - 这基本上是通过缩短变量名称、删除空格等来混淆它。虽然它通常用于节省带宽,但它也降低了代码的可读性。请注意,除了更改的变量名称和删除的注释之外的所有内容都可以通过 jsbeautufier 之类的东西轻松撤消。但对于大型应用程序,如果没有任何有意义的变量/函数名称或注释,则很难理解代码。
回答by Zoidberg
There is no such thing as 100% secure javascript code. This is because any code executed on the client's machine cannot be fully secure. Your best bet is to obfuscate your javascript and make it hard to read.
没有 100% 安全的 JavaScript 代码。这是因为在客户端机器上执行的任何代码都不能完全安全。最好的办法是混淆你的 javascript 并使其难以阅读。
Your best bet is to ensure all vital secure code runs on the server, and allow javascript to do only simple, UI enhancing tasks on the client side.
最好的办法是确保所有重要的安全代码都在服务器上运行,并允许 javascript 在客户端只执行简单的 UI 增强任务。
回答by dinesh707
As i know it is not possible. Only thing you can do it making the code very badly organized. Which will take longer time to find out actually what you are doing.
据我所知,这是不可能的。你唯一能做的就是让代码组织得非常糟糕。这将需要更长的时间才能真正了解您在做什么。
If you are searching this because of security reasons, you have to remember the only thing matters in security is the password-which is not put in the code. So find a nice way to encrypt you stuff. You can find many good ways on web.
如果您出于安全原因进行搜索,您必须记住安全方面唯一重要的事情是密码——它没有放在代码中。所以找到一个很好的方法来加密你的东西。你可以在网上找到很多好方法。