Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Button Send;
  2. EditText IP;
  3. EditText Port;
  4. TextView Answer;
  5. EditText Message;
  6.  
  7. private String ip_address;
  8. private int port = 11880;
  9. private SSLSocket socket = null;
  10. private BufferedReader in = null;
  11. private BufferedWriter out = null;
  12. private final String TAG = "TAG";
  13. private char keystorepass[] = "key12345".toCharArray();
  14.  
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18.     super.onCreate(savedInstanceState);
  19.     setContentView(R.layout.activity_main);
  20.  
  21.     Send = (Button) findViewById(R.id.btnSend);
  22.     IP = (EditText) findViewById(R.id.etIP);
  23.     Port = (EditText) findViewById(R.id.etPort);
  24.     Answer = (TextView) findViewById(R.id.tvAnswer);
  25.     Message = (EditText) findViewById(R.id.etMessage);
  26.  
  27.  
  28.     Send.setClickable(true);
  29.     Send.setOnClickListener(new View.OnClickListener() {
  30.  
  31.         @Override
  32.         public void onClick(View v) {
  33.  
  34.             if (IP.getText().toString().equals(null) || Port.getText().toString().equals(null)){
  35.                 Toast.makeText(v.getContext(), "Bitte IP und Port eingeben", Toast.LENGTH_LONG).show();
  36.             }else {
  37.                 String temp = Message.getText().toString();
  38.                 if (temp == null){
  39.                     temp = "Es wurde keine Nachricht eingegeben";
  40.                 }
  41.                 port = Integer.parseInt(Port.getText().toString());
  42.                 ip_address = IP.getText().toString();
  43.  
  44.                 try {
  45.                     KeyStore ks = KeyStore.getInstance("BKS");
  46.                     InputStream keyin = v.getResources().openRawResource(R.raw.neuserverkeypem);
  47.                     ks.load(keyin,keystorepass);
  48.                     org.apache.http.conn.ssl.SSLSocketFactory socketFactory = new org.apache.http.conn.ssl.SSLSocketFactory(ks);
  49.                     socketFactory.setHostnameVerifier(socketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  50.                     socket = (SSLSocket)
  51.                             socketFactory.createSocket(new Socket(ip_address,port), ip_address, port, false);
  52.                     socket.startHandshake();
  53.  
  54.                     printServerCertificate(socket);
  55.                     printSocketInfo(socket);
  56.  
  57.  
  58. out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  59.                     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  60.                     chat(temp);
  61.                 } catch (UnknownHostException e) {
  62.                     Toast.makeText(v.getContext(), "Unknown host", Toast.LENGTH_SHORT).show();
  63.                     Log.i(TAG,"Unknown host");
  64.                     //System.exit(1);
  65.                 } catch  (IOException e) {
  66.                     Toast.makeText(v.getContext(), "No I/O", Toast.LENGTH_SHORT).show();
  67.                     Log.i(TAG,"No I/O");
  68.                     e.printStackTrace();
  69.                     //System.exit(1);
  70.                 } catch (KeyStoreException e) {
  71.                     Toast.makeText(v.getContext(), "Keystore ks error", Toast.LENGTH_SHORT).show();
  72.                     Log.i(TAG,"Keystore ks error");
  73.                     //System.exit(-1);
  74.                 } catch (NoSuchAlgorithmException e) {
  75.                     Toast.makeText(v.getContext(), "No such algorithm for ks.load", Toast.LENGTH_SHORT).show();
  76.                     Log.i(TAG,"No such algorithm for ks.load");
  77.                     e.printStackTrace();
  78.                     //System.exit(-1);
  79.                 } catch (CertificateException e) {
  80.                     Toast.makeText(v.getContext(), "certificate missing", Toast.LENGTH_SHORT).show();
  81.                     Log.i(TAG,"certificate missing");
  82.                     e.printStackTrace();
  83.                     //System.exit(-1);
  84.                 } catch (UnrecoverableKeyException e) {
  85.                     Toast.makeText(v.getContext(), "UnrecoverableKeyException", Toast.LENGTH_SHORT).show();
  86.                     Log.i(TAG,"unrecoverableKeyException");
  87.                     e.printStackTrace();
  88.                     //System.exit(-1);
  89.                 } catch (KeyManagementException e) {
  90.                     Toast.makeText(v.getContext(), "KeyManagementException", Toast.LENGTH_SHORT).show();
  91.                     Log.i(TAG,"key management exception");
  92.                     e.printStackTrace();
  93.                     //System.exit(-1);
  94.                 }
  95.             }
  96.         }
  97.     });
  98.  
  99.  
  100.  
  101. }
  102. private void printServerCertificate(SSLSocket socket) {
  103.     try {
  104.         Certificate[] serverCerts =
  105.                 socket.getSession().getPeerCertificates();
  106.         for (int i = 0; i < serverCerts.length; i++) {
  107.             Certificate myCert = serverCerts[i];
  108.             Log.i(TAG,"====Certificate:" + (i+1) + "====");
  109.             Log.i(TAG,"-Public Key-\n" + myCert.getPublicKey());
  110.             Log.i(TAG,"-Certificate Type-\n " + myCert.getType());
  111.  
  112.             System.out.println();
  113.         }
  114.     } catch (SSLPeerUnverifiedException e) {
  115.         Log.i(TAG,"Could not verify peer");
  116.         e.printStackTrace();
  117.         System.exit(-1);
  118.     }
  119. }
  120. private void printSocketInfo(SSLSocket s) {
  121.     Log.i(TAG,"Socket class: "+s.getClass());
  122.     Log.i(TAG,"   Remote address = "
  123.             +s.getInetAddress().toString());
  124.     Log.i(TAG,"   Remote port = "+s.getPort());
  125.     Log.i(TAG,"   Local socket address = "
  126.             +s.getLocalSocketAddress().toString());
  127.     Log.i(TAG,"   Local address = "
  128.             +s.getLocalAddress().toString());
  129.     Log.i(TAG,"   Local port = "+s.getLocalPort());
  130.     Log.i(TAG,"   Need client authentication = "
  131.             +s.getNeedClientAuth());
  132.     SSLSession ss = s.getSession();
  133.     Log.i(TAG,"   Cipher suite = "+ss.getCipherSuite());
  134.     Log.i(TAG,"   Protocol = "+ss.getProtocol());
  135. }
  136.  
  137. public void chat(String temp){
  138.     String message = temp;
  139.     String line = "";
  140.     // send id of the device to match with the image
  141.     try {
  142.         out.write(message+"\n");
  143.         out.flush();
  144.     } catch (IOException e2) {
  145.         Log.i(TAG,"Read failed");
  146.         System.exit(1);
  147.     }
  148.     // receive a ready command from the server
  149.     try {
  150.         line = in.readLine();
  151.         Answer.setText("SERVER SAID: "+line);
  152.         //Log.i(TAG,line);
  153.     } catch (IOException e1) {
  154.         Log.i(TAG,"Read failed");
  155.         System.exit(1);
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement