RSA Chat Messenger(Number Format Exception in Java) -
im doing rsa chat messenger(single server,multiple clients).i have included send,decrypt,exit button.when client sends message 1 client other client.the client recieves message gets in encrypted form , clicks decrypt button original message.but when im clicking decrypt button im getting awt-eventqueue-0" java.lang.numberformatexception , getting wierd characters in decryption section.
here code:
private string bytestostring(byte[] encrypted) { // todo auto-generated method stub //return null; string test = ""; (byte b : encrypted) { test += byte.tostring(b); } return test; } public static void main(string ... args) { // take username user string name = joptionpane.showinputdialog(null,"enter name :", "username", joptionpane.plain_message); string servername = "localhost"; try { new chatclient( name ,servername); } catch(exception ex) { out.println( "error --> " + ex.getmessage()); } } // end of main // inner class messages thread class messagesthread extends thread { public void run() { string line; try { while(true) { line = br.readline(); tamessages.append(line + "\n"); } // end of while } catch(exception ex) {} } } public class rsa { private biginteger p; private biginteger q; private biginteger n; private biginteger phi; private biginteger e; private biginteger d; private int bitlength = 1024; private int blocksize = 256; //blocksize in byte private random r; public rsa() { r = new random(); p = biginteger.probableprime(bitlength, r); q = biginteger.probableprime(bitlength, r); n = p.multiply(q); phi = p.subtract(biginteger.one).multiply(q.subtract(biginteger.one)); e = biginteger.probableprime(bitlength/2, r); while (phi.gcd(e).compareto(biginteger.one) > 0 && e.compareto(phi) < 0 ) { e.add(biginteger.one); } d = e.modinverse(phi); } public rsa(biginteger e, biginteger d, biginteger n) { this.e = e; this.d = d; this.n = n; } private string bytestostring(byte[] encrypted) { string test = ""; (byte b : encrypted) { test += byte.tostring(b); } return test; } //encrypt message public byte[] encrypt(byte[] message) { return (new biginteger(message)).modpow(e, n).tobytearray(); } // decrypt message public byte[] decrypt(byte[] message) { return (new biginteger(message)).modpow(d, n).tobytearray(); } } class eavesdropper implements actionlistener { jtextarea mytextarea; public eavesdropper(jtextarea ta) { mytextarea = ta; } public void actionperformed(actionevent e) { rsa rsa = new rsa(); // string teststring = tfinput.gettext(); // byte[] encrypted = rsa.encrypt(teststring.getbytes()); string text1 = tamessages.gettext(); string[] parts =text1.split("-"); string part1=parts[1]; part1 =part1.replaceall("\n", ""); byte[] b = part1.getbytes(); byte[] decrypted = rsa.decrypt(b); //rsaencryption rsa=new rsaencryption(1024); // biginteger plaintext = new biginteger(part1.getbytes()); // biginteger ciphertext = rsa.encrypt(plaintext); //string plaintext3 = rsa.decrypt(part1); //string text2 = new string(plaintext3.tobytearray()); // mytextarea.append(plaintext3); mytextarea.append("decrypted string:" + new string(decrypted)); } } } // end of client
according docs
throws: numberformatexception - val 0 bytes long.
http://docs.oracle.com/javase/6/docs/api/java/math/biginteger.html#biginteger(byte[])
Comments
Post a Comment