-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSM4EncryptUtilsTest.java
More file actions
58 lines (48 loc) · 2.32 KB
/
Copy pathSM4EncryptUtilsTest.java
File metadata and controls
58 lines (48 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cn.yang37.sm;
import cn.yang37.utils.HexUtils;
import cn.yang37.utils.RandomUtils;
import cn.yang37.utils.TraceUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
class SM4EncryptUtilsTest {
@Test
void name1() throws Exception {
TraceUtils.start("SM4加解密");
String plaintext = RandomUtils.generateRandomString(200);
String keyHex = HexUtils.byteArr2Hex(SM4KeyUtils.generateKey());
String ivHex = SM4EncryptUtils.generateIv2Hex();
log.info("PlainText: {}", plaintext);
log.info("Key(Hex): {}", keyHex);
log.info("IV(Hex): {}", ivHex);
log.info("");
for (SM4EncryptUtils.Mode mode : SM4EncryptUtils.Mode.values()) {
for (SM4EncryptUtils.Padding padding : SM4EncryptUtils.Padding.values()) {
String encryptedHex = SM4EncryptUtils.encrypt4Hex(plaintext, keyHex, ivHex, mode, padding);
log.info("[{}][{}] Encrypted(Hex): {}", mode, padding, encryptedHex);
String decryptedText = SM4EncryptUtils.decrypt4Hex(encryptedHex, keyHex, ivHex, mode, padding);
log.info("[{}][{}] Decrypted(Text): {}", mode, padding, decryptedText);
log.info("");
}
}
}
@Test
void name2() throws Exception {
String plaintext = RandomUtils.generateRandomString(200);
String keyBase64 = HexUtils.byteArr2Base64(SM4KeyUtils.generateKey());
String ivBase64 = SM4EncryptUtils.generateIv2Base64();
log.info("[SM4] plaintext: {}", plaintext);
log.info("[SM4] key(Base64): {}", keyBase64);
log.info("[SM4] iv(Base64): {}", ivBase64);
log.info("");
for (SM4EncryptUtils.Mode mode : SM4EncryptUtils.Mode.values()) {
for (SM4EncryptUtils.Padding padding : SM4EncryptUtils.Padding.values()) {
String encryptedBase64 = SM4EncryptUtils.encrypt4Base64(plaintext, keyBase64, ivBase64, mode, padding);
log.info("[SM4][{}][{}] Encrypted(Base64): {}", mode, padding, encryptedBase64);
String decryptedText = SM4EncryptUtils.decrypt4Base64(encryptedBase64, keyBase64, ivBase64, mode, padding);
log.info("[SM4][{}][{}] Decrypted(Text): {}", mode, padding, decryptedText);
log.info("");
}
}
}
}