admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
package com.lcjian.library.util.security;
 
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
 
import android.util.Base64;
 
public class DEScrypt {
    /**
     * [加密]DES-->BASE64-->密文
     * <p>
     * [解密]BASE64->DES-->明文
     * <p>
     * 秘钥和向量要双方约定一致
     * <p>
     * DES加密的私钥,必须是8位长的字符串
     */
    private static final byte[] DESkey = Base64.decode("VW1nT3R2WVk=", Base64.DEFAULT);// 设置密钥
    private static final byte[] DESIV = Base64.decode("WXlIeEhsY0o=", Base64.DEFAULT);// 设置向量
    private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
    private static Key key = null;
 
    static {
        try {
            DESKeySpec keySpec = new DESKeySpec(DESkey);
            // 设置密钥参数
            iv = new IvParameterSpec(DESIV);// 设置向量
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
            key = keyFactory.generateSecret(keySpec);// 得到密钥对象
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
 
    public static String encode(String data) {
        String result = "";
        try {
            Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
            enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
            byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
            result = Base64.encodeToString(pasByte, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static String decode(String data) {
        String result = "";
        if (data == null)
            return result;
        Cipher deCipher;
        try {
            deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            deCipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] pasByte = deCipher.doFinal(Base64.decode(data, Base64.DEFAULT));
            result = new String(pasByte, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}