From 20424c9fc915fe581a530e9f9f8a09c850d007f1 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 15 Sep 2020 14:25:57 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E5=88=A0=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81=202.=20=E5=A2=9E=E5=8A=A0Mock?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=8D=8F=E8=AE=AE=E8=BF=94=E5=9B=9E=E6=97=B6?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E6=A0=87=E8=AE=B0=203.=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3DES=E7=AE=97=E6=B3=95=E5=BC=82=E5=B8=B8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dispose/common/DisposeConfigValue.java | 2 + .../interceptor/ResponseProtocolSecurity.java | 6 +++ .../dto/protocol/base/BaseRespStatus.java | 12 ------ .../com/security/arithmetic/CryptoHelper.java | 38 +++++++++---------- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/dispose/common/DisposeConfigValue.java b/src/main/java/com/dispose/common/DisposeConfigValue.java index d9054ba8..797f4d09 100644 --- a/src/main/java/com/dispose/common/DisposeConfigValue.java +++ b/src/main/java/com/dispose/common/DisposeConfigValue.java @@ -46,4 +46,6 @@ public class DisposeConfigValue { * The constant MIN_SPLIT_PAGE_SIZE. */ public static volatile int MIN_SPLIT_PAGE_SIZE = 10; + + public static volatile boolean ENABLE_UTEST_MOCK = false; } diff --git a/src/main/java/com/dispose/interceptor/ResponseProtocolSecurity.java b/src/main/java/com/dispose/interceptor/ResponseProtocolSecurity.java index 37623d43..13256948 100644 --- a/src/main/java/com/dispose/interceptor/ResponseProtocolSecurity.java +++ b/src/main/java/com/dispose/interceptor/ResponseProtocolSecurity.java @@ -1,5 +1,6 @@ package com.dispose.interceptor; +import com.dispose.common.DisposeConfigValue; import com.dispose.common.ProtoCryptoType; import com.dispose.common.SecurityConfigValue; import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO; @@ -42,6 +43,11 @@ public class ResponseProtocolSecurity implements ResponseBodyAdvice { @Override public boolean supports(@NotNull MethodParameter methodParameter, @NotNull Class> aClass) { + // 单元测试Mock模式 + if (DisposeConfigValue.ENABLE_UTEST_MOCK) { + return true; + } + return methodParameter.getContainingClass().isAnnotationPresent(Encryption.class) || methodParameter.hasMethodAnnotation(Encryption.class); } diff --git a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseRespStatus.java b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseRespStatus.java index ac1d465b..9a0289e5 100644 --- a/src/main/java/com/dispose/pojo/dto/protocol/base/BaseRespStatus.java +++ b/src/main/java/com/dispose/pojo/dto/protocol/base/BaseRespStatus.java @@ -4,8 +4,6 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import java.util.Arrays; - /** * The type Response status. * @@ -24,14 +22,4 @@ public class BaseRespStatus { * 登录消息: status状态码对应的提示信息 */ private String[] message; - - /** - * To string string. - * - * @return the string - */ - @Override - public String toString() { - return "{\"status\":" + status + ", \"message\":\"" + Arrays.toString(message) + "\"}"; - } } diff --git a/src/main/java/com/security/arithmetic/CryptoHelper.java b/src/main/java/com/security/arithmetic/CryptoHelper.java index 0582233c..de8ae56d 100644 --- a/src/main/java/com/security/arithmetic/CryptoHelper.java +++ b/src/main/java/com/security/arithmetic/CryptoHelper.java @@ -8,16 +8,12 @@ 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. @@ -71,7 +67,7 @@ public class CryptoHelper { } /** - * Aes 256 encryption byte [ ]. + * Aes 128 encryption byte [ ]. * * @param plaintext the plaintext * @param aesKey the aes key @@ -96,7 +92,7 @@ public class CryptoHelper { } /** - * Aes 256 decryption byte [ ]. + * Aes 128 decryption byte [ ]. * * @param ciphertext the ciphertext * @param aesKey the aes key @@ -129,20 +125,21 @@ public class CryptoHelper { * @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, + NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { - SecureRandom sr = new SecureRandom(); - DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes()); - SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); - SecretKey secretKey = keyFactory.generateSecret(desKeySpec); + KeyGenerator keyGen = KeyGenerator.getInstance("DES"); + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(desKey.getBytes()); + + keyGen.init(56, secureRandom); Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR); - cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr); + SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "DES"); + cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(ciphertext); } @@ -154,20 +151,21 @@ public class CryptoHelper { * @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[] desEncryption(byte[] plaintext, String desKey) throws InvalidKeyException, - NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, + NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { - SecureRandom sr = new SecureRandom(); - DESKeySpec desKeySpec = new DESKeySpec(desKey.getBytes()); - SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); - SecretKey secretKey = keyFactory.generateSecret(desKeySpec); + KeyGenerator keyGen = KeyGenerator.getInstance("DES"); + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(desKey.getBytes()); + + keyGen.init(56, secureRandom); Cipher cipher = Cipher.getInstance(DES_ALGORITHM_STR); - cipher.init(Cipher.DECRYPT_MODE, secretKey, sr); + SecretKeySpec key = new SecretKeySpec(keyGen.generateKey().getEncoded(), "DES"); + cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(plaintext); } }