Commit 2fde267d authored by liqin's avatar liqin 💬

bug fixed

parent b592c8c6
package cn.wisenergy.chnmuseum.party.common.video;
import javax.crypto.Cipher;
import java.io.IOException;
public final class AesCipherDataSink implements DataSink {
private final DataSink wrappedDataSink;
private final byte[] secretKey;
private AesFlushingCipher cipher;
public AesCipherDataSink(byte[] secretKey, DataSink wrappedDataSink) {
this.wrappedDataSink = wrappedDataSink;
this.secretKey = secretKey;
}
@Override
public void open() throws IOException {
wrappedDataSink.open();
cipher = new AesFlushingCipher(Cipher.ENCRYPT_MODE, secretKey, 0, 0);
}
@Override
public void write(byte[] data, int offset, int length) throws IOException {
cipher.updateInPlace(data, offset, length);
wrappedDataSink.write(data, offset, length);
}
@Override
public void close() throws IOException {
cipher = null;
wrappedDataSink.close();
}
}
package cn.wisenergy.chnmuseum.party.common.video;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public final class AesFlushingCipher {
private final Cipher cipher;
private final int blockSize;
private final byte[] zerosBlock;
private final byte[] flushedBlock;
private int pendingXorBytes;
public AesFlushingCipher(int mode, byte[] secretKey, long nonce, long offset) {
try {
cipher = Cipher.getInstance("AES/CTR/NoPadding");
blockSize = cipher.getBlockSize();
zerosBlock = new byte[blockSize];
flushedBlock = new byte[blockSize];
long counter = offset / blockSize;
int startPadding = (int) (offset % blockSize);
cipher.init(
mode,
new SecretKeySpec(secretKey, cipher.getAlgorithm().split("/", 2)[0]),
new IvParameterSpec(getInitializationVector(nonce, counter)));
if (startPadding != 0) {
updateInPlace(new byte[startPadding], 0, startPadding);
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| InvalidAlgorithmParameterException e) {
throw new RuntimeException(e);
}
}
public void updateInPlace(byte[] data, int offset, int length) {
update(data, offset, length, data, offset);
}
public void update(byte[] in, int inOffset, int length, byte[] out, int outOffset) {
while (pendingXorBytes > 0) {
out[outOffset] = (byte) (in[inOffset] ^ flushedBlock[blockSize - pendingXorBytes]);
outOffset++;
inOffset++;
pendingXorBytes--;
length--;
if (length == 0) {
return;
}
}
int written = nonFlushingUpdate(in, inOffset, length, out, outOffset);
if (length == written) {
return;
}
int bytesToFlush = length - written;
if (!(bytesToFlush < blockSize)) {
throw new IllegalStateException();
}
outOffset += written;
pendingXorBytes = blockSize - bytesToFlush;
written = nonFlushingUpdate(zerosBlock, 0, pendingXorBytes, flushedBlock, 0);
if (!(written == blockSize)) {
throw new IllegalStateException();
}
for (int i = 0; i < bytesToFlush; i++) {
out[outOffset++] = flushedBlock[i];
}
}
private int nonFlushingUpdate(byte[] in, int inOffset, int length, byte[] out, int outOffset) {
try {
return cipher.update(in, inOffset, length, out, outOffset);
} catch (ShortBufferException e) {
throw new RuntimeException(e);
}
}
private byte[] getInitializationVector(long nonce, long counter) {
return ByteBuffer.allocate(16).putLong(nonce).putLong(counter).array();
}
}
package cn.wisenergy.chnmuseum.party.common.video;
import java.io.IOException;
public interface DataSink {
interface Factory {
DataSink createDataSink();
}
void open() throws IOException;
void write(byte[] buffer, int offset, int length) throws IOException;
void close() throws IOException;
}
package cn.wisenergy.chnmuseum.party.common.video;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@Slf4j
public class VideoTestUtil {
private static final String plainFilePath = "D:\\200.tmp\\";
//此为AES128位,如果要求AES256位,需要更新jdk内的包,jdk8发布版本默认不支持
private static final String cipher = "3348c95c60520be7";
private static final int dataLength = 4096;
public static final void main(String[] args){
//加密视频
new Thread(new Runnable() {
@Override
public void run() {
try {
log.info("开始加解密");
File f = new File(plainFilePath, "中华人民共和国成立.mp4");
FileInputStream fis = new FileInputStream(f);
File encryptFile = new File(plainFilePath, "中华人民共和国成立.chnmuseum");
AesCipherDataSink encryptingDataSink = new AesCipherDataSink(
"3348c95c60520be7".getBytes(StandardCharsets.UTF_8),
new DataSink() {
private FileOutputStream fileOutputStream;
@Override
public void open() throws IOException {
fileOutputStream = new FileOutputStream(encryptFile);
}
@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
fileOutputStream.write(buffer, offset, length);
}
@Override
public void close() throws IOException {
fileOutputStream.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();
log.info("加解密完成");
} catch (Exception e) {
log.info(e.getLocalizedMessage());
}
}
}).start();
}
}
package cn.wisenergy.chnmuseum.party.web.filter;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.HiddenHttpMethodFilter;
/**
* 但是需要把form表单的enctype属性设置为application/x-www-form-urlencoded
*
* @author Danny Lee
*/
@Component
public class HiddenMethodFilter extends HiddenHttpMethodFilter {
}
\ No newline at end of file
package cn.wisenergy.chnmuseum.party.web.filter;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.FormContentFilter;
/**
* 解决SpringBoot获取不到PUT方式提交的参数的问题,但是需要把form表单的enctype属性设置为application/x-www-form-urlencoded
*
* @author Danny Lee
*/
@Component
public class PutFilter extends FormContentFilter {
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment