有多个工具:
1)ProGuard 是一个免费的 Java类文件的压缩,优化,混肴器。它删除没有用的类,字段,方法与属性。使字节码最大程度地优化,使用简短且无意义的名字来重命名类、字段和方法 。
2)JavaGuard是一个通用的字节码模糊器,旨在容易地适合你的规则建造和测试进程,保证你的有价值的代码更安全,使其不易被反编译以及其它形式的反向处理。
3)RetroGuard是不错的Java混淆器,在JBuilder7的企业版中也带了这个混淆器。
4)JODE包含一个Java混淆器与一个Java优化器。通过一个脚本文件可以控制Class文件的多种优化方式。
重写ClassLoader的部分方法,对加载进来的.class进行字节加减来实现基本的加密和解秘
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
This program demonstrates a custom class loader that decrypts
class files.
*/
public class ClassLoaderTest
{
public static void main(String[] args)
{
JFrame frame = new ClassLoaderFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
This frame contains two text fields for the name of the class
to load and the decryption key.
*/
class ClassLoaderFrame extends JFrame
{
public ClassLoaderFrame()
{
setTitle("ClassLoaderTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLayout(new GridBagLayout());
add(new JLabel("Class"), new GBC(0, 0).setAnchor(GBC.EAST));
add(nameField, new GBC(1, 0).setWeight(100, 0).setAnchor(GBC.WEST));
add(new JLabel("Key"), new GBC(0, 1).setAnchor(GBC.EAST));
add(keyField, new GBC(1, 1).setWeight(100, 0).setAnchor(GBC.WEST));
JButton loadButton = new JButton("Load");
add(loadButton, new GBC(0, 2, 2, 1));
loadButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
runClass(nameField.getText(), keyField.getText());
}
});
pack();
}
/**
Runs the main method of a given class.
@param name the class name
@param key the decryption key for the class files
*/
public void runClass(String name, String key)
{
try
{
ClassLoader loader = new CryptoClassLoader(Integer.parseInt(key));
Class c = loader.loadClass(name);
String[] args = new String[] {};
Method m = c.getMethod("main", args.getClass());
m.invoke(null, (Object) args);
}
catch (Throwable e)
{
JOptionPane.showMessageDialog(this, e);
}
}
private JTextField keyField = new JTextField("3", 4);
private JTextField nameField = new JTextField(30);
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
}
/**
This class loader loads encrypted class files.
*/
class CryptoClassLoader extends ClassLoader
{
/**
Constructs a crypto class loader.
@param k the decryption key
*/
public CryptoClassLoader(int k)
{
key = k;
}
protected Class findClass(String name)
throws ClassNotFoundException
{
byte[] classBytes = null;
try
{
classBytes = loadClassBytes(name);
}
catch (IOException e)
{
throw new ClassNotFoundException(name);
}
Class cl = defineClass(name, classBytes, 0, classBytes.length);
if (cl == null)
throw new ClassNotFoundException(name);
return cl;
}
/**
Loads and decrypt the class file bytes.
@param name the class name
@return an array with the class file bytes
*/
private byte[] loadClassBytes(String name)
throws IOException
{
String cname = name.replace('.', '/') + ".caesar";
FileInputStream in = null;
in = new FileInputStream(cname);
try
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1)
{
byte b = (byte) (ch - key);
buffer.write(b);
}
in.close();
return buffer.toByteArray();
}
finally
{
in.close();
}
}
private int key;
}
1)ProGuard 是一个免费的 Java类文件的压缩,优化,混肴器。它删除没有用的类,字段,方法与属性。使字节码最大程度地优化,使用简短且无意义的名字来重命名类、字段和方法 。
2)JavaGuard是一个通用的字节码模糊器,旨在容易地适合你的规则建造和测试进程,保证你的有价值的代码更安全,使其不易被反编译以及其它形式的反向处理。
3)RetroGuard是不错的Java混淆器,在JBuilder7的企业版中也带了这个混淆器。
4)JODE包含一个Java混淆器与一个Java优化器。通过一个脚本文件可以控制Class文件的多种优化方式。
重写ClassLoader的部分方法,对加载进来的.class进行字节加减来实现基本的加密和解秘
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
This program demonstrates a custom class loader that decrypts
class files.
*/
public class ClassLoaderTest
{
public static void main(String[] args)
{
JFrame frame = new ClassLoaderFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
This frame contains two text fields for the name of the class
to load and the decryption key.
*/
class ClassLoaderFrame extends JFrame
{
public ClassLoaderFrame()
{
setTitle("ClassLoaderTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLayout(new GridBagLayout());
add(new JLabel("Class"), new GBC(0, 0).setAnchor(GBC.EAST));
add(nameField, new GBC(1, 0).setWeight(100, 0).setAnchor(GBC.WEST));
add(new JLabel("Key"), new GBC(0, 1).setAnchor(GBC.EAST));
add(keyField, new GBC(1, 1).setWeight(100, 0).setAnchor(GBC.WEST));
JButton loadButton = new JButton("Load");
add(loadButton, new GBC(0, 2, 2, 1));
loadButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
runClass(nameField.getText(), keyField.getText());
}
});
pack();
}
/**
Runs the main method of a given class.
@param name the class name
@param key the decryption key for the class files
*/
public void runClass(String name, String key)
{
try
{
ClassLoader loader = new CryptoClassLoader(Integer.parseInt(key));
Class c = loader.loadClass(name);
String[] args = new String[] {};
Method m = c.getMethod("main", args.getClass());
m.invoke(null, (Object) args);
}
catch (Throwable e)
{
JOptionPane.showMessageDialog(this, e);
}
}
private JTextField keyField = new JTextField("3", 4);
private JTextField nameField = new JTextField(30);
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
}
/**
This class loader loads encrypted class files.
*/
class CryptoClassLoader extends ClassLoader
{
/**
Constructs a crypto class loader.
@param k the decryption key
*/
public CryptoClassLoader(int k)
{
key = k;
}
protected Class findClass(String name)
throws ClassNotFoundException
{
byte[] classBytes = null;
try
{
classBytes = loadClassBytes(name);
}
catch (IOException e)
{
throw new ClassNotFoundException(name);
}
Class cl = defineClass(name, classBytes, 0, classBytes.length);
if (cl == null)
throw new ClassNotFoundException(name);
return cl;
}
/**
Loads and decrypt the class file bytes.
@param name the class name
@return an array with the class file bytes
*/
private byte[] loadClassBytes(String name)
throws IOException
{
String cname = name.replace('.', '/') + ".caesar";
FileInputStream in = null;
in = new FileInputStream(cname);
try
{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1)
{
byte b = (byte) (ch - key);
buffer.write(b);
}
in.close();
return buffer.toByteArray();
}
finally
{
in.close();
}
}
private int key;
}