Java類載入器( CLassLoader ) 死磕7: 基於加密的自定義網路載入器 本小節目錄
【正文】Java類載入器( CLassLoader ) 死磕7:
基於加密的自定義網路載入器
本小節目錄
7.1. 加密傳輸Server端的原始碼
7.2. 加密傳輸Client端的原始碼
7.3. 使用亦或實現簡單加密和解密演算法
7. 網路加密SafeClassLoader的原始碼
7.5. SafeSocketLoader的使用
眾所周知,java程式碼很容易被反編譯,如果你需要把自己的程式碼進行加密,可以先將編譯後的程式碼用某種加密演算法加密,然後結合自己的網路類載入器,進行加密後的安全傳輸。
客戶端接收到加密後的位元組碼後,負責將這段加密後的程式碼還原。
1.1.1. 加密傳輸Server端的原始碼
和檔案傳輸Server端的原始碼基本一致,只有一行程式碼的差別。
簡單粗暴,直接上碼。
package com.crazymakercircle.classLoader; import com.crazymakercircle.config.SystemConfig; import com.crazymakercircle.util.DeEnCode; import com.crazymakercircle.util.IOUtil; import com.crazymakercircle.util.Logger; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * 檔案傳輸Server端<br> */ public class SafeSocketServer { ServerSocket serverSocket = null; static String filePath = null; public SafeSocketServer() throws Exception { serverSocket = new ServerSocket(SystemConfig.SOCKET_SERVER_PORT); this.filePath = SystemConfig.CLASS_SERVER_PATH; startServer(); } /** * 啟動服務端 * 使用執行緒處理每個客戶端傳輸的檔案 * * @throws Exception */ public void startServer() { while (true) { // server嘗試接收其他Socket的連線請求,server的accept方法是阻塞式的 Logger.info("server listen at:" + SystemConfig.SOCKET_SERVER_PORT); Socket socket = null; try { socket = serverSocket.accept(); // 每接收到一個Socket就建立一個新的執行緒來處理它 new Thread(new Task(socket)).start(); } catch (Exception e) { e.printStackTrace(); } } } /** * 處理客戶端傳輸過來的檔案執行緒類 */ class Task implements Runnable { private Socket socket; private DataInputStream dis; private FileOutputStream fos; public Task(Socket socket) { this.socket = socket; } @Override public void run() { try { dis = new DataInputStream(socket.getInputStream()); // 檔名和長度 String fileName = dis.readUTF(); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); sendFile(fileName, dos); } catch (Exception e) { e.printStackTrace(); } finally { IOUtil.closeQuietly(fos); IOUtil.closeQuietly(dis); IOUtil.closeQuietly(socket); } } private void sendFile(String fileName, DataOutputStream dos) throws Exception { fileName=classNameToPath(fileName); fileName = SafeSocketServer.filePath + File.separator + fileName; File file = new File(fileName); if (!file.exists()) { throw new Exception("file not found! :"+fileName); } long fileLen = file.length(); dos.writeLong(fileLen); dos.flush(); byte one= (byte) 0xff; FileInputStream fis = new FileInputStream(file); // 開始傳輸檔案 Logger.info("======== 開始傳輸檔案 ========"); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; while ((length = fis.read(bytes, 0, bytes.length)) != -1) { DeEnCode.encode(bytes,length); dos.write(bytes, 0, length); dos.flush(); progress += length; Logger.info("| " + (100 * progress / fileLen) + "% |"); } Logger.info("======== 檔案傳輸成功 ========"); } } private String classNameToPath(String className) { return className.replace('.', '/') + ".class"; } public static void main(String[] args) { try { SafeSocketServer socketServer = new SafeSocketServer(); socketServer.startServer(); } catch (Exception e) { e.printStackTrace(); } } }
和檔案傳輸Server端的原始碼基本一致,只有一行程式碼的差別。這個類僅僅增加的一行是:
DeEnCode.encode(bytes,length);
其目的是,在傳送位元組碼之前,使用定義的加密函式,進行位元組碼加密。
原始碼比較長,建議執行main函式,先將服務端的原始碼跑起來,然後再閱讀程式碼,這樣閱讀起來更加容易懂。
另外,在使用基於網路的類載入器之前,一定要確保服務端的程式碼先執行。否則客戶端會報錯。
案例路徑:com.crazymakercircle.classLoader.SafeSocketServer
案例提示:無程式設計不創客、無案例不學習。一定要跑案例哦
執行的結果是:
<clinit> |> 開始載入配置檔案到SystemConfig loadFromFile |> load properties: /system.properties startServer |> server listen at:18899
看到以上結果,表示服務端開始啟動。監聽了18899埠,等待客戶端的連線。
1.1.2. 加密傳輸Client端的原始碼
客戶端的工作:
建立和伺服器的TCP連線後,首先做的第一步工作,是傳送檔名稱給伺服器端。
然後阻塞,直到伺服器的資料過來。客戶端開始接受伺服器傳輸過來的資料。接受資料的工作由函式receivefile()完成。
整個的資料的讀取工作分為兩步,先讀取檔案的大小,然後讀取傳輸過來的檔案內容。
在傳輸檔案內容的位元組碼時,需要對位元組碼進行解密。
簡單粗暴,直接上原始碼。
/** * 檔案傳輸Client端 */ public class SafeSocketClient { private Socket client; private FileInputStream fis; private DataOutputStream dos; /** * 建構函式<br/> * 與伺服器建立連線 * * @throws Exception */ public SafeSocketClient() throws IOException { this.client = new Socket( SystemConfig.SOCKET_SERVER_IP, SystemConfig.SOCKET_SERVER_PORT ); Logger.info("Cliect[port:" + client.getLocalPort() + "] 成功連線服務端"); } /** *向服務端去取得檔案 * * @throws Exception */ public byte[] getFile(String fileName) throws Exception { byte[] result = null; try { dos = new DataOutputStream(client.getOutputStream()); // 檔名和長度 dos.writeUTF(fileName); dos.flush(); DataInputStream dis = new DataInputStream(client.getInputStream()); result = receivefile(dis); Logger.info("檔案接收成功,File Name:" + fileName); } catch (Exception e) { e.printStackTrace(); } finally { IOUtil.closeQuietly(fis); IOUtil.closeQuietly(dos); IOUtil.closeQuietly(client); } return result; } public byte[] receivefile(DataInputStream dis) throws Exception { int fileLength = (int) dis.readLong(); ByteArrayOutputStream bos = new ByteArrayOutputStream(fileLength); long startTime = System.currentTimeMillis(); Logger.info("block IO 傳輸開始:"); // 開始接收檔案 byte[] bytes = new byte[1024]; int length = 0; while ((length = dis.read(bytes, 0, bytes.length)) != -1) { DeEnCode.decode(bytes,length); bos.write(bytes, 0, length); bos.flush(); } Logger.info(" Size:" + IOUtil.getFormatFileSize(fileLength)); long endTime = System.currentTimeMillis(); Logger.info("block IO 傳輸毫秒數:" + (endTime - startTime)); bos.flush(); byte[] result = bos.toByteArray(); IOUtil.closeQuietly(bos); return result; } }
與前面的基礎案例SafeSocketClient 相比,只有一行程式碼的差別。這個類僅僅增加的一行是:
DeEnCode.decode(bytes,length);
其目的是,在接受位元組碼之後,使用定義的解密函式,進行位元組碼解密。
案例路徑:com.crazymakercircle.classLoader.SafeSocketClient
此案例類沒法獨立執行,因為沒有執行的入口。只能作為基礎類,供其他類呼叫。
下面介紹一下加密和解密的演算法。
1.1.3. 使用亦或實現簡單加密和解密演算法
加密和解密演算法有很多,這裡為為了演示方便,使用亦或的方式,實現簡單加密和解密演算法。
簡單粗暴,直接上程式碼:
public class DeEnCode { private static final String key0 = "FECOI()*&<MNCXZPKL"; private static final Charset charset = Charset.forName("UTF-8"); private static byte[] keyBytes = key0.getBytes(charset); public static void encode(byte[] b, int length) { for (int i = 0, size =length; i < size; i++) { for (byte kb : keyBytes) { b[i] = (byte) (b[i] ^ kb); } } } public static void decode(byte[] dee, int length) { for (int i = 0, size =length; i < size; i++) { for (byte kb : keyBytes) { dee[i] = (byte) (dee[i] ^ kb); } } } public static void main(String[] args) { String s = "you are right ok 測試"; byte[] sb = s.getBytes(); encode(sb,sb.length); decode(sb, sb.length); Logger.info(new String(sb)); } }
案例路徑:com.crazymakercircle.util.DeEnCode
無程式設計不創客、無案例不學習。一定要跑案例哦
執行上面的main方法,測試一下加密和解密。結果是:
main |> you are right ok 測試
從結果可以看到,解密後的資料和加密前的資料是一致的。說明這組演算法是沒有問題的。
1.1.4. 網路加密SafeClassLoader的原始碼
與前面的網路類載入器SocketClassLoader,只有一行之差。
原始碼如下:
public class SafeClassLoader extends ClassLoader {
public SafeClassLoader() {
}
protected Class<?> findClass(String name)
throws ClassNotFoundException {
Logger.info("findClass name = " + name);
byte[] classData = null;
try {
SafeSocketClient client = new SafeSocketClient();
classData = client.getFile(name);
} catch (Exception e) {
e.printStackTrace();
throw new ClassNotFoundException();
}
if (classData == null) {
throw new ClassNotFoundException();
} else {
return defineClass(name, classData, 0, classData.length);
}
}
}
1.1.5. SafeSocketLoader的使用
簡單粗暴,先上程式碼:
package com.crazymakercircle.classLoaderDemo.net; import com.crazymakercircle.classLoader.SafeClassLoader; import com.crazymakercircle.config.SystemConfig; import com.crazymakercircle.petStore.pet.IPet; import com.crazymakercircle.util.ClassLoaderUtil; import com.crazymakercircle.util.Logger; public class SafeNetDemo { public static void testLoader() { try { SafeClassLoader classLoader = new SafeClassLoader(); String className = SystemConfig.PET_DOG_CLASS; Class dogClass = classLoader.loadClass(className); Logger.info("顯示dogClass的ClassLoader =>"); ClassLoaderUtil.showLoader4Class(dogClass); IPet pet = (IPet) dogClass.newInstance(); pet.sayHello(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } public static void main(String[] args) { testLoader(); } }
案例路徑:com.crazymakercircle.classLoaderDemo.base.SafeSocketDemo
案例提示:無程式設計不創客、無案例不學習。一定要跑案例哦
執行的結果,與前面的SocketLoaderDemo結果是相同的,這裡不在贅述。
原始碼:
程式碼工程: classLoaderDemo.zip
下載地址:在瘋狂創客圈QQ群檔案共享。
瘋狂創客圈:如果說Java是一個武林,這裡的聚集一群武痴, 交流程式設計體驗心得
QQ群連結:瘋狂創客圈QQ群
無程式設計不創客,無案例不學習。 一定記得去跑一跑案例哦
類載入器系列全目錄