VideoEncryptUtil.java 1.65 KB
Newer Older
liqin's avatar
liqin committed
1 2
package cn.wisenergy.chnmuseum.party.common.video;

liqin's avatar
liqin committed
3 4 5 6
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
liqin's avatar
liqin committed
7 8 9 10 11
import java.nio.charset.StandardCharsets;

public class VideoEncryptUtil {

    //此为AES128位,如果要求AES256位,需要更新jdk内的包,jdk8发布版本默认不支持
liqin's avatar
liqin committed
12
    public static final String cipher = "3348c95c60520be7";
liqin's avatar
liqin committed
13 14
    private static final int dataLength = 4096;

liqin's avatar
liqin committed
15
    public static InputStream encrypt(InputStream fis, String cipher) throws IOException {
liqin's avatar
liqin committed
16 17
        final ByteArrayOutputStream[] outputStream = new ByteArrayOutputStream[1];
        AesCipherDataSink encryptingDataSink = new AesCipherDataSink(cipher.getBytes(StandardCharsets.UTF_8),
liqin's avatar
liqin committed
18 19 20 21 22 23 24
                new DataSink() {
                    @Override
                    public void open() {
                        outputStream[0] = new ByteArrayOutputStream();
                    }

                    @Override
liqin's avatar
liqin committed
25
                    public void write(byte[] buffer, int offset, int length) {
liqin's avatar
liqin committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
                        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();
liqin's avatar
liqin committed
42
        return new ByteArrayInputStream(outputStream[0].toByteArray());
liqin's avatar
liqin committed
43 44 45
    }

}