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

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

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

  1. import java.io.*;
  2. import java.net.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.security.*;
  6. import java.security.KeyStore.ProtectionParameter;
  7. import java.security.KeyStore.SecretKeyEntry;
  8. import javax.crypto.*;
  9. import javax.crypto.spec.SecretKeySpec;
  10. public class CipherServer
  11. {
  12. public static void main(String[] args) throws Exception
  13. {
  14. int port = 7999;
  15. ServerSocket server = new ServerSocket(port);
  16. Socket s = server.accept();
  17. ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));
  18. // YOU NEED TO DO THESE STEPS:
  19. // -Read the key from the file generated by the client.
  20. SecretKey desKey = (SecretKey)in.readObject();
  21. // YOU NEED TO DO THESE STEPS:
  22. ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
  23. in = new ObjectInputStream(s.getInputStream());
  24. byte[] cipherText;
  25. cipherText= (byte[]) in.readObject();
  26. // -Use the key to decrypt the incoming message from socket s.
  27. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  28. cipher.init(Cipher.DECRYPT_MODE, desKey);
  29. CipherInputStream cipherIn = new CipherInputStream(s.getInputStream(), cipher);
  30. System.out.println("Algorithm used to generate key : "+desKey.getAlgorithm());
  31. byte[] plaintext = cipher.doFinal(cipherText);
  32. // -Print out the decrypt String to see if it matches the orignal message.
  33. System.out.println(plaintext.toString());
  34. }
  35. }

客户机代码

  1. import java.io.*;
  2. import java.net.*;
  3. import javax.crypto.*;
  4. public class CipherClient
  5. {
  6. public static void main(String[] args) throws Exception
  7. {
  8. // YOU NEED TO DO THESE STEPS:
  9. // -Generate a DES key.
  10. KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
  11. SecretKey desKey = keygenerator.generateKey();
  12. // -Store it in a file.
  13. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("KeyFile.xx"));
  14. out.writeObject(desKey);
  15. String message = "The quick brown fox jumps over the lazy dog.";
  16. int port = 7999;
  17. Socket s = new Socket("127.0.0.1", port);
  18. out = new ObjectOutputStream(s.getOutputStream());
  19. //convert the massage to bits
  20. byte[] messa= message.getBytes();
  21. // -Use the key to encrypt the message above and send it over socket s to the server.
  22. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  23. cipher.init(Cipher.ENCRYPT_MODE, desKey);
  24. byte[] cipherText = cipher.doFinal(messa);
  25. System.out.println(new String(cipherText));
  26. // YOU NEED TO DO THESE STEPS:
  27. // ObjectOutputStream.reset(cipherText);
  28. out.write(cipherText);
  29. }
  30. }

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

  1. Exception in thread "main" java.net.SocketException: Connection reset
  2. at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
  3. at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
  4. at java.base/java.net.SocketInputStream.read(SocketInputStream.java:200)
  5. at java.base/java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2802)
  6. at java.base/java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:3129)
  7. at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3139)
  8. at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1619)
  9. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:482)
  10. at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:440)
  11. at DES.CipherServer.main(CipherServer.java:77)
nfg76nw0

nfg76nw01#

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

相关问题