同步回家做家庭作业

This commit is contained in:
Cheney
2024-11-19 20:34:38 +08:00
parent 385a2a6ecc
commit e16420c2c5
19 changed files with 507 additions and 55 deletions
+8
View File
@@ -20,6 +20,14 @@
<dependencies>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.33</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@@ -2,6 +2,7 @@ package com.sunyard.chsm.model.dto;
import com.sunyard.chsm.model.PageQuery;
import com.sunyard.chsm.model.Subject;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -20,8 +21,10 @@ public abstract class KeyInfoDTO {
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
public static class Query extends PageQuery {
private Long appId;
private Long keyId;
private String status;
private String keyType;
}
@@ -44,6 +47,10 @@ public abstract class KeyInfoDTO {
private Integer genNumber;
}
@Data
public static class KeyView {
private Long id;
@@ -80,6 +87,13 @@ public abstract class KeyInfoDTO {
private LocalDateTime createTime;
}
@Data
@EqualsAndHashCode(callSuper = true)
public static class KeyValue extends KeyView {
private String keyData;
}
@Data
public static class IDs {
/**
@@ -16,7 +16,7 @@ import static com.sunyard.chsm.utils.gm.BCSM4Utils.DEFAULT_KEY_SIZE;
*/
public abstract class AbstractSdfApiService implements SdfApiService {
private static final Map<KeyAlg, Integer> algKeyLen = new HashMap<>();
protected static final Map<KeyAlg, Integer> algKeyLen = new HashMap<>();
static {
algKeyLen.put( KeyAlg.SM4, DEFAULT_KEY_SIZE );
}
@@ -42,9 +42,13 @@ public abstract class AbstractSdfApiService implements SdfApiService {
*/
@Override
public byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data){
return symEncrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
return symEncrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, null, data );
}
@Override
public byte[] symEncrypt(KeyAlg alg, AlgMode mode, byte[] key, byte[] iv, byte[] data) {
return symEncrypt( alg, mode, Padding.PCKS5Padding, key, iv, data );
}
/**
* 对称解密
@@ -55,7 +59,11 @@ public abstract class AbstractSdfApiService implements SdfApiService {
*/
@Override
public byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data) {
return symDecrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key, data );
return symDecrypt( alg, AlgMode.ECB, Padding.PCKS5Padding, key,null, data );
}
@Override
public byte[] symDecrypt(KeyAlg alg, AlgMode mode, byte[] key, byte[] iv, byte[] data) {
return symDecrypt( alg, mode, Padding.PCKS5Padding, key, iv, data );
}
}
@@ -81,15 +81,16 @@ public class BCSdfApiService extends AbstractSdfApiService {
}
}
@Override
public byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data) {
return symCalc(Cipher.ENCRYPT_MODE, alg, mode, padding, key, data);
public byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] iv, byte[] data) {
return symCalc(Cipher.ENCRYPT_MODE, alg, mode, padding, key, iv, data);
}
@Override
public byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data) {
return symCalc(Cipher.DECRYPT_MODE, alg, mode, padding, key, data);
public byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] iv, byte[] data) {
return symCalc(Cipher.DECRYPT_MODE, alg, mode, padding, key, iv, data);
}
@@ -100,7 +101,8 @@ public class BCSdfApiService extends AbstractSdfApiService {
* @param key 密钥值,明文
* @param data 加密时明文数据,解密时为密文数据
*/
private byte[] symCalc(int cipherMode, KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data) {
private byte[] symCalc(int cipherMode, KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] iv, byte[] data) {
// 只支持对称算法
if (alg.getCategory() != KeyCategory.SYM_KEY) {
throw new IllegalArgumentException("Must SYM_KEY, unsupported algorithm: " + alg);
}
@@ -122,13 +124,27 @@ public class BCSdfApiService extends AbstractSdfApiService {
Cipher cipher = null;
try {
cipher = BCSM4Utils.generateECBCipher(algName, cipherMode, key);
if ( mode == AlgMode.ECB ) {
cipher = BCSM4Utils.generateECBCipher(algName, cipherMode, key);
} else {
// iv 检查
if ( null == iv ) {
throw new IllegalArgumentException("Current Mode "+ mode.getCode() + " Unsupported IV is null");
}
if ( iv.length * 8 != algKeyLen.get(alg) ) {
throw new IllegalArgumentException("Current Alg "+ alg.getCode() + " Unsupported IV length: " + iv.length);
}
cipher = BCSM4Utils.generateCipher(algName, cipherMode, key, iv);
}
return cipher.doFinal(data);
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException e) {
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchProviderException |
NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new RuntimeException("算法执行错误", e);
}
}
@SneakyThrows
@Override
public EccKey genKeyPairEcc() {
@@ -43,9 +43,11 @@ public interface SdfApiService {
* @param key 密钥值,明文
* @param data 原始数据
*/
byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] iv, byte[] data);
byte[] symEncrypt(KeyAlg alg, AlgMode mode, byte[] key, byte[] iv, byte[] data);
byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data);
/**
* 对称解密
* @param alg 算法,只支持对称算法
@@ -54,7 +56,8 @@ public interface SdfApiService {
* @param padding 填充模式
* @param data 密文数据
*/
byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data);
byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] iv, byte[] data);
byte[] symDecrypt(KeyAlg alg, AlgMode mode, byte[] key, byte[] iv, byte[] data);
byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data);
@@ -12,6 +12,13 @@ import java.util.List;
*/
public interface KeyInfoService {
/**
* 查询单个密钥
* @param query
* @return
*/
KeyInfoDTO.KeyValue selectKeyInApp(KeyInfoDTO.Query query);
Page<KeyInfoDTO.KeyView> selectPageList(KeyInfoDTO.Query query);
Long save(KeyInfoDTO.KeySave save);
@@ -88,6 +88,16 @@ public class KeyInfoServiceImpl implements KeyInfoService {
private SdfApiService sdfApiService;
@Override
public KeyInfoDTO.KeyValue selectKeyInApp(KeyInfoDTO.Query query) {
LocalDateTime now = LocalDateTime.now();
return null;
}
@Override
public Page<KeyInfoDTO.KeyView> selectPageList(KeyInfoDTO.Query query) {
LocalDateTime now = LocalDateTime.now();
@@ -80,7 +80,7 @@ public class BCSM4Utils extends GMBaseUtil {
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
InvalidAlgorithmParameterException {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.ENCRYPT_MODE, key, iv);
Cipher cipher = generateCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.ENCRYPT_MODE, key, iv);
return cipher.doFinal(data);
}
@@ -88,7 +88,7 @@ public class BCSM4Utils extends GMBaseUtil {
throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.DECRYPT_MODE, key, iv);
Cipher cipher = generateCipher(ALGORITHM_NAME_CBC_PADDING, Cipher.DECRYPT_MODE, key, iv);
return cipher.doFinal(cipherText);
}
@@ -96,7 +96,7 @@ public class BCSM4Utils extends GMBaseUtil {
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
InvalidAlgorithmParameterException {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_NOPADDING, Cipher.ENCRYPT_MODE, key, iv);
Cipher cipher = generateCipher(ALGORITHM_NAME_CBC_NOPADDING, Cipher.ENCRYPT_MODE, key, iv);
return cipher.doFinal(data);
}
@@ -104,7 +104,7 @@ public class BCSM4Utils extends GMBaseUtil {
throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException,
InvalidAlgorithmParameterException {
Cipher cipher = generateCBCCipher(ALGORITHM_NAME_CBC_NOPADDING, Cipher.DECRYPT_MODE, key, iv);
Cipher cipher = generateCipher(ALGORITHM_NAME_CBC_NOPADDING, Cipher.DECRYPT_MODE, key, iv);
return cipher.doFinal(cipherText);
}
@@ -179,7 +179,7 @@ public class BCSM4Utils extends GMBaseUtil {
return cipher;
}
public static Cipher generateCBCCipher(String algorithmName, int mode, byte[] key, byte[] iv)
public static Cipher generateCipher(String algorithmName, int mode, byte[] key, byte[] iv)
throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);