使用AES进行大文件传输

时间:2013-01-20 05:02:42

标签: java android file aes

我想在服务器和客户端之间传输一个大文件,我知道如何做到这一点: 对于客户端,代码如下:

OutputStream output = sk.getOutputStream();     

    FileInputStream fileInputStream = new FileInputStream(file);
    byte[] buffer = new byte[1024*1024];
    int bytesRead = 0;

    while((bytesRead = fileInputStream.read(buffer))>0)
    {
        output.write(buffer,0,bytesRead);
    }

    fileInputStream.close();

服务器端:

InputStream input = sk.getInputStream();

    file = new File(filename);
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[1024*1024];

    int bytesReceived = 0;

    while((bytesReceived = input.read(buffer))>0) {
        out.write(buffer,0,bytesReceived);
        System.out.println(bytesReceived);
        break;
    }

现在的问题是,服务器和客户端就128位AES密钥达成一致,因此我想充分利用会话密钥来确保文件传输过程的安全。我的问题是,我在哪里可以在我的客户端和服务器中插入AES加密过程?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你可以使用javax.crypto.CipherInputStream / javax.crypto.CipherOutputStream。用法示例http://www.flexiprovider.de/examples/ExampleCrypt.html