編輯:Android 開發實例
在 Android 上使用 SSL,私鑰與信任憑證必須是 BKS 格式。這是因為 Java 標準環境並未內建 BouncyCastle 這個加密函式庫,而 Android 原生採用它。
實務上的做法是:先設定好本地 JDK,讓 keytool 能輸出 BKS 格式,再據此產生你要的私鑰與憑證檔。若用預設的 keytool 直接產生,你會得到 JKS 格式,Android 端讀不了。
服務端這邊使用 Java 自帶的 JKS 加密庫即可:
public class SSLServer {
private static final int SERVER_PORT = 50030;
private static final String SERVER_KEY_PASSWORD = "123456";
private static final String SERVER_AGREEMENT = "TLS"; // 使用的協議
private static final String SERVER_KEY_MANAGER = "SunX509"; // 密鑰管理器
private static final String SERVER_KEY_KEYSTORE = "JKS"; // 密庫,這裡用 Java 自帶的
private static final String SERVER_KEYSTORE_PATH = "src/data/kserver.keystore"; // 密庫路徑
private SSLServerSocket serverSocket;
public static void main(String[] args) {
SSLServer server = new SSLServer();
server.init();
server.start();
}
public void init() {
try {
// 取得 SSLContext
SSLContext ctx = SSLContext.getInstance(SERVER_AGREEMENT);
// 取得 SunX509 私鑰管理器
KeyManagerFactory kmf = KeyManagerFactory.getInstance(SERVER_KEY_MANAGER);
// 取得 JKS 密庫實例
KeyStore ks = KeyStore.getInstance(SERVER_KEY_KEYSTORE);
// 載入服務端私鑰
ks.load(new FileInputStream(SERVER_KEYSTORE_PATH),
SERVER_KEY_PASSWORD.toCharArray());
// 初始化密鑰管理器
kmf.init(ks, SERVER_KEY_PASSWORD.toCharArray());
// 初始化 SSLContext
ctx.init(kmf.getKeyManagers(), null, null);
// 透過 SSLContext 取得 ServerSocketFactory,建立 ServerSocket
serverSocket = (SSLServerSocket) ctx.getServerSocketFactory()
.createServerSocket(SERVER_PORT);
} catch (Exception e) {
System.out.println(e);
}
}
// 這裡不是要示範 Socket 監聽,因此簡單採用單執行緒,
// 只接收客戶端訊息並回傳固定內容
public void start() {
if (serverSocket == null) {
System.out.println("ERROR");
return;
}
while (true) {
try {
System.out.println("Server Side......");
Socket s = serverSocket.accept();
InputStream input = s.getInputStream();
OutputStream output = s.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(input);
BufferedOutputStream bos = new BufferedOutputStream(output);
byte[] buffer = new byte[20];
bis.read(buffer);
System.out.println(new String(buffer));
bos.write("This is Server".getBytes());
bos.flush();
s.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
整個 SSL 建置可以看成四層堆疊,順序不能顛倒:
這是最值得記住的一點:換成 SSL 之後,你的 I/O 程式碼幾乎不用改。差別只在於物件是從哪個 Factory 產生的。握手、憑證驗證、加解密全部由底層代為處理。
客戶端(Android 端)在初始化 SSLContext 時,第二個參數就必須明確提供 TrustManager,用來載入你的 BKS 憑證檔——否則預設信任庫裡沒有你的自簽憑證,握手會直接失敗並拋出 SSLHandshakeException。
網路上常見的「自訂 TrustManager 全部信任、不做任何驗證」寫法雖然能讓連線成功,但等於完全關閉了 SSL 的保護作用,中間人攻擊可以輕鬆得手。除非是內部測試環境,正式產品不該採用。
另外,範例中的密碼直接寫在程式碼裡,這在真實專案中應改為從安全儲存機制讀取,並避免把憑證檔與其密碼一同打包進 apk。
Android 自訂 Toast:讓提示訊息符合 App 的視覺風格
系統預設的 Toast 簡潔好用,一行 makeText 就能搞定。問題是它長得實在太「系統」了,放在一個講究視覺設計的 App 裡總顯得格格不入。好在 Toast 的樣
把簡訊備份到 Gmail:Android 短信打包寄送的實作拆解
這個小工具的功能是把指定期間內的簡訊打包,透過系統分享流程寄到自己的 Gmail 信箱,前提是手機上已設定好 Gmail 帳號。 專案原始碼:https://github
Android LayoutInflater 的使用介紹
LayoutInflater 常用來將 XML 布局實例化成 View 物件。它與 findViewById 不同,後者是用來查找已經存在的控制項,而前者則是把布局檔案轉
Android 學習筆記:單元測試實例完整解析
本文以實例方式,完整說明如何在 Android 專案中設定並執行單元測試。測試是開發流程中不可或缺的一環,透過 JUnit 框架能有效驗證程式的正確性。第一步:修改 An