Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2788695
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: Java

2012-07-31 15:44:13

对一个二进制文件进行加密,令java的类加载器无法识别(不符合类文件结构)

点击(此处)折叠或打开

  1. package cn.itcast.cl;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;

  8. public class MyClassLoader extends ClassLoader{

  9.     /**把源目录下的二进制文件加密,生成到itcastlib下
  10.      * @param args
  11.      * 源路径G:\Users\king\eclipsespace\classloader\bin\cn\itcast\loadedclass\Attachment.class
  12.      * dest:itcastlib
  13.      */
  14.     public static void main(String[] args) throws Exception {
  15.         // TODO Auto-generated method stub
  16.         String srcPath = args[0];
  17.         String destDir = args[1];
  18.         FileInputStream fis = new FileInputStream(srcPath);
  19.         String destFileName = srcPath.substring(srcPath.lastIndexOf('\\')+1);
  20.         String destPath = destDir + "\\" + destFileName;
  21.         FileOutputStream fos = new FileOutputStream(destPath);
  22.         cypher(fis,fos);
  23.         fis.close();
  24.         fos.close();
  25.     }
  26.     
  27.     //就异或一下而已,当然可以用更复杂的算法对二进制流进行加密
  28.     private static void cypher(InputStream ips ,OutputStream ops) throws Exception{
  29.         int b = -1;
  30.         while((b=ips.read())!=-1){
  31.             ops.write(b ^ 0xff);
  32.         }
  33.     }

  34.     private String classDir;
  35.     public MyClassLoader(){
  36.         
  37.     }
  38.     public MyClassLoader(String classDir){
  39.         this.classDir = classDir;
  40.     }
  41. }

  42. //被加载的类
  43. package cn.itcast.loadedclass;
  44. import java.util.Date;

  45. public class Attachment extends Date 
  46. {
  47. public String toString(){
  48. return "hello,itcast";
  49. }
  50. package cn.itcast.cl;
  51. import cn.itcast.loadedclass.Attachment;
  52. public class CLTest
  53. {
  54.     /**
  55.      * @param args
  56.      */
  57.     public static void main(String[] args)
  58.     {
  59.         System.out.println(CLTest.class.getClassLoader().getClass().getName());
  60.         System.out.println("C++ bootstrap "+System.class.getClassLoader());
  61.         System.out.println("xxx");
  62.         ClassLoader loader = CLTest.class.getClassLoader();
  63.         while(loader != null){
  64.             System.out.println(loader.getClass().getName());
  65.             loader = loader.getParent();
  66.         }
  67.         System.out.println("循环递归向上寻找: "+loader+"\n");
  68.         System.out.println("这是还没把加密类替换原来的类,运行没出错");
  69.         Attachment attachment= new Attachment();
  70.         System.out.println(attachment.toString());
  71.     }

  72. }

  73. sun.misc.Launcher$AppClassLoader
  74. C++ bootstrap null
  75. xxx
  76. sun.misc.Launcher$AppClassLoader
  77. sun.misc.Launcher$ExtClassLoader
  78. 循环递归向上寻找: null

  79. 这是还没把加密类替换原来的类,运行没出错
  80. hello,itcast

当我把生成的加密类,替换掉以后,一运行马上抛异常,因为加载器无法识别这些乱七八糟的魔数,这样的话就要自己写类加载器来加载了

Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 889275713 in class file cn/itcast/loadedclass/Attachment

at cn.itcast.cl.CLTest.main(CLTest.java:20)

阅读(1202) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~