From 2b0b8ae0a9192f4cc5358ef0e9521e0d31cdcd11 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 25 Aug 2020 18:09:21 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=A2=9E=E5=8A=A0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8D=8F=E8=AE=AE=E5=8A=A0=E5=AF=86=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=202.=20=E4=BF=AE=E6=AD=A3=E5=88=A4=E6=96=ADDeviceTask=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E9=87=8D=E5=A4=8D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application-dispose.properties | 6 + pom.xml | 6 + .../com/dispose/common/ProtoCryptoType.java | 20 ++- .../dispose/common/SecurityConfigValue.java | 23 +++ .../com/dispose/config/SecurityConfigure.java | 50 ++++++ .../dispose/controller/AuthController.java | 3 +- .../dispose/interceptor/ProtocolSecurity.java | 94 +++++++++++ .../manager/impl/DeviceTaskManagerImpl.java | 2 +- .../com/dispose/mapper/DeviceTaskMapper.java | 8 +- .../com/security/annotation/Decryption.java | 16 ++ .../com/security/annotation/Encryption.java | 16 ++ .../com/security/arithmetic/CryptoHelper.java | 148 ++++++++++++++++++ .../protocol/DecryptRequestProtocol.java | 130 +++++++++++++++ src/main/resources/mappers/DeviceTask.xml | 6 +- .../test/Global/InitTestEnvironment.java | 2 +- .../java/com/dispose/test/debug/demo.java | 55 +++++++ .../test/mapper/DeviceTaskMapperTest.java | 4 +- 17 files changed, 571 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/dispose/common/SecurityConfigValue.java create mode 100644 src/main/java/com/dispose/config/SecurityConfigure.java create mode 100644 src/main/java/com/dispose/interceptor/ProtocolSecurity.java create mode 100644 src/main/java/com/security/annotation/Decryption.java create mode 100644 src/main/java/com/security/annotation/Encryption.java create mode 100644 src/main/java/com/security/arithmetic/CryptoHelper.java create mode 100644 src/main/java/com/security/protocol/DecryptRequestProtocol.java diff --git a/config/application-dispose.properties b/config/application-dispose.properties index 631c4582..9b50ba12 100644 --- a/config/application-dispose.properties +++ b/config/application-dispose.properties @@ -19,3 +19,9 @@ permission.admin-users=admin # 认证配置 auth.verify-request-token=true auth.token-timeout-minute=30 + +# 安全配置 +crypto.security-protocol-type=0 +crypto.aes-key="hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40" +crypto.des-key="P3mq9iSIvQcvfyfdWR8sAnfAadO" + diff --git a/pom.xml b/pom.xml index 4596a7fe..1a0e7a1f 100644 --- a/pom.xml +++ b/pom.xml @@ -201,6 +201,12 @@ ipaddress 5.2.1 + + org.jetbrains + annotations + 20.0.0 + compile + diff --git a/src/main/java/com/dispose/common/ProtoCryptoType.java b/src/main/java/com/dispose/common/ProtoCryptoType.java index caf28398..74fb9231 100644 --- a/src/main/java/com/dispose/common/ProtoCryptoType.java +++ b/src/main/java/com/dispose/common/ProtoCryptoType.java @@ -7,13 +7,21 @@ package com.dispose.common; */ public enum ProtoCryptoType { /** - * Crypto none proto crypto type. + * The Crypto none. */ CRYPTO_NONE(0, "不加密"), + /** + * The Crypto base 64. + */ CRYPTO_BASE64(1, "Base64编码"), + /** + * The Crypto aes 256. + */ CRYPTO_AES256(2, "AES256加密"), - CRYPTO_RSA(3, "RSA非对称加密"), - CRYPTO_DES(4, "DES对称加密") + /** + * The Crypto des. + */ + CRYPTO_DES(3, "DES对称加密"), ; /** @@ -54,6 +62,12 @@ public enum ProtoCryptoType { return this.readme; } + /** + * Verify crypto valuable boolean. + * + * @param code the code + * @return the boolean + */ public static boolean verifyCryptoValuable(int code) { return code >= CRYPTO_NONE.getCode() && code <= CRYPTO_DES.getCode(); } diff --git a/src/main/java/com/dispose/common/SecurityConfigValue.java b/src/main/java/com/dispose/common/SecurityConfigValue.java new file mode 100644 index 00000000..6eb12e03 --- /dev/null +++ b/src/main/java/com/dispose/common/SecurityConfigValue.java @@ -0,0 +1,23 @@ +package com.dispose.common; + +/** + * The type Security config value. + * + * @author + */ +public class SecurityConfigValue { + /** + * The constant AES_KEY. + */ + public static String AES_KEY = ""; + + /** + * The constant DES_KEY. + */ + public static String DES_KEY = ""; + + /** + * The constant SECURITY_PROTOCOL_TYPE. + */ + public static Integer SECURITY_PROTOCOL_TYPE = 0; +} diff --git a/src/main/java/com/dispose/config/SecurityConfigure.java b/src/main/java/com/dispose/config/SecurityConfigure.java new file mode 100644 index 00000000..4c03124e --- /dev/null +++ b/src/main/java/com/dispose/config/SecurityConfigure.java @@ -0,0 +1,50 @@ +package com.dispose.config; + +import com.dispose.common.ProtoCryptoType; +import com.dispose.common.SecurityConfigValue; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.Optional; + +/** + * The type Security configure. + * + * @author + */ +@Getter +@Setter +@Component +@ConfigurationProperties(prefix = "crypto") +@Configuration +public class SecurityConfigure { + /** + * The Aes key. + */ + private String aesKey; + + /** + * The Des key. + */ + private String desKey; + + /** + * The Security protocol type. + */ + private Integer securityProtocolType; + + /** + * Init global value. + */ + @PostConstruct + private void initGlobalValue() { + SecurityConfigValue.AES_KEY = Optional.ofNullable(aesKey).orElse(""); + SecurityConfigValue.DES_KEY = Optional.ofNullable(desKey).orElse(""); + SecurityConfigValue.SECURITY_PROTOCOL_TYPE = + Optional.ofNullable(securityProtocolType).orElse(ProtoCryptoType.CRYPTO_NONE.getCode()); + } +} diff --git a/src/main/java/com/dispose/controller/AuthController.java b/src/main/java/com/dispose/controller/AuthController.java index e76f0109..fd7f6fff 100644 --- a/src/main/java/com/dispose/controller/AuthController.java +++ b/src/main/java/com/dispose/controller/AuthController.java @@ -10,6 +10,7 @@ import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; import com.dispose.pojo.dto.protocol.base.ValidGroups; import com.dispose.pojo.po.MulReturnType; import com.dispose.service.UserAccountService; +import com.security.annotation.Decryption; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @@ -54,7 +55,7 @@ public class AuthController { @PostMapping("/login") @ResponseBody @ApiOperation("登录") - public ProtocolRespDTO userLogin( + public ProtocolRespDTO userLogin(@Decryption @Validated(ValidGroups.LoginReqValid.class) @RequestBody ProtocolReqDTO mr) throws NoSuchAlgorithmException { diff --git a/src/main/java/com/dispose/interceptor/ProtocolSecurity.java b/src/main/java/com/dispose/interceptor/ProtocolSecurity.java new file mode 100644 index 00000000..94177a61 --- /dev/null +++ b/src/main/java/com/dispose/interceptor/ProtocolSecurity.java @@ -0,0 +1,94 @@ +package com.dispose.interceptor; + +import com.security.annotation.Decryption; +import com.security.protocol.DecryptRequestProtocol; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; + +import java.lang.reflect.Type; + +/** + * The type Protocol security. + * + * @author + */ +@Slf4j +@RestControllerAdvice +public class ProtocolSecurity implements RequestBodyAdvice { + /** + * Supports boolean. + * + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the boolean + */ + @Override + public boolean supports(@NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return methodParameter.hasParameterAnnotation(Decryption.class); + } + + /** + * Before body read http input message. + * + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the http input message + */ + @Override + @NotNull + public HttpInputMessage beforeBodyRead(@NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return new DecryptRequestProtocol(httpInputMessage); + } + + /** + * Handle empty body object. + * + * @param o the o + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the object + */ + @Override + public Object handleEmptyBody(Object o, + @NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return o; + } + + /** + * After body read object. + * + * @param o the o + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the object + */ + @Override + @NotNull + public Object afterBodyRead(@NotNull Object o, + @NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return o; + } +} diff --git a/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java index 17fac82c..7eaee307 100644 --- a/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java +++ b/src/main/java/com/dispose/manager/impl/DeviceTaskManagerImpl.java @@ -36,7 +36,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override public boolean addDisposeDeviceTaskInfo(Long taskId, Long deviceId, Long attackTypeMask) { // 如果已经存在相同任务,不再添加重复任务 - if (deviceTaskMapper.getTaskByDetails(taskId, deviceId, attackTypeMask).size() != 0) { + if (deviceTaskMapper.getTaskByDetails(taskId, deviceId).size() != 0) { return true; } diff --git a/src/main/java/com/dispose/mapper/DeviceTaskMapper.java b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java index e5ec61ae..624c5500 100644 --- a/src/main/java/com/dispose/mapper/DeviceTaskMapper.java +++ b/src/main/java/com/dispose/mapper/DeviceTaskMapper.java @@ -68,14 +68,12 @@ public interface DeviceTaskMapper { /** * Gets task by details. * - * @param taskId the task id - * @param deviceId the device id - * @param attackType the attack type + * @param taskId the task id + * @param deviceId the device id * @return the task by details */ List getTaskByDetails(@Param("taskId") Long taskId, - @Param("deviceId") Long deviceId, - @Param("attackType") Long attackType); + @Param("deviceId") Long deviceId); /** * Change task status int. diff --git a/src/main/java/com/security/annotation/Decryption.java b/src/main/java/com/security/annotation/Decryption.java new file mode 100644 index 00000000..da724a7b --- /dev/null +++ b/src/main/java/com/security/annotation/Decryption.java @@ -0,0 +1,16 @@ +package com.security.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The interface Decryption. + * + * @author + */ +@Target({ElementType.PARAMETER, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Decryption { +} diff --git a/src/main/java/com/security/annotation/Encryption.java b/src/main/java/com/security/annotation/Encryption.java new file mode 100644 index 00000000..17c83056 --- /dev/null +++ b/src/main/java/com/security/annotation/Encryption.java @@ -0,0 +1,16 @@ +package com.security.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The interface Encryption. + * + * @author + */ +@Target({ElementType.PARAMETER, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Encryption { +} diff --git a/src/main/java/com/security/arithmetic/CryptoHelper.java b/src/main/java/com/security/arithmetic/CryptoHelper.java new file mode 100644 index 00000000..9f45da54 --- /dev/null +++ b/src/main/java/com/security/arithmetic/CryptoHelper.java @@ -0,0 +1,148 @@ +package com.security.arithmetic; + +import jodd.util.Base64; +import lombok.extern.slf4j.Slf4j; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; + +/** + * The type Crypto helper. + * + * @author + */ +@Slf4j +public class CryptoHelper { + + /** + * The constant AES_ALGORITHM_STR. + */ + private static final String AES_ALGORITHM_STR = "AES/ECB/PKCS5Padding"; + + /** + * The constant DES_ALGORITHM_STR. + */ + private static final String DES_ALGORITHM_STR = "DES/ECB/PKCS5Padding"; + + /** + * Base 64 decryption byte [ ]. + * + * @param ciphertext the ciphertext + * @return the byte [ ] + */ + public static byte[] base64Decryption(String ciphertext) { + return Base64.decode(ciphertext); + } + + /** + * Base 64 encryption string. + * + * @param plaintext the plaintext + * @return the string + */ + public static String base64Encryption(byte[] plaintext) { + return Base64.encodeToString(plaintext); + } + + /** + * Sha 256 encryption byte [ ]. + * + * @param plaintext the plaintext + * @return the byte [ ] + * @throws NoSuchAlgorithmException the no such algorithm exception + */ + public static byte[] sha256Encryption(String plaintext) throws NoSuchAlgorithmException { + MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); + messageDigest.update(plaintext.getBytes(StandardCharsets.UTF_8)); + return messageDigest.digest(); + } + + /** + * Aes 256 encryption byte [ ]. + * + * @param plaintext the ciphertext + * @param aesKey the aes key + * @return the byte [ ] + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchPaddingException the no such padding exception + * @throws InvalidKeyException the invalid key exception + * @throws BadPaddingException the bad padding exception + * @throws IllegalBlockSizeException the illegal block size exception + */ + public static byte[] aes256Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException, + NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(sha256Encryption(aesKey)); + + keyGen.init(256, secureRandom); + Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); + SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); + cipher.init(Cipher.ENCRYPT_MODE, key); + return cipher.doFinal(plaintext); + } + + /** + * Aes 256 decryption byte [ ]. + * + * @param ciphertext the ciphertext + * @param aesKey the aes key + * @return the byte [ ] + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws NoSuchPaddingException the no such padding exception + * @throws InvalidKeyException the invalid key exception + * @throws BadPaddingException the bad padding exception + * @throws IllegalBlockSizeException the illegal block size exception + */ + public static byte[] aes256Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException, + NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(sha256Encryption(aesKey)); + + keyGen.init(256, secureRandom); + Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR); + SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "AES"); + cipher.init(Cipher.DECRYPT_MODE, key); + return cipher.doFinal(ciphertext); + } + + + /** + * Des decryption byte [ ]. + * + * @param ciphertext the ciphertext + * @param desKey the des key + * @return the byte [ ] + * @throws InvalidKeyException the invalid key exception + * @throws NoSuchAlgorithmException the no such algorithm exception + * @throws InvalidKeySpecException the invalid key spec exception + * @throws NoSuchPaddingException the no such padding exception + * @throws BadPaddingException the bad padding exception + * @throws IllegalBlockSizeException the illegal block size exception + */ + public static byte[] desDecryption(byte[] ciphertext, String desKey) throws InvalidKeyException, + NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, + IllegalBlockSizeException { + SecureRandom sr = new SecureRandom(); + DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes()); + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); + SecretKey secretKey = keyFactory.generateSecret(desKeySpec); + Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); + return cipher.doFinal(ciphertext); + } +} diff --git a/src/main/java/com/security/protocol/DecryptRequestProtocol.java b/src/main/java/com/security/protocol/DecryptRequestProtocol.java new file mode 100644 index 00000000..4a29e7ad --- /dev/null +++ b/src/main/java/com/security/protocol/DecryptRequestProtocol.java @@ -0,0 +1,130 @@ +package com.security.protocol; + +import com.dispose.common.ProtoCryptoType; +import com.dispose.common.SecurityConfigValue; +import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.security.arithmetic.CryptoHelper; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpInputMessage; + +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +/** + * The type Decrypt request protocol. + * + * @author + */ +@Slf4j +public class DecryptRequestProtocol implements HttpInputMessage { + + /** + * The Object mapper. + */ + private final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * The Input message. + */ + private final HttpInputMessage inputMessage; + + /** + * Instantiates a new Decrypt request protocol. + * + * @param inputMessage the input message + */ + @Contract(pure = true) + public DecryptRequestProtocol(HttpInputMessage inputMessage) { + this.inputMessage = inputMessage; + } + + /** + * Gets body. + * + * @return the body + * @throws IOException the io exception + */ + @Override + @NotNull + public InputStream getBody() throws IOException { + // 解密后的内容 + byte[] decryptContent; + + // 提取协议中的JSON字符串 + String reqMessage = IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8); + + JsonNode objRoot = objectMapper.readTree(reqMessage); + + int cryptoType = objRoot.path("cryptoType").asInt(); + + // 协议未加密 + if (cryptoType == ProtoCryptoType.CRYPTO_NONE.getCode()) { + return IOUtils.toInputStream(reqMessage, StandardCharsets.UTF_8); + } + + // 反序列化为对象处理 + ProtocolReqDTO proReq = objectMapper.readValue(reqMessage, + new TypeReference>() { + }); + + // 首先对加密内容进行base64解码 + byte[] base64Decode = CryptoHelper.base64Decryption(proReq.getMsgContent()); + + // 加密类型为base64直接返回处理结果 + if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_BASE64.getCode()) { + decryptContent = base64Decode; + } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) { + try { + decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY); + } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException + | IllegalBlockSizeException e) { + log.error("AES256 decode message error: {}", base64Decode); + decryptContent = base64Decode; + } + } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) { + try { + decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY); + } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException + | BadPaddingException | IllegalBlockSizeException e) { + log.error("DES256 decode message error: {}", base64Decode); + decryptContent = base64Decode; + } + } else { + log.error("Unknown protocol security type: {}, {}", proReq.getCryptoType(), inputMessage.getBody()); + return inputMessage.getBody(); + } + + // 字节数组转换为字符串 + String decodeMsg = new String(decryptContent, StandardCharsets.UTF_8); + + String decodeJson = reqMessage.replace("\"" + proReq.getMsgContent() + "\"", decodeMsg); + + // 返回解密后的内容 + return IOUtils.toInputStream(decodeJson, StandardCharsets.UTF_8); + } + + /** + * Gets headers. + * + * @return the headers + */ + @Override + @NotNull + public HttpHeaders getHeaders() { + return inputMessage.getHeaders(); + } +} diff --git a/src/main/resources/mappers/DeviceTask.xml b/src/main/resources/mappers/DeviceTask.xml index 1f45f621..d4dd4c40 100644 --- a/src/main/resources/mappers/DeviceTask.xml +++ b/src/main/resources/mappers/DeviceTask.xml @@ -69,12 +69,8 @@ diff --git a/src/test/java/com/dispose/test/Global/InitTestEnvironment.java b/src/test/java/com/dispose/test/Global/InitTestEnvironment.java index aa440aa4..9e86b76a 100644 --- a/src/test/java/com/dispose/test/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/test/Global/InitTestEnvironment.java @@ -23,7 +23,7 @@ import javax.annotation.Resource; */ @Slf4j @Getter -@ActiveProfiles("local") +@ActiveProfiles("local,dispose") public class InitTestEnvironment { /** * The constant HTTP_CONNECT_TIMEOUT. diff --git a/src/test/java/com/dispose/test/debug/demo.java b/src/test/java/com/dispose/test/debug/demo.java index ee4dc293..9e104bb2 100644 --- a/src/test/java/com/dispose/test/debug/demo.java +++ b/src/test/java/com/dispose/test/debug/demo.java @@ -5,14 +5,26 @@ import com.dispose.common.DisposeDeviceType; import com.dispose.common.HttpType; import com.dispose.common.ObjectStatus; import com.dispose.common.PrivacyHelper; +import com.dispose.common.SecurityConfigValue; import com.dispose.pojo.entity.DisposeDevice; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.security.arithmetic.CryptoHelper; import lombok.extern.slf4j.Slf4j; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import javax.crypto.BadPaddingException; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Optional; @@ -23,6 +35,8 @@ import java.util.Optional; * @author */ @Slf4j +@RunWith(SpringRunner.class) +@SpringBootTest public class demo { // /** // * A 1 idid array req test. @@ -177,4 +191,45 @@ public class demo { // } } + @Test + public void aes256Encrypt() throws UnsupportedEncodingException { + byte[] aesMsg; + byte[] deAesMsg; + String srcMsg = "{\n" + + " \"password\": \"c3855e6b6bb120450f160ba91134522868f89d36062f2061ebeefd80817e1d58\",\n" + + " \"userName\": \"admin\"\n" + + "}"; + + try { + aesMsg = CryptoHelper.aes256Encryption(srcMsg.getBytes(StandardCharsets.UTF_8), SecurityConfigValue.AES_KEY); + deAesMsg = CryptoHelper.aes256Decryption(aesMsg, SecurityConfigValue.AES_KEY); + } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) { + e.printStackTrace(); + aesMsg = new byte[] {0}; + deAesMsg = new byte[] {0}; + } + + log.info("src: {}", srcMsg); + log.info("src AES256: {}", CryptoHelper.base64Encryption(aesMsg)); + + log.info("src DeAES256: {}", new String(deAesMsg, StandardCharsets.UTF_8)); + } + + @Test + public void aes256Decrypt() throws UnsupportedEncodingException { + byte[] deAesMsg; + String aesBase64Msg = "AiBxQak+lHi3NtmRbLhM4JLW7LTddVDGMUzcutt+Ijx2wqUWKnMVlzeqLgVSdqekQFY1gA6Fg7n16IoQmlNvsMOBilFfyiWw9Noyk21fUt57RyRADsf/ABRzzbdcqz1+Cp0/zO73gssm5+xZpwZpAg=="; + byte[] base64Decode = CryptoHelper.base64Decryption(aesBase64Msg); + + try { + deAesMsg = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY); + } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | InvalidKeyException | IllegalBlockSizeException e) { + e.printStackTrace(); + deAesMsg = new byte[] {0}; + } + + log.info("src: {}", aesBase64Msg); + log.info("src DeAES256: {}", new String(deAesMsg, StandardCharsets.UTF_8)); + } + } diff --git a/src/test/java/com/dispose/test/mapper/DeviceTaskMapperTest.java b/src/test/java/com/dispose/test/mapper/DeviceTaskMapperTest.java index 09588c1e..a35b8e9e 100644 --- a/src/test/java/com/dispose/test/mapper/DeviceTaskMapperTest.java +++ b/src/test/java/com/dispose/test/mapper/DeviceTaskMapperTest.java @@ -1,6 +1,6 @@ package com.dispose.test.mapper; -import com.dispose.common.*; +import com.dispose.common.DisposeTaskStatus; import com.dispose.mapper.DeviceTaskMapper; import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.mapper.DisposeTaskMapper; @@ -191,7 +191,7 @@ public class DeviceTaskMapperTest { } deviceTaskList.forEach(v -> { - List detailsTask = deviceTaskMapper.getTaskByDetails(v.getTaskId(), v.getDeviceId(), v.getTaskAttackType()); + List detailsTask = deviceTaskMapper.getTaskByDetails(v.getTaskId(), v.getDeviceId()); try { log.info("detailsTask: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(detailsTask));