xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
package com.monkeylessey.framework.service.cipher;
 
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
 
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.function.Supplier;
 
@Configuration
@ConfigurationProperties(prefix = "cipher")
public abstract class DefaultCipherService implements CipherService,InitializingBean {
 
    /**
     * 用于生成密钥的字符串
     */
    private String key;
 
    /**
     * 随机增强字符串
     */
    private String iv;
 
    @Override
    public String decode(String cipherText) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        return decodeImpl(cipherText);
    }
 
    @Override
    public String encode(String plainText) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        return encodeImpl(plainText);
    }
 
    /**
     * 解密默认实现
     * @return
     */
    abstract public String decodeImpl(String cipherText) throws NoSuchPaddingException,
                                                                NoSuchAlgorithmException,
                                                                InvalidAlgorithmParameterException,
                                                                InvalidKeyException,
                                                                IllegalBlockSizeException,
                                                                BadPaddingException;
 
    /**
     * 加密默认实现
     * @return
     */
    abstract public String encodeImpl(String plainText) throws NoSuchPaddingException,
                                                                NoSuchAlgorithmException,
                                                                InvalidAlgorithmParameterException,
                                                                InvalidKeyException,
                                                                IllegalBlockSizeException,
                                                                BadPaddingException;
 
    @Override
    public void afterPropertiesSet() {
        this.checkProperties(this::getKey, "请配置key");
        this.checkProperties(this::getIv, "请配置iv");
    }
 
    /**
     * 检查读取的配置
     *
     * @param getter
     * @param errMsg
     */
    public void checkProperties(Supplier<String> getter, String errMsg) {
        if (StringUtils.isEmpty(getter.get())) {
            throw new IllegalArgumentException(errMsg);
        }
    }
 
    public String getKey() {
        return key;
    }
 
    public void setKey(String key) {
        this.key = key;
    }
 
    public String getIv() {
        return iv;
    }
 
    public void setIv(String iv) {
        this.iv = iv;
    }
}