简体   繁体   中英

Socket Programming java - encryption File

I am sending( encrypting and sending the file) and receiving the file over socket:

MY Server code:

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

}

MY receiving Code (at Client side):

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

 }

The file is not receiving at the client side NO exception or nothing. The client just stops here.

What I am doing wrong here?? I have tried Cipher O/I streams. but My problem was While encryption I need to close the CipherOutputStream else the file was not receiving at the client. I need to recieve acknowledgment from client after sending the file, since I am closing the CipherOutputStream in the server, it was not receiving the message from client. It was throwing Socket closed exception.

SO I did a different version (the code given). But that also not working. Please help me with this.

Here is your code with some comments that might help you understand where the problem is. See my comments inside the code.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM