套接字编程java - 加密文件

时间:2014-10-28 16:10:33

标签: java sockets encryption

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

我的服务器代码:

     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关闭异常。

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

1 个答案:

答案 0 :(得分:0)

以下是您的代码,其中包含一些注释,可帮助您了解问题所在。 请参阅我在代码中的评论。

    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");

}