<p>How do I lock compiled Java classes to prevent decompilation?</p>

<p>I know this must be very well discussed topic on the Internet, but I could not come to any conclusion after referring them.</p>

<p>Many people do suggest obfuscator, but they just do renaming of classes, methods, and fields with tough-to-remember character sequences but what about sensitive constant values?</p>

<p>For example, you have developed the encryption and decryption component based on a password based encryption technique. Now in this case, any average Java person can use <a href="http://www.kpdus.com/jad.html" rel="noreferrer">JAD</a> to decompile the class file and easily retrieve the password value (defined as constant) as well as <a href="http://en.wikipedia.org/wiki/Salt_%28cryptography%29" rel="noreferrer">salt</a> and in turn can decrypt the data by writing small independent program!</p>

<p>Or should such sensitive components be built in native code (for example, VC++) and call them via <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="noreferrer">JNI</a>?</p>

[{'answer_id': 49385, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 4, 'selected': False, 'text': "<p>No matter what you do, it can be 'decompiled'. Heck, you can just disassemble it. Or look at a memory dump to find your constants. You see, the computer needs to know them, so your code will need to too.</p>\n\n<p>What to do about this?</p>\n\n<p>Try not to ship the key as a hardcoded constant in your code: Keep it as a per-user setting. Make the user responsible for looking after that key.</p>\n"}, {'answer_id': 49397, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 4, 'selected': False, 'text': "<p>Disclaimer: I am not a security expert.</p>\n\n<p>This sounds like a bad idea: You are letting someone encrypt stuff with a 'hidden' key that you give him. I don't think this can be made secure.</p>\n\n<p>Maybe asymmetrical keys could work:</p>\n\n<ul>\n<li>deploy an encrypted license with a public key to decrypt</li>\n<li>let the customer create a new license and send it to you for encryption</li>\n<li>send a new license back to the client.</li>\n</ul>\n\n<p>I'm not sure, but I believe the client can actually encrypt the license key with the public key you gave him. You can then decrypt it with your private key and re-encrypt as well.</p>\n\n<p>You could keep a separate public/private key pair per customer to make sure you actually are getting stuff from the right customer - now <em>you</em> are responsible for the keys...</p>\n"}, {'answer_id': 49400, 'author': 'Stu Thompson', 'author_id': 2961, 'author_profile': 'https://Stackoverflow.com/users/2961', 'pm_score': 3, 'selected': False, 'text': "<p>@jatanp: or better yet, they can decompile, remove the licensing code, and recompile.  With Java, I don't really think there is a proper, hack-proof solution to this problem.  Not even an evil little dongle could prevent this with Java.</p>\n\n<p>My own biz managers worry about this, and I think too much.  But then again, we sell our application into large corporates who tend to abide by licensing conditions--generally a safe environment thanks to the bean counters and lawyers.  The act of decompiling itself can be illegal if your license is written correctly. </p>\n\n<p>So, I have to ask, do you <em>really</em> need hardened protection like you are seeking for your application?  What does your customer base look like?  (Corporates?  Or the teenage gamer masses, where this would be more of an issue?)</p>\n"}, {'answer_id': 49401, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 4, 'selected': False, 'text': '<p>As long as they have access to both the encrypted data and the software that decrypts it, there is basically no way you can make this completely secure. Ways this has been solved before is to use some form of external black box to handle encryption/decryption, like dongles, remote authentication servers, etc. But even then, given that the user has full access to their own system, this only makes things difficult, not impossible -unless you can tie your product directly to the functionality stored in the "black box", as, say, online gaming servers.</p>\n'}, {'answer_id': 49428, 'author': 'Roland Tepp', 'author_id': 1712, 'author_profile': 'https://Stackoverflow.com/users/1712', 'pm_score': 8, 'selected': True, 'text': '<p>Some of the more advanced Java bytecode obfuscators do much more than just class name mangling. <a href="http://www.zelix.com/" rel="noreferrer">Zelix KlassMaster</a>, for example, can also scramble your code flow in a way that makes it really hard to follow and works as an excellent code optimizer...</p>\n\n<p>Also many of the obfuscators are also able to scramble your string constants and remove unused code.</p>\n\n<p>Another possible solution (not necessarily excluding the obfuscation) is to use <a href="http://www.componio.com/products/jinstaller/jinstaller_secure_edition/jarcryp.html" rel="noreferrer">encrypted JAR files</a> and a custom classloader that does the decryption (preferably using native runtime library).</p>\n\n<p>Third (and possibly offering the strongest protection) is to use native ahead of time compilers like <a href="http://en.wikipedia.org/wiki/GNU_Compiler_Collection" rel="noreferrer">GCC</a> or <a href="http://www.excelsior-usa.com/jet.html" rel="noreferrer">Excelsior JET</a>, for example, that compile your Java code directly to a platform specific native binary.</p>\n\n<p>In any case You\'ve got to remember that as the saying goes in Estonian "Locks are for animals". Meaning that every bit of code is available (loaded into memory) during the runtime and given enough skill, determination and motivation, people can and will decompile, unscramble and hack your code... Your job is simply to make the process as uncomfortable as you can and still keep the thing working...</p>\n'}, {'answer_id': 49494, 'author': 'hakan', 'author_id': 3993, 'author_profile': 'https://Stackoverflow.com/users/3993', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re looking for a licensing solution, you can check out the <a href="http://java.net/projects/truelicense/" rel="nofollow noreferrer">TrueLicense API</a>. It\'s based on the use of asymmetrical keys. However, it doesn\'t mean your application cannot be cracked. Every application can be cracked with enough effort. What really important is, as <a href="https://stackoverflow.com/questions/49379/how-to-lock-compiled-java-classes-to-prevent-decompilation/49400#49400">Stu answered</a>, figuring out how strong protection you need. </p>\n'}, {'answer_id': 49651, 'author': 'Telcontar', 'author_id': 518, 'author_profile': 'https://Stackoverflow.com/users/518', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t think there exists any effective offline antipiracy method. The videogame industry has tried to find that many times and their programs has always been cracked. The only solution is that the program must be run online connected with your servers, so that you can verify the lincense key, and that there is only one active connecion by the licensee at a time. This is how <a href="http://en.wikipedia.org/wiki/World_of_Warcraft" rel="nofollow noreferrer">World of Warcraft</a> or <a href="http://en.wikipedia.org/wiki/Diablo_%28video_game%29" rel="nofollow noreferrer">Diablo</a> works. Even tough there are private servers developed for them to bypass the security. </p>\n\n<p>Having said that, I don\'t believe that mid/large corporations use illegal copied software, because the cost of the license for them is minimal (perhaps, I don\'t know how much you are goig to charge for your program) compared to the cost of a trial version.</p>\n'}, {'answer_id': 9598442, 'author': 'Yury Bendersky', 'author_id': 1239661, 'author_profile': 'https://Stackoverflow.com/users/1239661', 'pm_score': 2, 'selected': False, 'text': "<p>You can use byte-code encryption with no fear. </p>\n\n<p>The fact is that the cited above paper “Cracking Java byte-code encryption” contains a logic fallacy. The main claim of the paper is <em>before running all classes must be decrypted and passed to the <code>ClassLoader.defineClass(...)</code> method</em>. But this is not true. </p>\n\n<p>The assumption missed here is <em>provided that they are running in authentic, or standard, java run-time environment</em>. Nothing can oblige the protected java app not only to launch these classes but even decrypt and pass them to <code>ClassLoader</code>. In other words, if you are in standard JRE you can't intercept <code>defineClass(...)</code> method because the standard java has no API for this purpose, and if you use modified JRE with patched <code>ClassLoader</code> or any other “hacker trick” you can't do it because protected java app will not work at all, and therefore you will have nothing to intercept. And absolutely doesn't matter which “patch finder” is used or which trick is used by hackers. These technical details are a quite different story. </p>\n"}, {'answer_id': 32651383, 'author': 'S Harish Morampudi', 'author_id': 5060052, 'author_profile': 'https://Stackoverflow.com/users/5060052', 'pm_score': 0, 'selected': False, 'text': '<p>Q: If I encrypt my .class files and use a custom classloader to load and decrypt them on the fly, will this prevent decompilation?</p>\n\n<p>A: The problem of preventing Java byte-code decompilation is almost as old the language itself. Despite a range of obfuscation tools available on the market, novice Java programmers continue to think of new and clever ways to protect their intellectual property. In this Java Q&amp;A installment, I dispel some myths around an idea frequently rehashed in discussion forums.</p>\n\n<p>The extreme ease with which Java .class files can be reconstructed into Java sources that closely resemble the originals has a lot to do with Java byte-code design goals and trade-offs. Among other things, Java byte code was designed for compactness, platform independence, network mobility, and ease of analysis by byte-code interpreters and JIT (just-in-time)/HotSpot dynamic compilers. Arguably, the compiled .class files express the programmer\'s intent so clearly they could be easier to analyze than the original source code.</p>\n\n<p>Several things can be done, if not to prevent decompilation completely, at least to make it more difficult. For example, as a post-compilation step you could massage the .class data to make the byte code either harder to read when decompiled or harder to decompile into valid Java code (or both). Techniques like performing extreme method name overloading work well for the former, and manipulating control flow to create control structures not possible to represent through Java syntax work well for the latter. The more successful commercial obfuscators use a mix of these and other techniques.</p>\n\n<p>Unfortunately, both approaches must actually change the code the JVM will run, and many users are afraid (rightfully so) that this transformation may add new bugs to their applications. Furthermore, method and field renaming can cause reflection calls to stop working. Changing actual class and package names can break several other Java APIs (JNDI (Java Naming and Directory Interface), URL providers, etc.). In addition to altered names, if the association between class byte-code offsets and source line numbers is altered, recovering the original exception stack traces could become difficult.</p>\n\n<p>Then there is the option of obfuscating the original Java source code. But fundamentally this causes a similar set of problems.\nEncrypt, not obfuscate?</p>\n\n<p>Perhaps the above has made you think, "Well, what if instead of manipulating byte code I encrypt all my classes after compilation and decrypt them on the fly inside the JVM (which can be done with a custom classloader)? Then the JVM executes my original byte code and yet there is nothing to decompile or reverse engineer, right?"</p>\n\n<p>Unfortunately, you would be wrong, both in thinking that you were the first to come up with this idea and in thinking that it actually works. And the reason has nothing to do with the strength of your encryption scheme.</p>\n'}]
['https://Stackoverflow.com/questions/49379', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/959/']