开源的东西象菜市场的菜 ,要自己挑 关于加密,其实就是加减BYTE,算法当然是重头戏,给个简单例子你看下,加密CLASS类,加密文件也一样 /** @version 1.21 2004-09-11 @author Cay Horstmann */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; }
关于加密,其实就是加减BYTE,算法当然是重头戏,给个简单例子你看下,加密CLASS类,加密文件也一样
/**
@version 1.21 2004-09-11
@author Cay Horstmann
*/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;
}
但混淆的效果似乎不好,因为只能混淆一些局部变量等简单的东西。
几年前关注过这块,不知道现在做得如何了。
-------------------
这位朋友很强的说,怎么会找到这么多的好例子哦.呵呵.强个~~.
lixiaoxue85(蛮野蛮) ( ) 信誉:97 Blog 加为好友 2007-4-27 18:25:42 得分: 0 本来就不弱。