File tree 2 files changed +48
-1
lines changed
main/java/com/codingapi/springboot/framework/crypto
test/java/com/codingapi/springboot/framework/crypto
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 1
1
package com .codingapi .springboot .framework .crypto ;
2
2
3
3
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
4
+ import org .springframework .util .Base64Utils ;
4
5
5
6
import javax .crypto .Cipher ;
6
7
import javax .crypto .KeyGenerator ;
@@ -78,7 +79,7 @@ private SecretKey generateKey(int keySize) throws Exception {
78
79
79
80
private AlgorithmParameters generateIV (byte [] ivs ) throws Exception {
80
81
AlgorithmParameters params = AlgorithmParameters .getInstance (KEY_ALGORITHM );
81
- params .init (new IvParameterSpec (ivs ));
82
+ params .init (new IvParameterSpec (ivs , 0 , 16 ));
82
83
return params ;
83
84
}
84
85
Original file line number Diff line number Diff line change
1
+ package com .codingapi .springboot .framework .crypto ;
2
+
3
+ import org .junit .jupiter .api .BeforeAll ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .springframework .util .Base64Utils ;
6
+
7
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8
+
9
+ class AESTest {
10
+
11
+ static byte [] key ;
12
+ static byte [] iv ;
13
+
14
+ @ BeforeAll
15
+ static void before () throws Exception {
16
+ AES aes = new AES ();
17
+ key = aes .getKey ();
18
+ iv = aes .getIv ();
19
+
20
+ System .out .println ("keys:" + Base64Utils .encodeToString (key ));
21
+ System .out .println ("ivs:" + Base64Utils .encodeToString (iv ));
22
+
23
+ }
24
+
25
+ @ Test
26
+ void aes1 () throws Exception {
27
+ AES aes = new AES ();
28
+ String content = "hello world" ;
29
+ String encrypt = Base64Utils .encodeToString (aes .encrypt (content .getBytes ()));
30
+ System .out .println ("encrypt:" + encrypt );
31
+
32
+ String decrypt = new String (aes .decrypt (Base64Utils .decodeFromString (encrypt )));
33
+ System .out .println ("decrypt:" + decrypt );
34
+
35
+ assertEquals (content , decrypt , "AES encrypt error" );
36
+ }
37
+
38
+ @ Test
39
+ void aes2 () throws Exception {
40
+ AES aes = new AES (key , iv );
41
+ String content = "hello world" ;
42
+ String encrypt = Base64Utils .encodeToString (aes .encrypt (content .getBytes ()));
43
+ System .out .println ("encrypt:" + encrypt );
44
+ }
45
+
46
+ }
You can’t perform that action at this time.
0 commit comments