2013-03-23 66 views
0

我試圖根據請求從服務器端向客戶端發送文件。發送的文件被加密,客戶端將其解密。加密過程工作正常,但解密時我需要有我使用序列化的DerIOBuffer objetc。我應該do..please幫助java.lang.Exception:傳輸數據時出錯如何解決此錯誤

服務器:

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

import java.math.BigInteger; 
import com.dragongate_technologies.borZoi.*; 

public class FileServer { 

    static final int LISTENING_PORT =; 


    public static void main(String[] args) { 

    File directory;  // The directory from which the gets the files that it serves. 
    ServerSocket listener; // Listens for connection requests. 
    Socket connection;  // A socket for communicating with a client. 


    /* Check that there is a command-line argument. 
     If not, print a usage message and end. */ 

    if (args.length == 0) { 
     System.out.println("Usage: java FileServer <directory>"); 
     return; 
    } 

    /* Get the directory name from the command line, and make 
     it into a file object. Check that the file exists and 
     is in fact a directory. */ 

    directory = new File(args[0]); 
    if (! directory.exists()) { 
     System.out.println("Specified directory does not exist."); 
     return; 
    } 
    if (! directory.isDirectory()) { 
     System.out.println("The specified file is not a directory."); 
     return; 
    } 

    /* Listen for connection requests from clients. For 
     each connection, create a separate Thread of type 
     ConnectionHandler to process it. The ConnectionHandler 
     class is defined below. The server runs until the 
     program is terminated, for example by a CONTROL-C. */ 

    try { 
     listener = new ServerSocket(LISTENING_PORT); 
     System.out.println("Listening on port " + LISTENING_PORT); 
     while (true) { 
      connection = listener.accept(); 
      new ConnectionHandler(directory,connection); 
     } 
    } 
    catch (Exception e) { 
     System.out.println("Server shut down unexpectedly."); 
     System.out.println("Error: " + e); 
     return; 
    } 

    } // end main() 



    static class ConnectionHandler extends Thread { 
     // An object of this class is a thread that will 
     // process the connection with one client. The 
     // thread starts itself in the constructor. 

    File directory;  // The directory from which files are served 
    Socket connection; // A connection to the client. 
    TextReader incoming; // For reading data from the client. 
    PrintWriter outgoing; // For transmitting data to the client. 


    ConnectionHandler(File dir, Socket conn) { 
      // Constructor. Record the connection and 
      // the directory and start the thread running. 
     directory = dir; 
     connection = conn; 
     start(); 
    } 


    void sendIndex() throws Exception { 
      // This is called by the run() method in response 
      // to an "index" command. Send the list of files 
      // in the directory. 
     String[] fileList = directory.list(); 
     for (int i = 0; i < fileList.length; i++) 
      outgoing.println(fileList[i]); 
     outgoing.flush(); 
     outgoing.close(); 
     if (outgoing.checkError()) 
      throw new Exception("Error while transmitting data."); 
    } 


    void ecies_ex(String fileName) throws Exception { 
      // This function encrypts the file that has been requested 
      // by the client. 

     String at1,dc1,der1; 

     OutputStream os = connection.getOutputStream(); 
     ObjectOutputStream oos = new ObjectOutputStream(os); 

     ECDomainParameters dp = ECDomainParameters.NIST_B_163(); 

     ECPrivKey skA = new ECPrivKey(dp, BigInteger.valueOf(123)); 
     ECPubKey pkA = new ECPubKey(skA); 

     ECPrivKey skB = new ECPrivKey(dp, BigInteger.valueOf(230)); 
     ECPubKey pkB = new ECPubKey(skB); 

     File file = new File(directory,fileName); 
     if ((! file.exists()) || file.isDirectory()) { 
      // (Note: Don't try to send a directory, which 
      // shouldn't be there anyway.) 
      outgoing.println("error"); 
     } 
     else { 
      outgoing.println("ok"); 

      String pt1 = new String(); 
      BufferedReader br = null; 

     try { 
      String sCurrentLine; 

      br = new BufferedReader(new FileReader(fileName)); 
      while ((sCurrentLine = br.readLine()) != null) { 
       pt1=pt1+"\n"+sCurrentLine; 
      } 

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

     ECIES crypt = new ECIES(skA, pkB, pt1.getBytes()); // encrypt the data 

     try { 
      DerIOBuffer der = new DerIOBuffer(crypt); 

      oos.writeObject(der); 
      ECIES decrypt = der.toECIES(); 
      dc1=decrypt.toString2(); //cipher text 

      //at1=decrypt.toString3(); //authentication tag 

      FileWriter fstream = new FileWriter("encrypted.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write(dc1); 
      //Close the output stream 
      out.close(); 

     } catch (Exception e) { 
      System.out.println(e.toString()); 
     } 

     TextReader fileIn = new TextReader(new FileReader("encrypted.txt")); 
     while (fileIn.peek() != '\0') { 
      // Read and send lines from the file until 
      // an end-of-file is encountered. 
      String line = fileIn.getln(); 
      outgoing.println(line); 
     } 
     } 
     outgoing.flush(); 
    // oos.close(); 
    // os.close(); 
     outgoing.close(); 
     if (outgoing.checkError()) 
     throw new Exception("Error while transmitting data."); 

} 




    public void run() { 
      // This is the method that is executed by the thread. 
      // It creates streams for communicating with the client, 
      // reads a command from the client, and carries out that 
      // command. The connection is logged to standard output. 
      // An output beginning with ERROR indicates that a network 
      // error occurred. A line beginning with OK means that 
      // there was no network error, but does not imply that the 
      // command from the client was a legal command. 
     String command = "Command not read"; 
     try { 
      incoming = new TextReader(connection.getInputStream()); 
      outgoing = new PrintWriter(connection.getOutputStream()); 
      command = incoming.getln(); 
      if (command.equals("index")) { 
       sendIndex(); 
      } 
      else if (command.startsWith("get")){ 
       String fileName = command.substring(3).trim(); 
       ecies_ex(fileName); 
       //sendFile(fileName); 
      } 
      else { 
       outgoing.println("unknown command"); 
       outgoing.flush(); 
      } 
      System.out.println("OK " + connection.getInetAddress() 
             + " " + command); 
     } 
     catch (Exception e) { 
      System.out.println("ERROR " + connection.getInetAddress() 
            + " " + command + " " + e); 
     } 
     finally { 
      try { 
       connection.close(); 
      } 
      catch (IOException e) { 
      } 
     } 
    } 

    } // end nested class ConnectionHandler 


} //end class FileServer 

客戶端:

import java.net.*; 
import java.io.*; 
import java.math.BigInteger; 
import com.dragongate_technologies.borZoi.*; 


public class FileClient { 

    static final int LISTENING_PORT =; 


    public static void main(String[] args) { 

    String computer;  // Name or IP address of server. 
    Socket connection; // A socket for communicating with that computer. 
    PrintWriter outgoing; // Stream for sending a command to the server. 
    TextReader incoming; // Stream for reading data from the connection. 
    String command;  // Command to send to the server. 
    String pt3; 


    ECDomainParameters dp = ECDomainParameters.NIST_B_163(); 
    ECPrivKey skB = new ECPrivKey(dp, BigInteger.valueOf(230)); 
    //ECPrivKey skB = new ECPrivKey (dp); 
    ECPubKey pkB = new ECPubKey(skB); 
    /* Check that the number of command-line arguments is legal. 
     If not, print a usage message and end. */ 



    if (args.length == 0 || args.length > 3) { 
     System.out.println("Usage: java FileClient <server>"); 
     System.out.println(" or java FileClient <server> <file>"); 
     System.out.println(" or java FileClient <server> <file> <local-file>"); 
     return; 
    } 

    /* Get the server name and the message to send to the server. */ 

    computer = args[0]; 

    if (args.length == 1) 
     command = "index"; 
    else 
     command = "get " + args[1]; 

    /* Make the connection and open streams for communication. 
     Send the command to the server. If something fails 
     during this process, print an error message and end. */ 

    try { 
     connection = new Socket(computer, LISTENING_PORT); 
     incoming = new TextReader(connection.getInputStream()); 
     outgoing = new PrintWriter(connection.getOutputStream()); 


     outgoing.println(command); 
     outgoing.flush(); 
    } 
    catch (Exception e) { 
     System.out.println(
      "Can't make connection to server at \"" + args[0] + "\"."); 
     System.out.println("Error: " + e); 
     return; 
    } 

    /* Read and process the server's response to the command. */ 

    try { 
     if (args.length == 1) { 
       // The command was "index". Read and display lines 
       // from the server until the end-of-stream is reached. 
      System.out.println("File list from server:"); 
      while (incoming.eof() == false) { 
       String line = incoming.getln(); 
       System.out.println(" " + line); 
      } 
     } 
     else { 
       // The command was "get <file-name>". Read the server's 
       // response message. If the message is "ok", get the file. 
      String message = incoming.getln(); 
      if (! message.equals("ok")) { 
       System.out.println("File not found on server."); 
       return; 
      } 
      PrintWriter fileOut; // For writing the received data to a file. 
      if (args.length == 3) { 
       // Use the third parameter as a file name. 
       fileOut = new PrintWriter(new FileWriter(args[2])); 
      } 
      else { 
       // Use the second parameter as a file name, 
       // but don't replace an existing file. 
       File file = new File(args[1]); 
       if (file.exists()) { 
        System.out.println("A file with that name already exists."); 
        System.out.println("To replace it, use the three-argument"); 
        System.out.println("version of the command."); 
        return; 
       } 
       fileOut = new PrintWriter(new FileWriter(args[1])); 
      } 
      while (incoming.peek() != '\0') { 
        // Copy lines from incoming to the file until 
        // the end of the incoming stream is encountered. 
       String line = incoming.getln(); 
       fileOut.println(line); 
      } 
      InputStream is = connection.getInputStream(); 
      ObjectInputStream ois = new ObjectInputStream(is); 
      DerIOBuffer der = (DerIOBuffer)ois.readObject(); 
      ECIES decrypt = der.toECIES(); 

      byte[] pt2 = decrypt.decrypt(skB); // decrypt the data 
      pt3=new String(pt2); 

      if (fileOut.checkError()) { 
       System.out.println("Some error occurred while writing the file."); 
       System.out.println("Output file might be empty or incomplete."); 
      } 
     } 
    } 
    catch (Exception e) { 
     System.out.println("Sorry, an error occurred while reading data from the server."); 
     System.out.println("Error: " + e); 
    } 

    } // end main() 


} //end class FileClient 
+0

'DerIOBuffer'應該是'Serializable',並且'DerIOBuffer'的類文件也必須存在於客戶端。否則它不會工作。 –

回答

1

如果你關心的錯誤,你不應該使用PrintWriter。爲什麼?因爲如果通過PrintWriter在輸出上發生錯誤,您無法找出它是什麼。這就是在這種情況下很難弄清真正的問題。我建議你解決這個問題,以便你能夠找到問題的真正原因。

真正的問題可以涉及到下列問題:

  • 如果你試圖寫東西可以是二進制的,則不應使用PrintWriter ...或讀者/作家。

  • 您似乎在不必要地使用Object序列化......並且對於看起來可能不可序列化的類。

  • 基於我在爲「borZoi」圖書館尋找文檔方面遇到的困難......以及其他事情......我想你可能在圖書館做加密工作時做出了糟糕的選擇。

+0

同意所有積分。 +1 – EJP