Skip to content

Commit a4d9c72

Browse files
committed
fix #22
1 parent 4effb46 commit a4d9c72

File tree

2 files changed

+48
-1
lines changed
  • springboot-starter/src
    • main/java/com/codingapi/springboot/framework/crypto
    • test/java/com/codingapi/springboot/framework/crypto

2 files changed

+48
-1
lines changed

Diff for: springboot-starter/src/main/java/com/codingapi/springboot/framework/crypto/AES.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.framework.crypto;
22

33
import org.bouncycastle.jce.provider.BouncyCastleProvider;
4+
import org.springframework.util.Base64Utils;
45

56
import javax.crypto.Cipher;
67
import javax.crypto.KeyGenerator;
@@ -78,7 +79,7 @@ private SecretKey generateKey(int keySize) throws Exception {
7879

7980
private AlgorithmParameters generateIV(byte[] ivs) throws Exception {
8081
AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM);
81-
params.init(new IvParameterSpec(ivs));
82+
params.init(new IvParameterSpec(ivs, 0, 16));
8283
return params;
8384
}
8485

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)