puzhibing
2023-12-08 f88cfa02e36752e4acad7adc4b045155e8e50f21
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
package com.dsh.course.util.akeylogin;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
 
public class DES {
    public static final String ALGORITHM = "DES";
    public static final String PADDING = "DES/CBC/PKCS5Padding";
    private static final byte[] DEFAULT_INITIALIZATION_VECTOR = ("00000000".getBytes());
 
    private String padding = PADDING;
    private byte[] initializationVectorBytes = DEFAULT_INITIALIZATION_VECTOR;
    private AlgorithmParameterSpec paramSpec;
 
    public DES() {
        this.paramSpec = new IvParameterSpec(initializationVectorBytes);
    }
 
    public DES(String padding) {
        this.padding = padding;
        this.paramSpec = new IvParameterSpec(initializationVectorBytes);
    }
 
    public DES(String padding, byte[] initializationVectorBytes) {
        this.padding = padding;
        this.initializationVectorBytes = initializationVectorBytes;
        this.paramSpec = new IvParameterSpec(initializationVectorBytes);
    }
 
    public DES(byte[] initializationVectorBytes) {
        this.initializationVectorBytes = initializationVectorBytes;
        this.paramSpec = new IvParameterSpec(initializationVectorBytes);
    }
 
 
    public byte[] encodeBytes(byte[] data, byte[] key) throws Exception {
        Key secretKey = getKey(key);
        Cipher cipher = Cipher.getInstance(padding);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        byte[] bytes = cipher.doFinal(data);
        return bytes;
    }
 
    public byte[] decodeBytes(byte[] data, byte[] key) throws Exception {
        Key secretKey = getKey(key);
        Cipher cipher = Cipher.getInstance(padding);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        return cipher.doFinal(data);
 
    }
 
    public String getPadding() {
        return padding;
    }
 
    public byte[] getInitializationVectorBytes() {
        return initializationVectorBytes;
    }
 
    private static Key getKey(byte[] key) throws Exception {
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey secretKey = keyFactory.generateSecret(dks);
        return secretKey;
    }
 
    public static byte[] decode(byte[] data, byte[] key) throws Exception {
        return decode(data, key, DEFAULT_INITIALIZATION_VECTOR);
    }
 
 
    public static byte[] encode(byte[] data, byte[] key) throws Exception {
        return encode(data, key, DEFAULT_INITIALIZATION_VECTOR);
    }
 
    public static byte[] decode(byte[] data, byte[] key, byte[] ivBytes) throws Exception {
        DES des = new DES(PADDING, ivBytes);
        return des.decodeBytes(data, key);
    }
 
    public static byte[] encode(byte[] data, byte[] key, byte[] ivBytes) throws Exception {
        DES des = new DES(PADDING, ivBytes);
        return des.encodeBytes(data, key);
    }
}