前言:本博客內容由張孝祥Java高新技術整理而來
class加密部分
項目目錄結構
ClassLoaderAttach
package com.dao.chu.myloader;
import java.util.Date;
@SuppressWarnings("serial")
public class ClassLoaderAttach extends Date{
@Override
public String toString() {
return "this is ClassLoaderAttach";
}
}
MyClassLoader
package com.dao.chu.myloader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader {
public static void main(String[] args) throws IOException {
String srcPath = args[0];
String destDir = args[1];
String destFileName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
String destFilePath = destDir + "\\" + destFileName;
FileInputStream fin = new FileInputStream(srcPath);
FileOutputStream fout = new FileOutputStream(destFilePath);
cypher(fin,fout);
fin.close();
fout.close();
}
/**
* <p>Title: cypher</p>
* <p>Description:加密, 將原來的1改爲0,0改爲1</p>
* @param in
* @param out
* @throws IOException
*/
public static void cypher(InputStream in ,OutputStream out) throws IOException{
int b=0;
while ((b=in.read())!=-1) {
out.write(b ^ 0xff);
}
}
}
MyClassLoaderTest
package com.dao.chu.myloader;
public class MyClassLoaderTest {
public static void main(String[] args) {
ClassLoaderAttach classLoaderAttach = new ClassLoaderAttach();
System.out.println(classLoaderAttach.toString());
}
}
運行 MyClassLoader,輸入ClassLoaderAttach.class的路徑和要生成至的路徑。
運行成功後可看到有加密後的class文件生成
執行MyClassLoaderTest,可看到正常輸出
我們將加密的class文件與原來沒加密過的class文件進行替換
再次執行MyClassLoaderTest,可看到報錯信息。說明加密成功,用原來的類加載器不能運行,所以我們要用自己的類加載器運行
自定義ClassLoader部分
我們將MyClassLoader修改如下
package com.dao.chu.myloader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader extends ClassLoader{
public static void main(String[] args) throws IOException {
String srcPath = args[0];
String destDir = args[1];
String destFileName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
String destFilePath = destDir + "\\" + destFileName;
FileInputStream fin = new FileInputStream(srcPath);
FileOutputStream fout = new FileOutputStream(destFilePath);
cypher(fin,fout);
fin.close();
fout.close();
}
/**
* <p>Title: cypher</p>
* <p>Description:加密, 將原來的1改爲0,0改爲1</p>
* @param in
* @param out
* @throws IOException
*/
public static void cypher(InputStream in ,OutputStream out) throws IOException{
int b=0;
while ((b=in.read())!=-1) {
out.write(b ^ 0xff);
}
}
private String classDir;
public MyClassLoader(){
}
public MyClassLoader(String classDir){
this.classDir=classDir;
}
/**
* 覆蓋ClassLoader的findClass方法
*/
@SuppressWarnings("deprecation")
@Override
protected Class<?> findClass(String name)
throws ClassNotFoundException {
String classFileName = classDir + "\\" + name + ".class";
try {
FileInputStream fin = new FileInputStream(classFileName);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
//解密
cypher(fin,baout);
fin.close();
//轉爲字節數組
byte[] byteArray = baout.toByteArray();
return defineClass(byteArray, 0, byteArray.length);
} catch (Exception e) {
}
return null;
}
}
MyClassLoaderTest修改如下
package com.dao.chu.myloader;
import java.util.Date;
public class MyClassLoaderTest {
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
// ClassLoaderAttach classLoaderAttach = new ClassLoaderAttach();
// System.out.println(classLoaderAttach.toString());
Class c = new MyClassLoader("myloaderlib").loadClass("ClassLoaderAttach");
Date d = (Date)c.newInstance();
System.out.println(d);
}
}
測試前我們需將原來生成的ClassLoaderAttach.class刪掉,避免父類加載影響結果
最後執行MyClassLoaderTest
正是我們想要的結果