客户端服务器porgram文件在服务器加密并在客户端java解密

时间:2016-07-08 16:35:05

标签: java file encryption server client

需要工作示例客户端服务器程序文件在服务器上加密,并在客户端java解密到我的学术项目。

我尝试了很多,在客户端解密时输出文件将为零字节

//客户端程序

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class FileTransferClient { 

    public static void main(String[] args) throws Exception{

        //Initialize socket
        Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);
        byte[] contents = new byte[100000000];

        //Initialize the FileOutputStream to the output file's full path.
        FileOutputStream fos = new FileOutputStream("/home/shanmukhh/Desktop/op.mp4");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        InputStream is = socket.getInputStream();
        System.out.println("is: "+is);
        //No of bytes read in one read() call
        int bytesRead = 0; 
        String key ="1234567812345678";
        Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] outputBytes =null;
        while((bytesRead=is.read(is.toString().getBytes()))!=-1){
            outputBytes = cipher.doFinal(is.toString().getBytes());
            bos.write(outputBytes, 0, bytesRead); 
        }

        bos.flush(); 
        socket.close(); 



        System.out.println("File saved successfully!");
    }
    }

//服务器端

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class FileTransferServer { 

    public static void main(String[] args) throws Exception {
        //Initialize Sockets
        ServerSocket ssock = new ServerSocket(5000);
        Socket socket = ssock.accept();

        //The InetAddress specification
        InetAddress IA = InetAddress.getByName("localhost"); 

        //Specify the file
        File file = new File("/home/shanmukhh/Downloads/ApacheSpark.mp4");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis); 

        //Get socket's output stream
        OutputStream os = socket.getOutputStream();

        //Read File Contents into contents array 
        byte[] contents;
        long fileLength = file.length(); 
        long current = 0;

        long start = System.nanoTime();
        while(current!=fileLength){ 
            int size = 100000000;
            if(fileLength - current >= size)
                current += size;    
            else{ 
                size = (int)(fileLength - current); 
                current = fileLength;
            } 
            contents = new byte[size]; 
            bis.read(contents, 0, size); 
            String key = "1234567812345678";
            Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] outputBytes = cipher.doFinal(bis.toString().getBytes("UTF-8"));

            os.write(outputBytes);
            System.out.println(outputBytes.toString().getBytes("UTF-8"));
            System.out.print("Sending file ... "+(current*100)/fileLength+"% complete!");
            System.out.println(outputBytes.toString().getBytes("UTF-8"));
        }   

        //os.flush(); 
        //File transfer done. Close the socket connection!
        socket.close();
        ssock.close();
        System.out.println("File sent succesfully!");
    }
}

先谢谢

0 个答案:

没有答案