Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中使用AES加密的客户端服务器聊天_Java_Encryption_Cryptography - Fatal编程技术网

java中使用AES加密的客户端服务器聊天

java中使用AES加密的客户端服务器聊天,java,encryption,cryptography,Java,Encryption,Cryptography,我正在尝试用Java制作一个带有加密消息的聊天程序(你可以发送加密消息,当你收到消息时你必须发送加密消息) 解密它)。我找到了用于解密和加密的代码: import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import sun.misc.*; import java.io.BufferedReader; import java.io.FileReader; pub

我正在尝试用Java制作一个带有加密消息的聊天程序(你可以发送加密消息,当你收到消息时你必须发送加密消息) 解密它)。我找到了用于解密和加密的代码:

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class test {
private static String algorithm = "AES/CBC/PKCS5Padding";
private static byte[] keyValue=new byte[]      {'0','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};// your key

// Performs Encryption
public static String encrypt(String plainText) throws Exception
{
        Key key = generateKey();
        Cipher chiper = Cipher.getInstance(algorithm);
        chiper.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = chiper.doFinal(plainText.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
}

// Performs decryption
public static String decrypt(String encryptedText) throws Exception
{
        // generate key
        Key key = generateKey();
        Cipher chiper = Cipher.getInstance(algorithm);
        chiper.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
        byte[] decValue = chiper.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
}

//generateKey() is used to generate a secret key for AES algorithm
private static Key generateKey() throws Exception
{
        Key key = new SecretKeySpec(keyValue, algorithm);
        return key;
}
因此,加密功能运行良好:

public void send(String text) {
    try {
        test test = new test();
        String textTS = chat.AES.test.encrypt(text);
        OUT.println(ClientGUI.UserName + ": " + textTS);
        OUT.flush();
        Client_GUI.TF_Message.setText("");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
但我无法真正接收消息,我认为问题在于密钥生成,我从未保存用于加密消息的密钥,并尝试使用新密钥对其解密,但如果问题在于如何发送密钥? 接收功能的代码:

public void receive() {
    try {
        if (INPUT.hasNext()) {
            String MESSAGE = INPUT.nextLine();
            String dMessage = chat.AES.test.decrypt(MESSAGE);
            System.out.println(dMessage);
            if (MESSAGE.contains("#?!")) {
                String TEMP1 = MESSAGE.substring(3);
                TEMP1 = TEMP1.replace("[", "");
                TEMP1 = TEMP1.replace("]", "");

                String[] CurrentUsers = TEMP1.split(", ");

                Client_GUI.JL_ONLINE.setListData(CurrentUsers);
            } else {
                Client_GUI.TA_CONVERSATION.append(MESSAGE + "\n");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

将密钥与密文一起发送不会给您带来任何安全性,只会带来一些混淆。您可以让用户键入密码并从中派生密钥,或者您需要通过非对称加密(如RSA)分发密钥。仅使用CBC而不进行密钥管理或完整性/真实性对于传输协议来说是完全无用的。你需要至少对你实际上在做什么有一点概念,而不是仅仅抓住机会就跑。