久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Android開發(fā)?實現(xiàn)SSL握手

 用勿龍潛 2012-03-19

Android開發(fā) 實現(xiàn)SSL握手

  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";//使用協(xié)議 
      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監(jiān)聽,,所以簡單采用單線程形式,,并且僅僅接受客戶端的消息,并且返回客戶端指定消息 
      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,,創(chuàng)建ServerSocket 
              serverSocket = (SSLServerSocket) ctx.getServerSocketFactory

  ().createServerSocket(SERVER_PORT);  
          } catch (Exception e) { 
              System.out.println(e); 
          } 
      } 
  }
客戶端:
  Java代碼

  public class MySSLSocket extends Activity { 
      private static final int SERVER_PORT = 50030;//端口號 
      private static final String SERVER_IP = "218.206.176.146";//連接IP 
      private static final String CLIENT_KET_PASSWORD = "123456";//私鑰密碼 
      private static final String CLIENT_TRUST_PASSWORD = "123456";//信任證書密碼 
      private static final String CLIENT_AGREEMENT = "TLS";//使用協(xié)議 
      private static final String CLIENT_KEY_MANAGER = "X509";//密鑰管理器 
      private static final String CLIENT_TRUST_MANAGER = "X509";// 
      private static final String CLIENT_KEY_KEYSTORE = "BKS";//密庫,,這里用的是BouncyCastle密庫 
      private static final String CLIENT_TRUST_KEYSTORE = "BKS";// 
      private static final String ENCONDING = "utf-8";//字符集 
      private SSLSocket Client_sslSocket; 
      private Log tag; 
      private TextView tv; 
      private Button btn; 
      private Button btn2; 
      private Button btn3; 
      private EditText et; 
     
      /** Called when the activity is first created. */ 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
          super.onCreate(savedInstanceState); 
          setContentView(R.layout.main); 
          tv = (TextView) findViewById(R.id.TextView01); 
          et = (EditText) findViewById(R.id.EditText01); 
          btn = (Button) findViewById(R.id.Button01); 
          btn2 = (Button) findViewById(R.id.Button02); 
          btn3 = (Button) findViewById(R.id.Button03); 
         
          btn.setOnClickListener(new Button.OnClickListener(){ 
              @Override 
              public void onClick(View arg0) { 
                  if(null != Client_sslSocket){ 
                      getOut(Client_sslSocket, et.getText().toString()); 
                      getIn(Client_sslSocket); 
                      et.setText(""); 
                  } 
              } 
          }); 
          btn2.setOnClickListener(new Button.OnClickListener(){ 
              @Override 
              public void onClick(View arg0) { 
                  try { 
                      Client_sslSocket.close(); 
                      Client_sslSocket = null; 
                  } catch (IOException e) { 
                      e.printStackTrace(); 
                  } 
              } 
          }); 
          btn3.setOnClickListener(new View.OnClickListener(){ 
              @Override 
              public void onClick(View arg0) { 
                  init(); 
                  getIn(Client_sslSocket); 
              } 
          }); 
      } 
     
      public void init() { 
          try { 
              //取得SSL的SSLContext實例 
              SSLContext sslContext = SSLContext.getInstance(CLIENT_AGREEMENT); 
              //取得KeyManagerFactory和TrustManagerFactory的X509密鑰管理器實例 
              KeyManagerFactory keyManager = KeyManagerFactory.getInstance(CLIENT_KEY_MANAGER);  
              TrustManagerFactory trustManager = TrustManagerFactory.getInstance(CLIENT_TRUST_MANAGER); 
              //取得BKS密庫實例 
              KeyStore kks= KeyStore.getInstance(CLIENT_KEY_KEYSTORE); 
              KeyStore tks = KeyStore.getInstance(CLIENT_TRUST_KEYSTORE); 
              //加客戶端載證書和私鑰,通過讀取資源文件的方式讀取密鑰和信任證書 
              kks.load(getBaseContext().getResources()  .openRawResourc(R.drawable.kclient),CLIENT_KET_PASSWORD.toCharArray()); 
              tks.load(getBaseContext().getResources().openRawResource(R.drawable.lt_client),CLIENT_TRUST_PASSWORD.toCharArray()); 
              //初始化密鑰管理器 
              keyManager.init(kks,CLIENT_KET_PASSWORD.toCharArray()); 
              trustManager.init(tks); 
              //初始化SSLContext 
              sslContext.init(keyManager.getKeyManagers(),trustManager.getTrustManagers(),null); 
              //生成SSLSocket 
              Client_sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(SERVER_IP,SERVER_PORT); 
          } catch (Exception e) { 
              tag.e("MySSLSocket",e.getMessage()); 
          } 
      } 
          
      public void getOut(SSLSocket socket,String message){ 
          PrintWriter out; 
          try { 
              out = new PrintWriter( 
                      new BufferedWriter( 
                              new OutputStreamWriter( 
                                      socket.getOutputStream() 
                                      ) 
                              ),true); 
              out.println(message); 
          } catch (IOException e) { 
              e.printStackTrace(); 
          } 
      } 
     
      public void getIn(SSLSocket socket){ 
          BufferedReader in = null; 
          String str = null; 
          try { 
              in = new BufferedReader( 
                      new InputStreamReader( 
                              socket.getInputStream())); 
              str = new String(in.readLine().getBytes(),ENCONDING); 
          } catch (UnsupportedEncodingException e) { 
              e.printStackTrace(); 
          } catch (IOException e) { 
              e.printStackTrace(); 
          } 
          new AlertDialog 
          .Builder(MySSLSocket.this) 
          .setTitle("服務器消息") 
          .setNegativeButton("確定", null) 
          .setIcon(android.R.drawable.ic_menu_agenda) 
          .setMessage(str) 
          .show(); 
      } 
  }

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導購買等信息,謹防詐騙,。如發(fā)現(xiàn)有害或侵權內(nèi)容,,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多