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

xpszyzbs  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(531)

我厌倦了通过套接字发送字节加密的数据,但是,它不起作用,要求服务器从文件中读取密钥并从客户端接收加密消息我面临输入/输出问题stream:i have 使用了一个读取文件和套接字的变量。
服务器代码

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)
nfg76nw0

nfg76nw01#

在写端调用outputstream.write(byte[]),在读端调用readobject(然后尝试将其转换为byte[])。如果你打算使用objectstreams(我强烈反对),你需要在两边都与writeobject和readobject保持一致

相关问题