Harry Smith
Harry Smith

Reputation: 21

how to decrypt in java (snippet of my code included)

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

Upvotes: 1

Views: 724

Answers (2)

Gregory Graham
Gregory Graham

Reputation: 104

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 :)

Upvotes: 1

Warren Dew
Warren Dew

Reputation: 8938

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

Upvotes: 1

Related Questions