java class 加密_Java加密解密class文件,使用classLoader动态解密class文件

1 /**

2 * 加解密类3 */

4 public classEdCipher {5

6 private String encryptFolder = "encrypt";7

8 /**

9 * 加密方法10 *@paramname 需要加密的文件名11 */

12 public voidencryptClass(String name) {13 String path =getFilePath(name);14 //classFile为待加密的class文件

15 File classFile = newFile(path);16 if (!classFile.exists()) {17 //TODO 如果文件不存在,做相应的处理。一般情况下都是抛出异常;

18 } else{19 //folder 是准备在待加密的文件也就是classFIle的同级目录下创建一个文件夹,里面放着加密后的文件

20 File folder = new File(classFile.getParent() + File.separator +encryptFolder);21 if (!folder.exists()) {22 folder.mkdirs();23 }24 }25 //cipheredClass 为加密后文件的全路径文件名

26 String cipheredClass = classFile.getParent() + File.separator + encryptFolder + File.separator +classFile.getName();27 try(28 FileInputStream fileInputStream = newFileInputStream(classFile);29 BufferedInputStream bis = newBufferedInputStream(fileInputStream);30 FileOutputStream fileOutputStream = newFileOutputStream(cipheredClass);31 BufferedOutputStream bos = newBufferedOutputStream(fileOutputStream)32 ) {33 intdata;34 while ((data = bis.read()) != -1) {35 bos.write(data ^ 0xFF);36 }37 bos.flush();38 } catch(IOException e) {39 e.printStackTrace();40 }41

42 //现在将原来未加密的文件删除

43 classFile.delete();44

45 //下面这一句在文件后面加了一个“en”,最后生成的文件就是xxx.classen,这样做的目的是为了在启动服务器的时候46 //tomcat会自动检查classespath下的class文件,如果我不加上一个“en”,那么改加密文件就会被tomcat扫描到。47 //如果被扫描到了,但是它又是一个被加密后的文件,头部信息被修改了,那么tomcat就会报错,启动不起来。这算是一个小技巧。

48 File oldFile = new File(path + "en");49 if(oldFile.exists()) {50 oldFile.delete();51 }52 File cipheredFile = newFile(cipheredClass);53 cipheredFile.renameTo(oldFile);54 cipheredFile.getParentFile().delete();55 }56

57 /**

58 * 解密方法59 *@paramname 需要解密的文件名60 */

61 protected byte[] decryptClass(String name) {62 String path;63 if (!name.contains(".class")) {64 path =getDefFilePath(name);65 } else{66 path =name;67 }68 File encryptedClassFile = newFile(path);69 if (!encryptedClassFile.exists()) {70 System.out.println("decryptClass() File:" + path + " not found!");71 return null;72 }73 byte[] result = null;74 BufferedInputStream bis = null;75 ByteArrayOutputStream bos = null;76 try{77 bis = new BufferedInputStream(newFileInputStream(encryptedClassFile));78 bos = newByteArrayOutputStream();79 intdata;80 while ((data = bis.read()) != -1) {81 bos.write(data ^ 0xFF);82 }83 bos.flush();84 result =bos.toByteArray();85 } catch(Exception e) {86 e.printStackTrace();87 } finally{88 try{89 if (bis != null) {90 bis.close();91 }92 if (bos != null) {93 bos.close();94 }95 } catch(IOException e) {96 e.printStackTrace();97 }98 }99 returnresult;100 }101

102 //获取加密前文件的绝对路径

103 privateString getFilePath(String name) {104 String path;105 String str = name.substring(name.lastIndexOf(".") + 1, name.length()) + ".class";106 path = EdCipher.class.getResource(str).toString();107 path = path.substring(path.indexOf("file:/") + "file:/".length(), path.length());108 if (System.getProperty("os.name").toUpperCase().contains("LINUX")) {109 path = File.separator +path;110 }111 returnpath;112 }113

114 //获取加密后文件的绝对路径

115 privateString getDefFilePath(String name) {116 String path;117 String str = name.substring(name.lastIndexOf(".") + 1, name.length()) + ".classen";118 path = EdCipher.class.getResource(str).toString();119 path = path.substring(path.indexOf("file:/") + "file:/".length(), path.length());120 returnpath;121 }122

123 //测试

124 public static voidmain(String[] args) {125 EdCipher edCipher = newEdCipher();126 edCipher.encryptClass(args[0]);127 }128 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简要介绍 JAVA CLASS文件加密工具是一款专门为保护您的JAVA源代码而设计的软件。传统的JAVA代码保护方式通常是扰乱生成的CLASS文件,从而降低反编译生成的源代码的可读性;有的保护工具甚至能生成部分废代码(垃圾代码),使得反编译后的源码在重新编译时出现编译错误,不能直接生成CLASS文件。这种方法具有一定的效果,但是不能彻底保护您的JAVA源代码。 JAVA CLASS文件加密工具对CLASS文件进行加密保护,加密密钥高达256位(bit,即:32字节),并采用多重加密的算法,既安全又高效。加密后的CLASS文件不可能被破解;反编译工具对加密后的CLASS文件无能为力,根本就不能处理! 运行方式 运行时,加密后的CLASS文件要能正常加载,需要使用我们提供的动态库lanswon.dll。执行java时带上参数 -agentlib:<动态文件所在路径>\lanswon 注意:不要加文件后缀.dll,直接使用文件的名字部分(classloader)! 举例说明:例如,本加密工具安装在c:\lanswonsoft\java_protect,执行加密后的CLASS文件的命令行如下: java -agentlib:c:\lanswonsoft\java_protect\lanswon <您的CLASS类及参数> 应用场合 独立的应用程序(Application,自定义main方法),运行java时,带上参数-agentlib:<所在路径>\lanswon Tomcat等JAVA Web Server,修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\lanswon JBOSS等JAVA Application Server(应用服务器),修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\lanswon 适用环境 操作系统:Windows 98/2000/XP 等Windows系统 JDK:1.5.0及以上版本

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值