繁体   English   中英

java.net.SocketException:连接重置“”“

[英]java.net.SocketException: Connection reset “”"

我厌倦了通过套接字发送字节加密数据,但是,它不起作用,要求服务器从文件中读取密钥并从客户端接收加密消息读取文件的变量和 sockets。

服务器代码

    import java.io.*;
    import java.net.*;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.security.*;
    import java.security.KeyStore.ProtectionParameter;
    import java.security.KeyStore.SecretKeyEntry;

   import javax.crypto.*;
   import javax.crypto.spec.SecretKeySpec;

   public class CipherServer
  {


public static void main(String[] args) throws Exception 
{
    int port = 7999;
    ServerSocket server = new ServerSocket(port);
    Socket s = server.accept();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
    
    // YOU NEED TO DO THESE STEPS:
    // -Read the key from the file generated by the client.
    SecretKey desKey = (SecretKey)in.readObject();

    // YOU NEED TO DO THESE STEPS:
    ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
    
    in = new ObjectInputStream(s.getInputStream());
    byte[] cipherText;
            
    cipherText= (byte[]) in.readObject();


    // -Use the key to decrypt the incoming message from socket s.  
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, desKey);

     CipherInputStream cipherIn = new CipherInputStream(s.getInputStream(), cipher);

    System.out.println("Algorithm used to generate key : "+desKey.getAlgorithm());   

    byte[] plaintext = cipher.doFinal(cipherText);


    // -Print out the decrypt String to see if it matches the orignal message.
    System.out.println(plaintext.toString());       



}
 }

客户代码

   import java.io.*;
   import java.net.*;

   import javax.crypto.*;

   public class CipherClient
  {


public static void main(String[] args) throws Exception 
{

    // YOU NEED TO DO THESE STEPS:
    // -Generate a DES key.
    KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygenerator.generateKey();

    // -Store it in a file. 
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));  
    out.writeObject(desKey);   


    String message = "The quick brown fox jumps over the lazy dog.";
    int port = 7999;
    Socket s = new Socket("127.0.0.1", port);
    
    out = new ObjectOutputStream(s.getOutputStream());

    //convert the massage to bits
    byte[] messa= message.getBytes();

    // -Use the key to encrypt the message above and send it over socket s to the server.   
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, desKey);

    byte[] cipherText = cipher.doFinal(messa);  
    System.out.println(new String(cipherText));

    // YOU NEED TO DO THESE STEPS:
   //  ObjectOutputStream.reset(cipherText);
    out.write(cipherText);

  }
   }

我在服务器端收到一条错误消息

     Exception in thread "main" java.net.SocketException: Connection reset
     at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
     at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
     at java.base/java.net.SocketInputStream.read(SocketInputStream.java:200)
     at java.base/java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2802)
     at java.base/java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:3129)
     at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3139)
     at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1619)
     at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:482)
     at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:440)
     at DES.CipherServer.main(CipherServer.java:77)

在写入端,您正在调用 OutputStream.write(byte[]),在读取端,您正在调用 readObject(然后尝试将其转换为 byte[])。 如果您要使用 ObjectStreams(我强烈反对),您需要在双方都与 writeObject 和 readObject 保持一致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM