Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 将CipherInputStream与套接字一起使用_Java_Sockets_Encryption - Fatal编程技术网

Java 将CipherInputStream与套接字一起使用

Java 将CipherInputStream与套接字一起使用,java,sockets,encryption,Java,Sockets,Encryption,在网络连接上使用CipherInputStream时遇到问题。我读过一些关于这方面的问题和文章,并根据它们的内容进行了实验,但没有结果。我为自己的问题创建了一个简单的例子,希望有人能为我指明正确的方向 我的示例包含两个可执行类: EchoSocketTest–这是一个简单的服务器应用程序,可以监听 无论收到什么,端口都会回显给发送方 EchoClientTest–这是一个简单的客户端。它进行3次测试 测试1。创建到echo服务器的连接,发送一段数据并显示返回的内容 测试2。创建到echo服务

在网络连接上使用CipherInputStream时遇到问题。我读过一些关于这方面的问题和文章,并根据它们的内容进行了实验,但没有结果。我为自己的问题创建了一个简单的例子,希望有人能为我指明正确的方向

我的示例包含两个可执行类:

  • EchoSocketTest–这是一个简单的服务器应用程序,可以监听 无论收到什么,端口都会回显给发送方

  • EchoClientTest–这是一个简单的客户端。它进行3次测试

测试1。创建到echo服务器的连接,发送一段数据并显示返回的内容

测试2。创建到echo服务器的连接,但使用CipherOutputStream对其中的数据进行加密 发送。它发送一段数据并显示返回的内容

测试3。与测试2相同,但它尝试使用CipherInputStream对数据进行解密

在我的示例中,测试1和测试2完全成型,并给出正确的输出。但是,测试3失败,并显示消息“Error NOTH read from socket”(错误:未从套接字读取任何内容)。 我已经通过创建另一个使用FileInputStream的测试验证了加密和解密部分的工作,该测试按预期工作。我有一种感觉,这个问题与填充有关,但我已经尝试了网上建议的各种选项

代码和示例输出如下所示:

EchoSocketTest:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoSocketTest {

    public static class Connection extends Thread {
        Socket m_Sock = null;
        PrintWriter m_out = null;
        BufferedReader m_in = null;
        public Connection(Socket p_Sock) throws Exception {
            m_Sock = p_Sock;
            System.out.println("Connection accepted");
            m_out = new PrintWriter(m_Sock.getOutputStream(), true);     
            InputStreamReader isr;
            m_in = new BufferedReader(new InputStreamReader(m_Sock.getInputStream(),"UTF-8"));          
        }
        @Override
        public void run() {
            try {
                char buf[];
                while (true) {
                    String inputLine = "";
                    while (m_in.ready()) {
                        int i = m_in.read();
                        m_out.write(i);
                        inputLine += (char)i;
                    }
                    if (inputLine.length()>0) {
                        System.out.println(inputLine);
                        m_out.flush();
                        inputLine = "";
                    };
                }
                //System.out.println("Connection closed");
            } catch (Exception e) {
                System.out.println("Connection Exception:");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("Echo Socket accepting connections on port 1423");
            ServerSocket sSock = new ServerSocket(1423);
            while (true) {
                Connection c = new Connection(sSock.accept());
                c.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EchoClientTest {

    public static void main(String[] args) {
        try {
            byte[] keyB = {
                    (byte)0x2C, (byte)0xCF, (byte)0xBC, (byte)0x81, (byte)0x99, (byte)0xB7, (byte)0x37, (byte)0x84, 
                    (byte)0xD1, (byte)0x09, (byte)0x44, (byte)0x4F, (byte)0xB6, (byte)0x66, (byte)0x69, (byte)0x1F, 
                    (byte)0x17, (byte)0x61, (byte)0xAD, (byte)0xD7, (byte)0x43, (byte)0x0E, (byte)0x80, (byte)0x6C                      
            };
            byte[] ivB = {
                    (byte)0x7B, (byte)0x9B, (byte)0x5F, (byte)0x36, (byte)0xC9, (byte)0x26, (byte)0xE9, (byte)0x5E                      
            };


            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up - Unencrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                DataOutputStream doS = new DataOutputStream(oS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Unencrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, Algo);
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT and decrypting RECIEVED on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, "DESede");
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                final Cipher cipherDecrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      
                cipherDecrypt.init(Cipher.DECRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                CipherInputStream cipherInputS =  new CipherInputStream(iS,cipherDecrypt);
                InputStreamReader iSR = new InputStreamReader(cipherInputS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket (and Decrtpying)");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }           

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Connected
Setting up - Unencrypted version
Writing Unencrypted output
Waiting 1 second
Reading data from socket
Recieved:Test Message

Closing Socket
Repeating but encrypting the data SENT on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket
Recieved:?Ip?????9?m?
Closing Socket
Repeating but encrypting the data SENT and decrypting RECIEVED on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket (and Decrtpying)
Error nothing read from socket
Closing Socket
EchoClientTest:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoSocketTest {

    public static class Connection extends Thread {
        Socket m_Sock = null;
        PrintWriter m_out = null;
        BufferedReader m_in = null;
        public Connection(Socket p_Sock) throws Exception {
            m_Sock = p_Sock;
            System.out.println("Connection accepted");
            m_out = new PrintWriter(m_Sock.getOutputStream(), true);     
            InputStreamReader isr;
            m_in = new BufferedReader(new InputStreamReader(m_Sock.getInputStream(),"UTF-8"));          
        }
        @Override
        public void run() {
            try {
                char buf[];
                while (true) {
                    String inputLine = "";
                    while (m_in.ready()) {
                        int i = m_in.read();
                        m_out.write(i);
                        inputLine += (char)i;
                    }
                    if (inputLine.length()>0) {
                        System.out.println(inputLine);
                        m_out.flush();
                        inputLine = "";
                    };
                }
                //System.out.println("Connection closed");
            } catch (Exception e) {
                System.out.println("Connection Exception:");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("Echo Socket accepting connections on port 1423");
            ServerSocket sSock = new ServerSocket(1423);
            while (true) {
                Connection c = new Connection(sSock.accept());
                c.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EchoClientTest {

    public static void main(String[] args) {
        try {
            byte[] keyB = {
                    (byte)0x2C, (byte)0xCF, (byte)0xBC, (byte)0x81, (byte)0x99, (byte)0xB7, (byte)0x37, (byte)0x84, 
                    (byte)0xD1, (byte)0x09, (byte)0x44, (byte)0x4F, (byte)0xB6, (byte)0x66, (byte)0x69, (byte)0x1F, 
                    (byte)0x17, (byte)0x61, (byte)0xAD, (byte)0xD7, (byte)0x43, (byte)0x0E, (byte)0x80, (byte)0x6C                      
            };
            byte[] ivB = {
                    (byte)0x7B, (byte)0x9B, (byte)0x5F, (byte)0x36, (byte)0xC9, (byte)0x26, (byte)0xE9, (byte)0x5E                      
            };


            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up - Unencrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                DataOutputStream doS = new DataOutputStream(oS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Unencrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, Algo);
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT and decrypting RECIEVED on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, "DESede");
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                final Cipher cipherDecrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      
                cipherDecrypt.init(Cipher.DECRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                CipherInputStream cipherInputS =  new CipherInputStream(iS,cipherDecrypt);
                InputStreamReader iSR = new InputStreamReader(cipherInputS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket (and Decrtpying)");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }           

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Connected
Setting up - Unencrypted version
Writing Unencrypted output
Waiting 1 second
Reading data from socket
Recieved:Test Message

Closing Socket
Repeating but encrypting the data SENT on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket
Recieved:?Ip?????9?m?
Closing Socket
Repeating but encrypting the data SENT and decrypting RECIEVED on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket (and Decrtpying)
Error nothing read from socket
Closing Socket
示例客户端输出:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoSocketTest {

    public static class Connection extends Thread {
        Socket m_Sock = null;
        PrintWriter m_out = null;
        BufferedReader m_in = null;
        public Connection(Socket p_Sock) throws Exception {
            m_Sock = p_Sock;
            System.out.println("Connection accepted");
            m_out = new PrintWriter(m_Sock.getOutputStream(), true);     
            InputStreamReader isr;
            m_in = new BufferedReader(new InputStreamReader(m_Sock.getInputStream(),"UTF-8"));          
        }
        @Override
        public void run() {
            try {
                char buf[];
                while (true) {
                    String inputLine = "";
                    while (m_in.ready()) {
                        int i = m_in.read();
                        m_out.write(i);
                        inputLine += (char)i;
                    }
                    if (inputLine.length()>0) {
                        System.out.println(inputLine);
                        m_out.flush();
                        inputLine = "";
                    };
                }
                //System.out.println("Connection closed");
            } catch (Exception e) {
                System.out.println("Connection Exception:");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println("Echo Socket accepting connections on port 1423");
            ServerSocket sSock = new ServerSocket(1423);
            while (true) {
                Connection c = new Connection(sSock.accept());
                c.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EchoClientTest {

    public static void main(String[] args) {
        try {
            byte[] keyB = {
                    (byte)0x2C, (byte)0xCF, (byte)0xBC, (byte)0x81, (byte)0x99, (byte)0xB7, (byte)0x37, (byte)0x84, 
                    (byte)0xD1, (byte)0x09, (byte)0x44, (byte)0x4F, (byte)0xB6, (byte)0x66, (byte)0x69, (byte)0x1F, 
                    (byte)0x17, (byte)0x61, (byte)0xAD, (byte)0xD7, (byte)0x43, (byte)0x0E, (byte)0x80, (byte)0x6C                      
            };
            byte[] ivB = {
                    (byte)0x7B, (byte)0x9B, (byte)0x5F, (byte)0x36, (byte)0xC9, (byte)0x26, (byte)0xE9, (byte)0x5E                      
            };


            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up - Unencrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                DataOutputStream doS = new DataOutputStream(oS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Unencrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, Algo);
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                InputStreamReader iSR = new InputStreamReader(iS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }
            System.out.println("Repeating but encrypting the data SENT and decrypting RECIEVED on the socket");
            {
                Socket echoSocket = new Socket("127.0.0.1",1423);
                System.out.println("Connected");

                System.out.println("Setting up Ciphers");
                String Algo = "DESede"; //DES or DESede

                final SecretKey key = new SecretKeySpec(keyB, "DESede");
                final IvParameterSpec iv = new IvParameterSpec(ivB);
                final Cipher cipherEncrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                final Cipher cipherDecrypt = Cipher.getInstance("DESede/CFB8/NoPadding");
                cipherEncrypt.init(Cipher.ENCRYPT_MODE, key, iv);      
                cipherDecrypt.init(Cipher.DECRYPT_MODE, key, iv);      

                System.out.println("Setting up - Encrypted version");
                OutputStream oS = echoSocket.getOutputStream();
                CipherOutputStream cipherOutputS = new CipherOutputStream(oS,cipherEncrypt);
                DataOutputStream doS = new DataOutputStream(cipherOutputS);
                InputStream iS = echoSocket.getInputStream();
                CipherInputStream cipherInputS =  new CipherInputStream(iS,cipherDecrypt);
                InputStreamReader iSR = new InputStreamReader(cipherInputS,"UTF-8");
                BufferedReader bR = new BufferedReader(iSR);


                System.out.println("Writing Encrypted output");
                doS.write("Test Message\n".getBytes("UTF-8"));
                doS.flush();

                System.out.println("Waiting 1 second");
                Thread.sleep(1000);

                System.out.println("Reading data from socket (and Decrtpying)");

                String inputLine = "";
                while (bR.ready()) {
                    int i = bR.read();
                    inputLine += (char)i;
                }
                if (inputLine.length()==0) {
                    System.out.println("Error nothing read from socket");
                } else {
                    System.out.println("Recieved:" + inputLine);
                    inputLine = "";
                };

                System.out.println("Closing Socket");
                echoSocket.close();
            }           

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Connected
Setting up - Unencrypted version
Writing Unencrypted output
Waiting 1 second
Reading data from socket
Recieved:Test Message

Closing Socket
Repeating but encrypting the data SENT on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket
Recieved:?Ip?????9?m?
Closing Socket
Repeating but encrypting the data SENT and decrypting RECIEVED on the socket
Connected
Setting up Ciphers
Setting up - Encrypted version
Writing Encrypted output
Waiting 1 second
Reading data from socket (and Decrtpying)
Error nothing read from socket
Closing Socket

注:我遇到TLS和证书不适用的情况。如果可能的话,我当然会使用https而不是这种方法。

摆脱
ready()
测试。我应该如何查询流以确定字节是否准备好读取?所有
ready()
都会告诉你是否可以无阻塞地读取。所以当它为false时,这仅仅意味着read将(实际上是may)阻塞。您将此解释为错误。您应该一直读到EOF。您应该在流之上创建一个消息协议。您始终可以将数据包重新连接在一起。请注意,通过使用
“AES/CTR/NoPadding”
,您可以轻松地使其更加安全。如果您还有时间,请在消息上添加DH&authentication+MAC。我的主应用程序中有一个协议(这只是一个测试),我正在尝试升级该协议以使用加密。我试图使用三重DES而不是AES,因为我知道AES已经被破坏了。(我还将使用RSA公钥和通过RSA发送的随机三重DES密钥,节点将通过另一种安全方法预先共享其公钥)