public void processMessage(Chat chat, Message message) { if ((message.getBody() != null) && (!message.getType().equals(Message.Type.error))) { if (!cipher) { Log.i(TAG, "Recibido mensaje plano: " + message.getBody()); listMessages.add(message); refreshAdapter(); myListView.smoothScrollToPosition(adapter.getCount() - 1); } else { try { PrivateKey pk = RSA.getPrivateKeyDecryted(KeyStore.getInstance().getPk(), passPhrase); String decodedMessage = RSA.decipher(message.getBody(), pk); Log.i(TAG, "Recibido mensaje cifrado: " + decodedMessage); Message m = new Message(); m.setFrom(message.getFrom()); m.setTo(message.getTo()); m.setBody(decodedMessage); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); m.setSubject(sdf.format(new Date())); listMessages.add(m); refreshAdapter(); myListView.smoothScrollToPosition(adapter.getCount() - 1); } catch (Exception e) { Log.d(TAG, "PETO AL DESCIFRAR"); e.printStackTrace(); } } } }
public void send(View view) { Message message = new Message(destJid); EditText editText = (EditText) findViewById(R.id.textInput); String plainText = editText.getText().toString(); editText.setText(""); message.setFrom(myJid); message.setTo(destJid); Message m = new Message(); m.setFrom(myJid); m.setBody(plainText); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); m.setSubject(sdf.format(new Date())); listMessages.add(m); refreshAdapter(); myListView.smoothScrollToPosition(adapter.getCount() - 1); if (!cipher) { try { message.setBody(plainText); chatMan.getChat().sendMessage(message); Log.d(TAG, "Enviando: " + message.getBody()); } catch (XMPPException e) { Log.d(TAG, "ERROR al enviar mensaje"); } } else { try { String encodedMessage = RSA.cipher(plainText, cert.getPublicKey()); message.setBody(encodedMessage); chatMan.getChat().sendMessage(message); Log.d(TAG, "Enviando cifrado: " + message.getBody() + " " + plainText); } catch (Exception e) { Log.d(TAG, "PETO ENVIANDO CIFRADOOOO"); e.printStackTrace(); } } }