java - AES decryption after sending message to IP address -
so making app sends secure messages specified ip address. using aes encrypt message , part works great. able encrypt message , decrypt before send message. however, when try decrypt message has been recieved server, can not decrypt it. gets displayed in it's encrypted form.
i error "java.lang.numberformatexception: invalid int: "ne"" , think may have character encoding or something? string altered in way when sent on network?
here snippets may related issue.
public static string encrypt(string seed, string cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext.getbytes()); return tohex(result); } public static string decrypt(string seed, string encrypted) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] enc = tobyte(encrypted); byte[] result = decrypt(rawkey, enc); return new string(result); } public static byte[] tobyte(string hexstring) { int len = hexstring.length() / 2; byte[] result = new byte[len]; (int = 0; < len; i++) result[i] = integer.valueof(hexstring.substring(2 * i, 2 * + 2), 16).bytevalue(); return result; } public static string tohex(byte[] buf) { if (buf == null) return ""; stringbuffer result = new stringbuffer(2 * buf.length); (int = 0; < buf.length; i++) { appendhex(result, buf[i]); } return result.tostring(); }
this decryption message displayed.
while (!goout) { if (datainputstream.available() > 0) { incoming = datainputstream.readutf(); if(encrypt == true) { try{ msglog += aeshelper.decrypt(seed,incoming); } catch (exception e) { e.printstacktrace(); } } else msglog += incoming; mainactivity.this.runonuithread(new runnable() { @override public void run() { chatmsg.settext(msglog); } });
this encrypting message:
onclicklistener buttonencryptonclicklistener = new onclicklistener() { public void onclick(view v) { if (chatclientthread == null) { return; } if (edittextsay.gettext().tostring().equals("")) { return; } if(!edittextsay.gettext().tostring().equals("")){ string message = edittextsay.gettext().tostring(); encrypt = true; int seclvl = integer.parseint(edittextsecurity.gettext().tostring()); string encryptedmsg; try { encryptedmsg = aeshelper.encrypt(seed, message); textencryptedmsg.settext(encryptedmsg); textencryptedmsg.setvisibility(view.visible); } catch (exception e) { e.printstacktrace(); } } } };
this sending message:
onclicklistener buttonsendonclicklistener = new onclicklistener() { @override public void onclick(view v) { if (edittextsay.gettext().tostring().equals("")) { return; } if(chatclientthread==null){ return; } if (encrypt == true){ chatclientthread.sendmsg(textencryptedmsg.gettext().tostring() + "\n"); } else { chatclientthread.sendmsg(edittextsay.gettext().tostring() + "\n"); } edittextsay.settext(""); textencryptedmsg.settext(""); textdecryptedmsg.settext(""); encrypt = false; incomingmsg.setvisibility(view.visible); } };
i'll assume you're sending ciphertext in hex encoded form. datainputstream#readutf()
reads single unicode character stream. since you're sending hex characters mean single ciphertext byte can constructed 2 of such unicode characters.
the problem aes operates on blocks. trying decrypt every single "half"-byte separately won't work.
you need rewrite decryption method either
- use streaming decryption or
- read whole ciphertext , decrypt in 1 go.
if want try streaming decryption, have 2 options:
- update internal buffer of cipher object using offset-version of
cipher#update()
or - use
cipherinputstream
.
here (pseudo-code) example of reading whole thing before trying decrypt it:
stringbuilder incoming = new stringbuilder(); while (!goout && ) { incoming.append(datainputstream.readutf()); } if(encrypt == true) { try{ msglog += aeshelper.decrypt(seed, incoming.tostring()); } catch (exception e) { e.printstacktrace(); } } else msglog += incoming;
Comments
Post a Comment