简单的class文件加密解密

加密生成的class文件

package com.dasenlin.encrpt;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 测试取反操作
 * @author Administrator
 *
 */
public class EncriptUtil {

    /**
     * @param args
     */
    public static void main(String[] args) {
        encrpt("E:/LearnJavaProjectText/myjava/HelloWorld.class","E:/LearnJavaProjectText/mytemp/HelloWorld.class");
    }
    /**
     * 测试的是取反
     * @param src
     * @param dest
     */
    public static void encrpt(String src,String dest){
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dest);
            int temp =-1;
            while((temp=fis.read())!=-1){
                fos.write(temp^0xff);//读出的数据进行取反;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(null!=fis){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

解密加密后的class文件的类加载器

package com.dasenlin.encrpt;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 解密的类加载器
 * @author Administrator
 *
 */
public class DecriptClassLoader extends ClassLoader {
private String rootDir;

    public DecriptClassLoader(String rootDir){
        this.rootDir=rootDir;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        Class<?> c = findLoadedClass(name);
        if(c!=null){
            return c;
        }else{
            ClassLoader parent=this.getParent();
            try {
                c=parent.loadClass(name);
            } catch (Exception e) {
                //e.printStackTrace();
            }    //双亲委托机制,委派给父类加载。
            if(c!=null){
                return c;
            }else{
                byte[]classData = getClassData(name);
                if(classData==null){
                    throw new ClassNotFoundException();
                }else{
                     c=defineClass(name,classData,0,classData.length);
                }
            }
        }
        return c;
    }

    private byte[] getClassData(String name) {
        String path=rootDir+"/"+name.replace('.', '/')+".class";
        InputStream is =null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
             is=new FileInputStream(path);
             int temp =-1;
                while((temp=is.read())!=-1){
                    baos.write(temp^0xff);//读出的数据进行取反;
            }
            return baos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

测试

package com.dasenlin.encrpt;
/**
 * 通过自定义的解密类加载器加载
 * @author Administrator
 *
 */
public class DemoLoad {

    /**
     * @param args
     */
    public static void main(String[] args) {
        DecriptClassLoader loader = new DecriptClassLoader("E:/LearnJavaProjectText/mytemp/");
        try {
            Class<?> c =loader.loadClass("HelloWorld");
            System.out.println(c);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
软件介绍 本工具是对java class文件进行加密保护防止反编译的工具!本工具全面支持linux/unix/windows操作系统。 继推出v1.0版本后,获得了用户大量的支持与的反馈,我们再次推出本v2.0版,对加密算法进行了更大的改进,安全性大大提升! 众所周知,java编译后的class文件是一种中间字节字文件, 很容易被反编译工具反编译,而传统的java源代码保护方法基本都是采用混淆的方式, 但这样会带来很多麻烦,而且也不能真正保护class文件, 本工具是对class文件进行加密,采用jni的方式解密运行, 加密算法达到256位,加密后的class文件不可能被破解,反编译工具也对加密后的class文件无能为力。 运行方式: 运行时,要能正确的加载加密后的class文件, 必须使用我们提供的动态链接库classloader.dll(windows操作系统)或者libclassloader.so(Linux、Unix操作系统)。 执行java时带上参数-agentlib:\classloader 注意此处不要后缀名.dll(或者.so)。 如: 我把classloader.dll放在C:\目录下; 运行加密后的class文件命令如下: windows下执行java: java -agentlib:C:\classloader Sample Linux、Unix等系列操作系统下执行java: java -agentlib:/home/classloader Sample 或者把libclassloader.so拷贝到如“/home/yzj/jdk1.6.0_23/jre/lib/i386/”这jdk的运行目录下, 然后执行java如:java -agentlib:classloader Sample 当然如果class文件加密,这样运行也不会出错! 应用场合: 独立的应用程序,运行java时,带上参数-agentlib:\classloader Tomcat、Jboss等Java application server修改启动脚本, 把执行java的命令行后面加上参数-agentlib:\classloader 适应环境: 操作系统:所有操作系统,Windows系统、Linux/Unix,只是运行时, 使用的动态链接库文件不一样而已,其它配置完全一样。 jdk必须1.5以上。 备注:如果下载站下载下来的程序有运行bug,请从上面两个下载地址更新软件。并给我们留言!谢谢... 升级提示:v2.1相比v2.0修改了一个注册bug。
以下是Java使用异或方式对文件进行加密解密的示例代码: 加密: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class XorEncryptor { public static void encrypt(File inputFile, File outputFile, String key) throws IOException { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int len; int index = 0; while ((len = inputStream.read(buffer)) != -1) { for (int i = 0; i < len; i++) { buffer[i] ^= key.charAt(index % key.length()); index++; } outputStream.write(buffer, 0, len); } inputStream.close(); outputStream.close(); } } ``` 解密: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class XorDecryptor { public static void decrypt(File inputFile, File outputFile, String key) throws IOException { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; int len; int index = 0; while ((len = inputStream.read(buffer)) != -1) { for (int i = 0; i < len; i++) { buffer[i] ^= key.charAt(index % key.length()); index++; } outputStream.write(buffer, 0, len); } inputStream.close(); outputStream.close(); } } ``` 其中,encrypt方法用于加密文件,decrypt方法用于解密文件。key为加密解密的密钥。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值