lmw
2023-04-03 16ea883d3c03fd8b910f9282aa1bc08378d40d54
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.zhaoyang.driver.utils;
import android.util.Log;
 
import com.zhaoyang.driver.netUtls.NetKitKt;
 
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import cn.sinata.util.Base64DES;
 
public class DES {
    
    /*
     * 不要写成:Cipher cipher = Cipher.getInstance("DES");
     * 或:Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
     * 原因是Cipher cipher = Cipher.getInstance("DES");
     * 与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
     * 等同,填充方式错误,加密的时候会得到16长度的字节数组。
     * 应该写成
     * Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
     */
 
 
    
    private static final String KEY = "ForUApp=";
 
    private static byte[] iv = {25, 24, 40, 41, 60, 68, 70, 97};
 
    
    /**
     * 指定密钥加密
     * @param encryptString        原文
     * @param encryptKey        密钥
     * @return                    密文
     * @throws Exception
     */
    public static String encryptDES(String encryptString, String encryptKey)
            throws Exception {
 
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
 
        SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
 
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
 
        return Base64DES.encode(encryptedData);
    }
    
    /**
     * 指定密钥进行解密
     * @param decryptString        密文
     * @param decryptKey        密钥
     * @return                    原文
     * @throws Exception
     */
    public static String decryptDES(String decryptString, String decryptKey)
            throws Exception {
 
        byte[] byteMi = Base64DES.decode(decryptString);
 
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
 
        SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
 
        byte decryptedData[] = cipher.doFinal(byteMi);
 
        return new String(decryptedData);
 
    }
    
    /**
     * 默认密钥加密
     * @param encryptString        原文
     * @return                    密文
     * @throws Exception
     */
    public static String encryptDES(String encryptString)
            throws Exception {
 
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
 
        SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "DES");
 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
 
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
 
        return Base64DES.encode(encryptedData);
    }
    
    /**
     * 默认密钥解密
     * @param decryptString        密文
     * @return                    解密得到的原文
     * @throws Exception
     */
    public static String decryptDES(String decryptString)
            throws Exception {
 
        byte[] byteMi = Base64DES.decode(decryptString);
 
        IvParameterSpec zeroIv = new IvParameterSpec(iv);
 
        SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), "DES");
 
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
        cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
 
        byte decryptedData[] = cipher.doFinal(byteMi);
 
        return new String(decryptedData, "UTF-8");
 
    }
    
    public static String parseByte2HexStr(byte buf[]) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < buf.length; i++) {  
            String hex = Integer.toHexString(buf[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            sb.append(hex.toUpperCase());  
        }  
        return sb.toString();  
    }
 
    public static void main(String[] args) {
        String plaintext = "server=/app/public/getCostSet?type=1";
        try {
            System.out.println("明文:" + plaintext);
            System.out.println("密钥:" + KEY);
            System.out.println("DES密文:" + DES.encryptDES(plaintext));
            System.out.println("DES解密"+DES.decryptDES(DES.encryptDES(plaintext)));
 
            System.out.println("URLEncoder加密"+URLEncoder.encode(DES.encryptDES(plaintext),"UTF-8"));
            System.out.println("URLEncoder解密"+URLDecoder.decode(URLEncoder.encode(DES.encryptDES(plaintext),"UTF-8"), "UTF-8"));
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 生成签名
     * @param map
     * @return
     */
    public static String getSign(Map<String, Object> map) {
 
        String result = "";
        try {
            List<Map.Entry<String, Object>> infoIds = new ArrayList<>(map.entrySet());
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
            Collections.sort(infoIds, (o1, o2) -> (o1.getKey()).compareTo(o2.getKey()));
            // 构造签名键值对的格式
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> item : infoIds) {
                if (item.getKey() == null) {
                    item.getKey();
                }
                String key = item.getKey();
                Object val = item.getValue();
                if (!(val == "" || val == null)) {
                    sb.append(key).append("=").append(val).append("&");
                }
            }
            result = sb.toString();
            result = result.substring(0,result.length() - 1);
            Log.i("getSign====", "getSign: ==="+result);
            Log.i("getSign====", "AppId: ==="+ NetKitKt.getAppId());
            result = Base64DES.encode(HMACSHA1.HmacSHA1Encrypt(result, NetKitKt.getAppId()));
            //进行MD5加密
        } catch (Exception e) {
            return null;
        }
        return result;
    }
}