1

I am wanting to create a functional Java chat application. So I have a small application which allows users to connect via server classes and talk with each other via client classes and I have started to add Encryption. I am having trouble decrypting output from other clients in my Java chat application.

can someone help me please?

snippet of my code is included below:

THE CLIENTGUI.JAVA CLASS (encrypt is a button which is clicked)

if(o == encrypt) { String change = null; try{ change = tf.getText(); change = FileEncryption.encryptString(change); tf.setText("" + change); return; } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally{ } 

THE FILEENCRYPTION.JAVA

public class FileEncryption { //Initial Vector public static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //EncryptAndDecrypt String -> Input : PlainText + Return : CipherText+DecipherText public static String encryptString(String src) throws Exception { String dst=""; //Not Input! if(src == null || src.length()==0) return ""; //Encryption Setting byte[] k="Multimediaproces".getBytes(); SecretKeySpec Key = new SecretKeySpec(k,"AES"); IvParameterSpec ivspec = new IvParameterSpec(iv); Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE,Key,ivspec); ByteArrayOutputStream baos = new ByteArrayOutputStream(); CipherOutputStream cout = new CipherOutputStream(baos,encryptCipher); cout.write(src.getBytes()); cout.flush(); //ByteOutputStream -> Write Encryption Text cout.close(); // in encrypt method dst = DatatypeConverter.printHexBinary(baos.toByteArray()); return dst; } //String src -> EncryptedData public static String decryptString(String src) throws Exception { //src value is Encrypted Value! //So, src value -> Not Byte! String dst=""; byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);; //Not Input! if(src == null || src.length()==0) return ""; //Decryption Setting IvParameterSpec ivspec = new IvParameterSpec(iv); byte[] k="Multimediaproces".getBytes(); SecretKeySpec Key = new SecretKeySpec(k,"AES"); Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); decryptCipher.init(Cipher.DECRYPT_MODE,Key,ivspec); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(encryptedBytes); CipherInputStream cin = new CipherInputStream(bais,decryptCipher); byte[] buf = new byte[1024]; int read; while((read=cin.read(buf))>=0) //reading encrypted data! { baos.write(buf,0,read); //writing decrypted data! } // closing streams cin.close(); dst = new String(baos.toByteArray()); return dst; } } 

the problem is that when i try to decrypt the code entering the following code: if(o == decrypt) {

 try{ msg = tf.getText(); msg = FileEncryption.decryptString(msg); fop. } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); }finally{ } 

Currently, it ALLOWS me to encrypt what I type into text field.

It does not allow me to decrypt the output of what the users have said in the chat. The current code I have included for the decrypt does not function.

Can anyone help me? or have any suggestions that I could make to my program to help it decrypt?

Thanks

EDIT:

currently this is what my application looks like. The window at the bottom is the Server class where it can show who is logged in etc. the top left shows the client chat for me 'Harry'. in the text box shows when i have clicked the encrypt button. however, clicking the decrypt button does not work

7
  • 1
    fop. isn't a valid Java statement. What's your actual code? Commented May 28, 2015 at 1:22
  • Testing your encryption and decryption methods seems to work just fine for me...I'm wondering if there is some kind reliance on the current machine? Commented May 28, 2015 at 1:32
  • ahh apologies, fop. isn't a part of that. The decryption bit is a bit I wrote which didn't seem to work for me. Commented May 28, 2015 at 1:42
  • @MadProgrammer did it work for you? it simply doesnt seem to work for me.... did you manage to decrypt it? Commented May 28, 2015 at 1:43
  • 1
    I passed a String to the encrypt method then passed the result to the decrypt method and got the correct result. I dumped the "encrypted" value to the stdout and could see that it was indeed encrypted Commented May 28, 2015 at 1:46

2 Answers 2

1

Your best bet would probably be to simply use SSL sockets for your network communications, rather than writing the encryption code yourself. While your question isn't exactly a duplicate of this one, you'd likely be well served by the answers here:

Secret Key SSL Socket connections in Java

Sign up to request clarification or add additional context in comments.

4 Comments

That's assuming the OP's purpose is to make something for use, rather than to learn some basic cryptography.
TLS/SSL also has some requirements (certificates...) that won't make sense if you don't know how the cryptography works.
Indeed. If he's trying to learn something about cryptography, learning about TLS/SSL would be an excellent place to start.
Hi, thanks for the reply. however I thik SSL sockets is a step too ahead for me as for now i would just like to focus on the use of AES encryption.... any other suggestions?
1

I suspect that the problem is not passing the encrypted status between the 2 clients.

If the "encrypt" object is a button then it is a button on only one side of the client-client connection. You will need to pass the encrypted state to the other client, so that it knows to decrypt the message.

A short cut to confirming this would be to automatically show the plaintext and decrypted message on the receiving end. One of them will always be gibberish but it should change depending on the use of the encrypt button.

Good luck :)

2 Comments

Hi gregory, as I am am quite an amateur at programming I do not really understand what you mean by this. from what i currently understand, you have said that I would need to send an encrypted message from one side and receive the encrypted message on the receiving side. Problem is, on the receiving side, the decryption still does not work. the button is just not functioning. I have made sure that it is available to be used by enabling it to be true further above the code...... Basically, i would like to decrypt it within the text field if possible, or any way....
Given that others seem to have confirmed that you encrypt/decrypt methods work, my suggestion is that the "o==decrypt" is the problem. I recommend removing the test and outputting both the plain text and the decrypted message for all messages to ensure that the methods are being called and work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.