問
如果我把我的class文件加密
答
防止JAVA字節碼反編譯這個問題在java語言雛形期就有了
Class文件能被很輕松的重構生成JAVA源文件與最初JAVA字節碼的設計目的和商業交易有緊密地聯系
如果不能阻止被反編譯的話
不幸的是
於是就有了一些打亂JAVA源代碼的選項
加密而不打亂
或許上述可能會使你問
考慮到你是第一個提出這種想法的並且它又能正常運行
下面是一個簡單的類編碼器
為了闡明這種思想
public class Main
{
public static void main (final String [] args)
{
System
}
} // End of class
package de;
import java
public class MySecretClass
{
/**
* Guess what
*/
public static int mySecretAlgorithm ()
{
return (int) s_random
}
private static final Random s_random = new Random (System
} // End of class
我想通過加密相關的class文件並在運行期解密來隱藏de
public class EncryptedClassLoader extends URLClassLoader
{
public static void main (final String [] args)
throws Exception
{
if (
{
// Create a custom loader that will use the current loader as
// delegation parent:
final ClassLoader appLoader =
new EncryptedClassLoader (EncryptedClassLoader
new File (args [
// Thread context loader must be adjusted as well:
Thread
final Class app = appLoader
final Method appmain = app
final String [] appargs = new String [args
System
appmain
}
else if (
{
}
else
throw new IllegalArgumentException (USAGE);
}
/**
* Overrides java
* delegation rules just enough to be able to
* from under system classloader
*/
public Class loadClass (final String name
throws ClassNotFoundException
{
if (TRACE) System
Class c = null;
// First
// instance:
c = findLoadedClass (name);
if (c == null)
{
Class parentsVersion = null;
try
{
// This is slightly unorthodox: do a trial load via the
// parent loader and note whether the parent delegated or not;
// what this accomplishes is proper delegation for all core
// and extension classes without my having to filter on class name:
parentsVersion = getParent ()
if (parentsVersion
c = parentsVersion;
}
catch (ClassNotFoundException ignore) {}
catch (ClassFormatError ignore) {}
if (c == null)
{
try
{
// OK
// or extension) loader (in which case I want to ignore that
// definition) or the parent failed altogether; either way I
// attempt to define my own version:
c = findClass (name);
}
catch (ClassNotFoundException ignore)
{
// If that failed
// [which could be null at this point]:
c = parentsVersion;
}
}
}
if (c == null)
throw new ClassNotFoundException (name);
if (resolve)
resolveClass (c);
return c;
}
/**
* Overrides java
* crypt() before defining a class
*/
protected Class findClass (final String name)
throws ClassNotFoundException
{
if (TRACE) System
//
// but if Sun
final String classResource = name
final URL classURL = getResource (classResource);
if (classURL == null)
throw new ClassNotFoundException (name);
else
{
InputStream in = null;
try
{
in = classURL
final byte [] classBytes = readFully (in);
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25555.html