加密JAVA源码

有多个工具:
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;
}
 
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值