教育训练处考试制证系统后端
“djh”
2025-04-24 f9cb0f3f384279b2cbe87c35dde3ba573a0197f3
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.ruoyi;
 
import com.alibaba.fastjson2.JSONObject;
import com.gkhy.exam.institutionalaccess.model.req.ThCertReqDTO;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.exception.BusinessException;
import lombok.extern.log4j.Log4j2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
 
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
@ActiveProfiles("dev")
@Log4j2
public class TestEncrypt {
 
 
    private static final String key="Bd26jqDDJcdnBocn";
    private static final String iv ="oVKRQCjElggSbd8D";
 
    /**
     * 加密
     */
    @Test
    public void text001(){
 
        //加密
        List<ThCertReqDTO> thCertDTOs=new ArrayList<>();
        ThCertReqDTO thCertDTO = new ThCertReqDTO();
        thCertDTO.setUuid(UUID.randomUUID().toString());
        JSONObject jsonObject = new JSONObject();
        thCertDTO.setName("王五");
        thCertDTO.setIdcard("123456789012345679");
        thCertDTO.setBatchUuid("1ef0b81f-dcb7-62a9-ad8f-63e252089bc8");
        thCertDTO.setTrainOrgName("1234567890123");
        thCertDTO.setCertTime(LocalDateTime.now());
        thCertDTO.setCertUrl("https://p26.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/22f10850c5234b5285350743cfa16357");
        thCertDTOs.add(thCertDTO);
        String jsonString = JSONObject.toJSONString(thCertDTOs);
        String encrypt = encrypt(jsonString);
        log.info("加密后为:"+encrypt);
 
        //解密
        String decrypt = "";
        try {
            decrypt = decrypt(encrypt);
        }catch (Exception e){
            throw new BusinessException(this.getClass(), ResultConstants.THREE_INSTITUTION_PARAMM_NULL);
        }
        log.info("解密后为:"+decrypt);
    }
 
    @Test
    public void dec(){
        String mes = "nCVoBsCqZnKKYg0NBnsUPgjZpienDHaThN+mIcdcK9IhbjCEZj+ePQJUyLqXWY5+On7OiaCBPbdpx9NbsmR/IbIz0eVhh0BuI0iwbsB5vxkFd0LQHb8GkIM0aqMjwgQLHb1QQKYgne3Nh6/GsOlFveYQJPwiwXfdzsd/zKeWM6gohNBYuXDHAkwTboq/L7TFDVjTNfbvsu8pCVc0txoFmOTkvq5JUCsqBk4RvmM/AZ+yZjTCYkzkufg/sF5CFpbr36nCOzCGGJc1/+wm2qREzzC0HUxinhsXoMm6qJqa7/M3XDjWh/t6SDtxlzBLEgUcyEj2FL7vWLG9oB7EsnExcuY5YpQxJG45qGY6y9kYDF/swQaPsPIpGJhtczamAZuwi/DHuGWJsxqEWyWVEIc80gRek59j0vBMAmn/QVVP3Zu+qtxbJKRY0BAEaNKvIb66";
        String decrypt = "";
        try {
            decrypt = decrypt(mes);
        }catch (Exception e){
            throw new BusinessException(this.getClass(), ResultConstants.THREE_INSTITUTION_PARAMM_NULL);
        }
        log.info("解密后为:"+decrypt);
    }
 
 
    private String decrypt(String data) {
        try
        {
            byte[] encryptCode = Base64.getDecoder().decode(data);//先用base64解密
 
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
 
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
 
            byte[] original = cipher.doFinal(encryptCode);
            String originalString = new String(original);
            return originalString.trim();
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
 
 
    private String encrypt(String data){
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes(Constants.UTF8);
            int plaintextLength = dataBytes.length;
 
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
 
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
 
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());  // CBC模式,需要一个向量iv,可增加加密算法的强度
 
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            // BASE64做转码。
            String aseEncode = Base64.getEncoder().encodeToString(encrypted);
            return aseEncode;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
 
 
 
}