首先編寫一個加密算法,對文件進行加密
package javatribe.fts.generic;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MyClassLoader {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//源文件路徑名
String srcPath=args[0];
//目標路徑名
String destDir=args[1];
//獲得輸入流
FileInputStream fis=new FileInputStream(srcPath);
//獲得目標的文件名,即加密之後的文件名稱
String destFileName=srcPath.substring(srcPath.lastIndexOf('\\')+1);
//目標的路徑,即加密之後文件傳輸到的路徑
String destPath=destDir+'\\'+destFileName;
//輸出流
FileOutputStream fos=new FileOutputStream(destPath);
//進行加密運算
cypher(fis,fos);
fis.close();
fos.close();
}
private static void cypher(InputStream ips,OutputStream ops) throws Exception{
int b=-1;
while((b=ips.read())!=-1){
ops.write(b ^ 0xff);
}
}
}
對文件ClassLoaderAttachment進行加密
package javatribe.fts.generic;
import java.util.Date;
public class ClassLoaderAttachment extends Date {
public String toString(){
return "Hello,World";
}
}
進行加密後的類系統自帶的類加載是無法讀取的,會提示錯誤
自己編寫的加密工具需要對class文件進行解密後系統自帶的編譯工具才能夠解釋運行,接下來可以自己編寫一個簡單的類加載器對加密後的文件進行解密加載