0
votes


I have created a chat application and to finish it I have to implement some Cryptography algorithm to secure the messages between server - client.

My implementation is:
1.Client creates kaypair (public and private key) and sends public key to server.
2.Server gets public key and creates symmetric key encrypted with the public key.
3.Server sends the encrypted key to Client.
4.Client unlocks symmetric key with private key.
5.Client and Server communicate with Symmetric key.

This part of the code is where the server gets the public key and sends the symmetric key encrypted

 else if(msg.type.equals("pubKey")){
                    pubKey = msg.content;                     //get public key
                    String key = Arrays.toString(crypt.geteKey());
                    clients[findClient(ID)].send(new Message("symmKey", "SERVER", key, msg.sender));//!  //send symmetric key encrypted with public key
            }

Key encryption method:

public void keyEncryption() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    eKey = cipher.doFinal(key.getBlowfishKeyBytes());           //symmetric key encrypted with public key
    //System.out.println("2. cipherText= " + bytesToHex(symmKey));
}

How I get encrypted symmetric key from server:

   else if(msg.type.equals("symmKey")){
                    symmKey = (String) msg.content;     //get encrypted symmetric key (must unlock with private key)
                }

The Server Message class: (Client Message class has "object content" instead of String)

package com.socket;

import java.io.Serializable;

public class Message implements Serializable{

    private static final long serialVersionUID = 1L;
    public String type, sender,content, recipient;

    public Message(String type, String sender, String content, String recipient){
        this.type = type; this.sender = sender; this.content = content; this.recipient = recipient;
    }

    @Override
    public String toString(){
        return "{type='"+type+"', sender='"+sender+"', content='"+content+"', recipient='"+recipient+"'}";
    }
}

Client GUI where I send the key to the Server:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //KeyPair
    try {
        keyPair = new Keypair();
    } catch (NoSuchAlgorithmException ex) {
        jTextArea1.append("Security Error! You are not safe!");
    }
    Object pubKey = keyPair.getKeyPair().getPublic();
    username = jTextField3.getText();
    password = jPasswordField1.getText();

    if (!username.isEmpty() && !password.isEmpty()) {
        client.send(new Message("login", username, password, "SERVER"));
        client.send(new Message("pubKey",username, pubKey, "SERVER"));      //send Public key to Server           
    }
}   

Error I Get on Server:

Database exception : userExists()
53846 ERROR reading: cannot assign instance of sun.security.rsa.RSAPublicKeyImpl to field com.socket.Message.content of type java.lang.String in instance of com.socket.Message

I have implemented steps 1-3 but I get this exception... If anyone has any idea how to deal with this issue, please help me.

(I will provide any additional code if required.)

Thank you.

2
Maybe a WhatsApp developer can give some insight on that topic - oh wait! They don't do cryptography :/ - Jack
Bogdan M. dude its about a Secure Chat Application... its all about security ... Why u messin up my post? - user3514877
I added '<br>' thats i,t you accept it, than accuse me of messing it up? - Bogdan M.
Throw it all away and use SSL. It's a solved problem, and it's not one you're ping to solve yourself. It's too hard. - user207421
@question - cannot assign RSAPublicKeyImpl to field .Message.content of type String, what i understand you need to convert something there: stackoverflow.com/questions/16653754/… - Bogdan M.

2 Answers

0
votes

msg.content is instance of String and you trying assign it to sun.security.rsa.RSAPublicKeyImpl here:

pubKey = msg.content;
0
votes

Just on a note, your implementation looks susceptible to a man-in-the-middle attack. If we call your client and server Alice and Bob. I'm Mallory - a malicious eavesdropper.

  1. Alice creates public-private key pair and sends public key to Bob.
  2. Mallory intercepts this, keeps Alice's public key for later and sends his own public key on to Bob.
  3. Bob receives Mallory's public key, thinking it belongs to Alice, generates a session key, encrypts it and sends back to Mallory.
  4. Mallory decrypts the session key, reencrypts using Alice's public key and returns it to her.
  5. Alice decrypts using her private key and happily goes about sending encrypted messages to Bob, not realising that Mallory has intercepted the session key.
  6. Mallory now listens in on their conversation and sells Alice's mums recipe for chocolate cake to the highest bidder. Alice blames Bob, Bob blames Alice etc.

You will need to introduce signing to your protocol to help to ensure authenticity of your protocol: Alice and Bob need to be certain that that the communications are original and have not been tampered with. I'm on my phone but will see if I can find a decent link later for you.