【问题标题】:Socket Programming java - encryption FileSocket编程java - 加密文件
【发布时间】:2014-10-29 23:13:00
【问题描述】:

我正在发送(加密和发送文件)并通过套接字接收文件:

我的服务器代码:

     private void send(OutputStream op,
        FileInputStream filetoprocess, long l) throws Throwable {


    Cipher ecipher;
    byte[] inputBytes = new byte[(int) l];
    filetoprocess.read(inputBytes);

    byte[] ivBytes = "1234567812345678".getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(ivBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(desKeySpec);
    ecipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] outputBytes = ecipher.doFinal(inputBytes);

    op.write(outputBytes);
    op.flush();

    System.out.println("File sent");

}

我的接收代码(在客户端):

private static void receive(InputStream ip, File fname,
        PrintWriter output2) throws Throwable    {


    byte[] ivBytes = "1234567812345678".getBytes();

    Cipher dcipher ;
    DESKeySpec desKeySpec = new DESKeySpec(ivBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(desKeySpec);

    dcipher = Cipher.getInstance("DES");
    dcipher.init(Cipher.DECRYPT_MODE, sKey);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    byte[] buffer = new byte[1024]; 
    int length;

    while ((length = ip.read(buffer)) != -1)
     { 
       out.write(buffer, 0, length); 
      }

    byte[] result = out.toByteArray();

    byte[] outputBytes = dcipher.doFinal(result);

    FileOutputStream outputStream = new FileOutputStream(fname);
    outputStream.write(outputBytes);
    outputStream.close();

    System.out.println("File received");

 }

文件没有在客户端接收没有异常或什么都没有。客户就到此为止了。

我在这里做错了什么?我已经尝试过 Cipher O/I 流。但我的问题是加密时我需要关闭 CipherOutputStream 否则文件没有在客户端接收。发送文件后我需要收到客户端的确认,因为我正在关闭服务器中的 CipherOutputStream,它没有收到来自客户端的消息。它正在抛出 Socket 关闭异常。

所以我做了一个不同的版本(给出的代码)。但这也行不通。请帮我解决一下这个。

【问题讨论】:

  • “客户端停在这里”,这里到底是哪里?我复制/粘贴您的代码,它包含错误,服务器代码中的密码和客户端代码中的密码无法解析。一个想法:尝试不加密发送文件,成功后再加密。
  • 你能告诉我什么错误吗?我只需要以加密形式发送。
  • 是的,同意你的看法。我给了你解决问题的想法,首先尝试发送文件,一旦你得到正确的文件然后尝试加密它。错误在服务器代码中:ecipher.init(Cipher.ENCRYPT_MODE, sKey); 变量ecipher 无法解析,在客户端代码中:dcipher.init(Cipher.DECRYPT_MODE, sKey); 变量dcipher 无法解析。
  • 我刚刚添加了它们。它们是在函数外部声明的。

标签: java sockets encryption


【解决方案1】:

这是您的代码,其中包含一些 cmets,可以帮助您了解问题所在。 在代码中查看我的 cmets。

    public void send( OutputStream op, 
                        FileInputStream filetoprocess, 
                        long l) throws Throwable
{

    byte[] inputBytes = new byte[(int) l];
    int iRead = filetoprocess.read(inputBytes);
    if (iRead != l)
    {
        System.out.println("Read error.");
        return;
    }
    byte[] ivBytes = "1234567812345678".getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(ivBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(desKeySpec);
    Cipher ecipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] outputBytes = ecipher.doFinal(inputBytes);

    // Send the file's size, 4 bytes.
    // Use an 8 byte buffer to send big files > 2GB.
    byte[] fileSize = new byte[4];
    fileSize[0] = (byte) ((iRead & 0xff000000) >> 24);
    fileSize[1] = (byte) ((iRead & 0x00ff0000) >> 16);
    fileSize[2] = (byte) ((iRead & 0x0000ff00) >>  8);
    fileSize[3] = (byte)  (iRead & 0x000000ff);
    op.write(fileSize, 0, 4);

    // Now send the file's data
    op.write(outputBytes);
    op.flush();

    System.out.println("File sent");

}

public static void receive(
                    InputStream ip, 
                    File fname, 
                    PrintWriter output2) throws Throwable
{

    byte[] ivBytes = "1234567812345678".getBytes();

    DESKeySpec desKeySpec = new DESKeySpec(ivBytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(desKeySpec);

    Cipher dcipher = Cipher.getInstance("DES");
    dcipher.init(Cipher.DECRYPT_MODE, sKey);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    // Receive the file's size first  
    byte[] bSize = new byte[4];
    ip.read(bSize, 0, 4);
    int fileSize=0;
    fileSize = (int) (bSize[0]) << 24 | (int) (bSize[1]) << 16 | 
               (int) (bSize[2]) << 8 | (int) (bSize[3]);

    // use a 4 or 8K buffer for better performance
    byte[] buffer = new byte[8*1024];
    int length;

    // Read up to the file size
    while (fileSize > 0)
    {
        if (fileSize > buffer.length) length=buffer.length;
        else length=fileSize;
        int iRead = ip.read(buffer, 0, length);
        if (iRead > 0) 
        {
            out.write(buffer, 0, iRead);
            fileSize -= iRead;              
        }
    }

    byte[] result = out.toByteArray();

    byte[] outputBytes = dcipher.doFinal(result);

    FileOutputStream outputStream = new FileOutputStream(fname);
    outputStream.write(outputBytes);
    outputStream.close();

    System.out.println("File received");

}

【讨论】:

  • 我了解您的意见。但我需要打开套接字才能发送其他信息。这是主要问题。如果关闭输出流,它将关闭套接字。
  • 那么在发送文件数据之前需要先发送文件的大小。有关示例,请参见编辑后的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多