聊天应用程序-加密




我已经创建了一个聊天应用程序,为了完成它,我必须实现一些加密算法来保护服务器和客户端之间的消息。

我的实现是:
1.客户端创建kaypair(公钥和私钥)并将公钥发送到服务器
2.服务器获取公钥并创建用公钥加密的对称密钥
3.服务器将加密后的密钥发送给客户端
4.客户端使用私钥解锁对称密钥
5.客户端和服务器使用对称密钥进行通信

这部分代码是服务器获取公钥并发送对称密钥加密的的地方

 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
            }

密钥加密方法:

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));
}

如何从服务器获得加密对称密钥:

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

服务器消息类:(客户端消息类具有"对象内容"而不是字符串)

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+"'}";
    }
}

我将密钥发送到服务器的客户端GUI:

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           
    }
}   

错误I登录服务器:

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

我已经实现了步骤1-3,但我得到了这个例外。。。如果有人知道如何处理这个问题,请帮助我。

(如果需要,我将提供任何额外的代码。)

谢谢。

msg.content是String的实例,您尝试将其分配给sun.security.rsa.RSAPublicKeyImpl此处:

pubKey = msg.content;

请注意,您的实现看起来容易受到中间人攻击。如果我们打电话给您的客户端和服务器Alice和Bob。我是Mallory——一个恶意窃听者。

  1. Alice创建公私密钥对并将公钥发送给Bob
  2. Mallory截获了这个消息,保留了Alice的公钥以备以后使用,并将自己的公钥发送给Bob
  3. Bob收到Mallory的公钥,认为它属于Alice,生成一个会话密钥,对其加密并发送回Mallory
  4. Mallory解密会话密钥,使用Alice的公钥重新加密并将其返回给她
  5. Alice使用她的私钥进行解密,并愉快地向Bob发送加密消息,但没有意识到Mallory已经截获了会话密钥
  6. 马洛里现在收听他们的对话,并将爱丽丝妈妈的巧克力蛋糕配方卖给出价最高的人。爱丽丝责怪鲍勃,鲍勃责怪爱丽丝等等。

您需要在协议中引入签名,以帮助确保协议的真实性:Alice和Bob需要确保通信是原始的,没有被篡改。我在打电话,但稍后会看看是否能为你找到一个合适的链接。

最新更新