mitao
2024-09-07 2862c3e4da3adbb4bea43151514f0c43b86476d6
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
package com.xinquan.common.core.utils;
 
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
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 = "hfh7L4nUzKcnK6JtJ5z27ygad5p9EanZn5wCT6Zw9KvctK5hs7Z8FsjDEpShmC5W";
 
    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/getAllRegionList";
        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"));
            System.out.println("长度:"
                    + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".length());
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}