1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package cn.wisenergy.chnmuseum.party.common.video;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class VideoEncryptUtil {
//此为AES128位,如果要求AES256位,需要更新jdk内的包,jdk8发布版本默认不支持
public static final String cipher = "3348c95c60520be7";
private static final int dataLength = 4096;
public static InputStream encrypt(InputStream fis, String cipher) throws IOException {
final ByteArrayOutputStream[] outputStream = new ByteArrayOutputStream[1];
AesCipherDataSink encryptingDataSink = new AesCipherDataSink(cipher.getBytes(StandardCharsets.UTF_8),
new DataSink() {
@Override
public void open() {
outputStream[0] = new ByteArrayOutputStream();
}
@Override
public void write(byte[] buffer, int offset, int length) {
outputStream[0].write(buffer, offset, length);
}
@Override
public void close() throws IOException {
outputStream[0].close();
}
});
encryptingDataSink.open();
int len;
byte[] buffer = new byte[dataLength];
while ((len = fis.read(buffer)) != -1) {
encryptingDataSink.write(buffer, 0, len);
}
encryptingDataSink.close();
fis.close();
return new ByteArrayInputStream(outputStream[0].toByteArray());
}
}