对比新文件 |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |