简体   繁体   English

如何加密.jar文件

[英]How to encrypt a .jar file

我正在一个项目中,我们需要加密.jar文件,所以没有人可以访问jar文件中的.class文件....是否有任何java编码可以帮助我加密.jar文件?

Even if you encrypt the jar file, it must be decrypted before the JVM is able to run it, so you'll need another jar file containing classes that decrypt and loads in the JVM. 即使您加密jar文件,它也必须在JVM能够运行之前解密,因此您需要另一个包含在JVM中解密和加载的类的jar文件。

Since this second jar file cannot be itself encrypted, a malicious user wanting to see you class files, can simply look at classes in this second jar file, and then decrypt your super-secret jar file and have access to it. 由于这个第二个jar文件本身不能加密,想要查看类文件的恶意用户可以简单地查看第二个jar文件中的类,然后解密你的超级机密jar文件并访问它。

Maybe you can increase security of your code using an obfuscator, but it will eventually protect (make it harder but not impossible) your class files from decompilation, not from being used. 也许您可以使用混淆器来提高代码的安全性,但它最终会保护(使其更难但不是不可能)您的类文件来自反编译,而不是被使用。

If obfuscation is not enough, you could consider compiling your jar file to a DLL for windows or a SO for unix/linux, that will make it much harder to decompile, but it's not always possible to do that correctly and it's generally a PITA. 如果混淆不够,你可以考虑将你的jar文件编译为windows的DLL或unix / linux的SO,这将使反编译变得更加困难,但并不总是能够正确地做到这一点,而且它通常是PITA。 GCJ is able to do this somehow, and there are other commercial products that will actually compile .class/.jar directly to machine code. GCJ能够以某种方式做到这一点,还有其他商业产品实际上将.class / .jar直接编译为机器代码。

However please consider that it does not matter how much security you put in it, since the client computer MUST be able to execute it, it must be able to read it, so no matter what your code will be exposed, you can only make it harder. 但请注意,放入它的安全性并不重要,因为客户端计算机必须能够执行它,它必须能够读取它,因此无论您的代码将被暴露,您只能制作它更难。

If you really have an algorithm so secret you don't want to disclose no matter what, consider converting it to a web service, hosting it on your server, so that you don't have to send the actual code to the client machines and can also better prevent unauthorized copies of your application by checking access to that vital part of it. 如果你真的有一个如此秘密的算法你不想透露什么,考虑将其转换为Web服务,将其托管在你的服务器上,这样你就不必将实际代码发送到客户端机器和通过检查对其重要部分的访问权限,还可以更好地防止未经授权的应用程序副本。

I assume you are aware of the fact that any skilled java coder can reverse-engineer the Java tool you use (or write) and still decode the app's jars? 我假设您知道任何熟练的Java编码器都可以对您使用(或编写)的Java工具进行逆向工程,并仍然可以对应用程序的jar进行解码? Also writing custom classloaders which read your "encrypted" code can be decompiled and a tool could be written to bypass it. 编写读取“加密”代码的自定义类加载器也可以反编译,并且可以编写工具来绕过它。

Even with obfuscation and bytecode modification and custom classloaders, java is hackable/decompileable and the source can almost always be brought to a somewhat readable state. 即使使用模糊处理和字节码修改以及自定义类加载器,java也是可攻击/可反编译的,并且源几乎总是可以进入某种可读状态。

You want to obfuscate, not encrypt, the jar file. 您想要对jar文件进行模糊处理,而不是加密。

A popular choice for doing this in Java is ProGuard . 在Java中这样做的一个流行选择是ProGuard

No. Since your program needs to be able to run the code it would be pointless anyway. 不。因为你的程序需要能够运行代码,所以无论如何都是毫无意义的。

You can obfuscate your code though so decompiling the .class files results in less readable code (meaningless variable/class names etc). 您可以对代码进行模糊处理,因此反编译.class文件会导致代码不太可读(无意义的变量/类名等)。

As far as I know this is not supported by standard JVM. 据我所知,标准JVM不支持此功能。 But you can do the following. 但是你可以做到以下几点。 Separate your application into 2 parts. 将您的申请分为两部分。 First will not be encrypted. 首先不会加密。 It will be a simple loader that will instantiate the rest using custom class loader. 它将是一个简单的加载器,它将使用自定义类加载器实例化其余的。 This class loader will get Classes as arrays of bytes, decrypt and load them. 这个类加载器将Classes作为字节数组,解密并加载它们。

如果你在jar中打包 - >只需将其重命名为jarname.ABCD或任何误导扩展名,甚至取消扩展名,并相应地在你的应用程序中指定jar名称。

if you don't want to provide an access to the class files inside the jar, why should you supply your jar with the application? 如果您不想提供对jar内部类文件的访问,为什么要为您的jar提供应用程序? It feels like your question is kind of wrong conceptually... 感觉你的问题在概念上有点错误......

If you need some custom way of loading the classes, consider to use custom classloader. 如果您需要一些自定义加载类的方法,请考虑使用自定义类加载器。

i prefer jCrypt ! 我更喜欢jCrypt It is a simple tool where you can crypt the classes(and ressources) 这是一个简单的工具,你可以加密类(和资源)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM