TestAesEncryptUtils.java
3.07 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.glsx.smart.service;
import cn.com.glsx.SmartServiceApplication;
import cn.com.glsx.smart.common.utils.AesEncryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.security.SecureRandom;
import java.util.Random;
/**
* @author fengzhi
* @version 1.0.0
* @description: 车钥匙AES加密解密测试
* @createTime 2021年05月08日 15:58:00
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SmartServiceApplication.class)
@Slf4j
public class TestAesEncryptUtils {
@Autowired
private AesEncryptUtils aesEncryptUtils;
@Test
public void testEncryptCrep() {
String encrypt = null;
try {
encrypt = aesEncryptUtils.encrypt("370342948446986240110");
} catch (Exception e) {
e.printStackTrace();
}
log.info("crep加密后:{}",encrypt);
}
@Test
public void testDecryptCrep() {
String decrypt = null;
try {
decrypt = aesEncryptUtils.decrypt("tZvuOJsVI6B8x5Yx3CBOH2MZn/3YhKbUtiVlkP9c+iU=");
} catch (Exception e) {
e.printStackTrace();
}
log.info("crep解密后:{}",decrypt);
}
@Test
public void testEncryptSecretKey() {
String encrypt = null;
try {
encrypt = aesEncryptUtils.encryptSecret("37061517396835532890027292");
} catch (Exception e) {
e.printStackTrace();
}
log.info("SecretKey加密后:{}",encrypt);
}
@Test
public void testDecryptSecretKey() {
String decrypt = null;
try {
decrypt = aesEncryptUtils.decryptSecret("pZz3WfhsWOJSxpky6q8r84EHTSL/rmlkHhcNeWR9bys=");
} catch (Exception e) {
e.printStackTrace();
}
log.info("SecretKey解密后:{}",decrypt);
}
@Test
public void testUUID() {
StringBuilder uid = new StringBuilder();
//产生16位的强随机数
Random rd = new SecureRandom();
for (int i = 0; i < 16; i++) {
//产生0-2的3位随机数
int type = rd.nextInt(3);
switch (type) {
case 0:
//0-9的随机数
uid.append(rd.nextInt(10));
/*int random = ThreadLocalRandom.current().ints(0, 10)
.distinct().limit(1).findFirst().getAsInt();*/
break;
case 1:
//ASCII在65-90之间为大写,获取大写随机
uid.append((char) (rd.nextInt(25) + 65));
break;
case 2:
//ASCII在97-122之间为小写,获取小写随机
uid.append((char) (rd.nextInt(25) + 97));
break;
default:
break;
}
}
String uuid = uid.toString();
log.info("UUID:{}",uuid);
}
}