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

Harry Smith
  • 21
  • 1
  • 3
  • 1
    `fop.` isn't a valid Java statement. What's your actual code? – user253751 May 28 '15 at 01: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? – MadProgrammer May 28 '15 at 01: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. – Harry Smith May 28 '15 at 01:42
  • @MadProgrammer did it work for you? it simply doesnt seem to work for me.... did you manage to decrypt it? – Harry Smith May 28 '15 at 01: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 – MadProgrammer May 28 '15 at 01:46
  • @MadProgrammer oh right, well in my code i do not use any stdout as i am using an actual chat application where i can send it back and forth through a client. ahh its quite difficult to explain you see but i would like to decrypt what is being input into a text field. – Harry Smith May 28 '15 at 01:53
  • And why would that make any difference? It's possible that the encrypted text has been mangled in transmission? But since we don't have anyway to test that, who knows. Compare the value you are sending with the value you are receiving (the encrypted `String`) to make sure the transmission is not the problem - My previous point is, the encrypt/decrypt methods seem to work okay – MadProgrammer May 28 '15 at 01:56

2 Answers2

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

Community
  • 1
  • 1
Warren Dew
  • 8,790
  • 3
  • 30
  • 44
  • That's assuming the OP's purpose is to make something for use, rather than to learn some basic cryptography. – user253751 May 28 '15 at 01:18
  • 1
    TLS/SSL also has some requirements (certificates...) that won't make sense if you don't know how the cryptography works. – user253751 May 28 '15 at 01:19
  • Indeed. If he's trying to learn something about cryptography, learning about TLS/SSL would be an excellent place to start. – Warren Dew May 28 '15 at 01:20
  • 1
    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? – Harry Smith May 28 '15 at 01:43
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 :)

  • 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.... – Harry Smith May 28 '15 at 14:51
  • 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. – Gregory Graham May 29 '15 at 04:19