sunshine
2024-11-05 53bd8a8d8184b3f19695b756ee78f343cdb9b9b9
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.nova.sankuai.domain.api.rongduoduo;
 
import org.springframework.stereotype.Component;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
 
@Component
public class SecurityCore {
 
    private static SecurityCore instance = null;
 
 
    private SecurityCore() {
 
    }
 
    public static SecurityCore getInstance() {
        if (instance == null)
            instance = new SecurityCore();
        return instance;
    }
 
    /*
     * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
     */
    public static String webSafeBase64StringEncoding(byte[] sSrc,boolean padded) throws Exception {
        String encodeString= Base64.getEncoder().encodeToString(sSrc);// 此处使用BASE64做转码。
 
        //websafe base64
        encodeString=encodeString.replace("+","-");
        encodeString=encodeString.replace("/","_");
 
        //nopadding base64
        if (!padded) {
            if (encodeString.endsWith("=")) {
                encodeString = encodeString.substring(0, encodeString.length() - 1);
                if (encodeString.endsWith("=")) {
                    encodeString = encodeString.substring(0, encodeString.length() - 1);
                }
            }
        }
        return encodeString;
    }
 
    public static byte[] webSafeBase64StringDecoding(String sSrc) throws Exception {
        //websafe base64
        sSrc=sSrc.replace("-","+");
        sSrc=sSrc.replace("_","/");
 
        return Base64.getDecoder().decode(sSrc);
    }
 
    public static String base64StringEncoding(byte[] sSrc,boolean padded) throws Exception {
        String encodeString=Base64.getEncoder().encodeToString(sSrc);// 此处使用BASE64做转码。
 
        //nopadding base64
        if (!padded) {
            if (encodeString.endsWith("=")) {
                encodeString = encodeString.substring(0, encodeString.length() - 1);
                if (encodeString.endsWith("=")) {
                    encodeString = encodeString.substring(0, encodeString.length() - 1);
                }
            }
        }
        return encodeString;
    }
 
    public static byte[] base64StringDecoding(String sSrc) throws Exception {
        return Base64.getDecoder().decode(sSrc);
    }
 
    public byte[] AES128CBCStringEncoding(String encData ,String secretKey,String vector) throws Exception {
        if(secretKey == null) {
            return null;
        }
        if(secretKey.length() != 16) {
            return null;
        }
        if (vector != null && vector.length() != 16) {
            return null;
        }
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] raw = secretKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
 
        return encrypted;
    }
 
    public String AES128CBCStringDecoding(byte[] sSrc,String key,String ivs){
        try {
            if(key == null) {
                return null;
            }
            if(key.length() != 16) {
                return null;
            }
            if (ivs != null && ivs.length() != 16) {
                return null;
            }
            byte[] raw = key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(sSrc);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            return null;
        }
    }
 
 
    // 加密
    public String encrypt(String sSrc){
        try {
            String encodeString=base64StringEncoding(AES128CBCStringEncoding(sSrc,"dxjf18129979469s","dxjf18129979469s"),false);
 
            return encodeString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
 
    // 解密
    public String decrypt(String sSrc){
        try {
            String decodeString=AES128CBCStringDecoding(base64StringDecoding(sSrc),"dxjf18129979469s","dxjf18129979469s");
            return decodeString;
        } catch (Exception ex) {
            return null;
        }
    }
}