Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
chnmuseum-party
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liqin
chnmuseum-party
Commits
2fde267d
Commit
2fde267d
authored
Mar 15, 2021
by
liqin
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixed
parent
b592c8c6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
235 additions
and
0 deletions
+235
-0
AesCipherDataSink.java
...nergy/chnmuseum/party/common/video/AesCipherDataSink.java
+36
-0
AesFlushingCipher.java
...nergy/chnmuseum/party/common/video/AesFlushingCipher.java
+91
-0
DataSink.java
...a/cn/wisenergy/chnmuseum/party/common/video/DataSink.java
+18
-0
VideoTestUtil.java
...wisenergy/chnmuseum/party/common/video/VideoTestUtil.java
+64
-0
HiddenMethodFilter.java
...energy/chnmuseum/party/web/filter/HiddenMethodFilter.java
+13
-0
PutFilter.java
...va/cn/wisenergy/chnmuseum/party/web/filter/PutFilter.java
+13
-0
No files found.
src/main/java/cn/wisenergy/chnmuseum/party/common/video/AesCipherDataSink.java
0 → 100644
View file @
2fde267d
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
();
}
}
src/main/java/cn/wisenergy/chnmuseum/party/common/video/AesFlushingCipher.java
0 → 100644
View file @
2fde267d
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
();
}
}
src/main/java/cn/wisenergy/chnmuseum/party/common/video/DataSink.java
0 → 100644
View file @
2fde267d
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
;
}
src/main/java/cn/wisenergy/chnmuseum/party/common/video/VideoTestUtil.java
0 → 100644
View file @
2fde267d
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
();
}
}
src/main/java/cn/wisenergy/chnmuseum/party/web/filter/HiddenMethodFilter.java
0 → 100644
View file @
2fde267d
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
src/main/java/cn/wisenergy/chnmuseum/party/web/filter/PutFilter.java
0 → 100644
View file @
2fde267d
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment