【发布时间】:2018-12-09 14:06:07
【问题描述】:
我想加密流,然后使用 Amazon S3 发送它。我正在使用遗留代码并有两个重要参数:未加密的InputStream 及其长度。这很重要,因为 AmazonS3Client 想在上传流之前知道流的长度。
加密流并不是很困难的任务:
InputStream in = new FileInputStream("path-to-file");
KeyGenerator keygen = KeyGenerator.getInstance("AES");
Key key = keygen.generateKey();
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
OutputStream encryptedStream = new ByteArrayOutputStream();
CipherOutputStream out = new CipherOutputStream(encryptedStream, cipher);
out.write(in.read());
out.flush();
byte[] bytes = ((ByteArrayOutputStream) encryptedStream).toByteArray();
InputStream encryptedInput = new ByteArrayInputStream(bytes);
但是,这种方法只能用于小文件,因为它们被写入内存中的字节数组。问题是我想加密大文件(> 1TB)。我该怎么做呢?更具体地说,我得到 InputStream,我的工作是返回将由 Amazon S3 使用的加密 InputStream。
【问题讨论】:
-
所以,总结一下:你想从未加密文件的大小知道加密文件的大小..
-
现在,我想加密输入流并最终加密输入流。
标签: java encryption amazon-s3