Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發 實現SSL握手

Android開發 實現SSL握手

編輯:Android開發實例

  Android的私鑰和信任證書的格式必須是BKS格式的,通過配置本地JDK,讓keytool可以生成BKS格式的私鑰和信任證書,java本身沒有BouncyCastle密庫。

  服務端:
  Java代碼

  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(); 
      } 
 
      //由於該程序不是演示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); 
              } 
          } 
      } 
     
      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); 
          } 
      } 
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved