Java最简单的DES加密算法实现案例

时间:2022-02-28 18:38:08

Base64.java

?
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
package com.mstf.des;
 
import java.io.UnsupportedEncodingException;
 
/**
 * base64编码/解码
 * @author ceet
 *
 */
public class Base64 {
 
  public static String encode(String data) {
    return new String(encode(data.getBytes()));
  }
 
  public static String decode(String data) {
    try {
      return new String(decode(data.toCharArray()),"utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      return null;
    }
  }
 
  private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      .toCharArray();
 
  private static byte[] codes = new byte[256];
 
  static {
    for (int i = 0; i < 256; i++) {
      codes[i] = -1;
    }
    for (int i = 'A'; i <= 'Z'; i++) {
      codes[i] = (byte) (i - 'A');
    }
 
    for (int i = 'a'; i <= 'z'; i++) {
      codes[i] = (byte) (26 + i - 'a');
    }
    for (int i = '0'; i <= '9'; i++) {
      codes[i] = (byte) (52 + i - '0');
    }
    codes['+'] = 62;
    codes['/'] = 63;
  }
 
  public static char[] encode(byte[] data) {
    char[] out = new char[((data.length + 2) / 3) * 4];
    for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
      boolean quad = false;
      boolean trip = false;
 
      int val = (0xFF & (int) data[i]);
      val <<= 8;
      if ((i + 1) < data.length) {
        val |= (0xFF & (int) data[i + 1]);
        trip = true;
      }
      val <<= 8;
      if ((i + 2) < data.length) {
        val |= (0xFF & (int) data[i + 2]);
        quad = true;
      }
      out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
      val >>= 6;
      out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
      val >>= 6;
      out[index + 1] = alphabet[val & 0x3F];
      val >>= 6;
      out[index + 0] = alphabet[val & 0x3F];
    }
    return out;
  }
 
  public static byte[] decode(char[] data) {
    int tempLen = data.length;
    for (int ix = 0; ix < data.length; ix++) {
      if ((data[ix] > 255) || codes[data[ix]] < 0) {
        --tempLen;
      }
    }
    int len = (tempLen / 4) * 3;
    if ((tempLen % 4) == 3) {
      len += 2;
    }
    if ((tempLen % 4) == 2) {
      len += 1;
 
    }
    byte[] out = new byte[len];
 
    int shift = 0;
    int accum = 0;
    int index = 0;
 
    for (int ix = 0; ix < data.length; ix++) {
      int value = (data[ix] > 255) ? -1 : codes[data[ix]];
 
      if (value >= 0) {
        accum <<= 6;
        shift += 6;
        accum |= value;
        if (shift >= 8) {
          shift -= 8;
          out[index++] = (byte) ((accum >> shift) & 0xff);
        }
      }
    }
 
    if (index != out.length) {
      throw new Error("Miscalculated data length (wrote " + index
          + " instead of " + out.length + ")");
    }
 
    return out;
  }
}

DESUtil.java

?
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
package com.mstf.des;
 
import java.security.Key;
import java.security.SecureRandom;
 
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
 
/**
 * DES对称算法(加密/解密)
 *
 * @author ceet
 *
 */
public class DESUtil {
 
  private Key key;
 
  public DESUtil(String strKey) {
    setKey(strKey);
  }
 
  public void setKey(String strKey) {
    try {
      KeyGenerator generator = KeyGenerator.getInstance("DES");
      generator.init(new SecureRandom(strKey.getBytes())); // 根据参数生成key
      this.key = generator.generateKey();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  public String encrypt(String source) {
    return encrypt(source, "utf-8");
  }
 
  public String decrypt(String encryptedData) {
    return decrypt(encryptedData, "utf-8");
  }
 
  public String encrypt(String source, String charSet) {
    String encrypt = null;
    try {
      byte[] ret = encrypt(source.getBytes(charSet));
      encrypt = new String(Base64.encode(ret));
    } catch (Exception e) {
      e.printStackTrace();
      encrypt = null;
    }
    return encrypt;
  }
 
  public String decrypt(String encryptedData, String charSet) {
    String descryptedData = null;
    try {
      byte[] ret = descrypt(Base64.decode(encryptedData.toCharArray()));
      descryptedData = new String(ret, charSet);
    } catch (Exception e) {
      e.printStackTrace();
      descryptedData = null;
    }
    return descryptedData;
  }
 
  private byte[] encrypt(byte[] primaryData) {
    try {
      Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成加密操作
      cipher.init(Cipher.ENCRYPT_MODE, this.key); // 用密钥初始化Cipher对象(加密)
 
      return cipher.doFinal(primaryData);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  private byte[] descrypt(byte[] encryptedData) {
    try {
      Cipher cipher = Cipher.getInstance("DES"); // Cipher对象实际完成解密操作
      cipher.init(Cipher.DECRYPT_MODE, this.key); // 用密钥初始化Cipher对象(解密)
 
      return cipher.doFinal(encryptedData);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  public static void main(String[] args) {
    String code = "ceet";
    DESUtil desUtil = new DESUtil("key");
    String encrypt = desUtil.encrypt(code);
    String decrypt = desUtil.decrypt(encrypt);
    System.out.println("原内容:" + code);
    System.out.println("加密:" + encrypt);
    System.out.println("解密:" + decrypt);
  }
}

以上这篇Java最简单的DES加密算法实现案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。