日期:2014-05-20  浏览次数:21007 次

java代码加密
怎样给java源码加密呢,我一点方向感也没有,希望大家提提意见。

------解决方案--------------------
加密源码?......

加密字节码的话,现在有不少的混淆工具,用来防止反编译,但java的混淆/加密效果实在很差劲,一般都比较容易破解。

因为字节码本身太容易阅读了!!即使没有专门的破解工具,单单Eclipse就足够强大了,
即使没有源代码,也可以在任何一个方法上加断点,可以在任何一个字段上加存、取断点,
又加上你可以控制ClassLoader。。。
------解决方案--------------------
有多个工具:
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)