DES 加解密

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
package com.bjtcrj.gms.core.utils;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class DesUtil {
/** 对称加解密DES密钥Key*/

private static Cipher mEncryptCipher = null;
private static Cipher mDecryptCipher = null;

static {
try {
mEncryptCipher = Cipher.getInstance("DES");
mDecryptCipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}

private DesUtil() {
}

// ****** 加密 ******
/**
* 对 字符串 加密
* */
public static String encrypt(String strIn, String key) throws Exception {
return byte2HexStr(encrypt(strIn.getBytes(), key));
}

/**
* 对 字节数组 加密
*/
private static byte[] encrypt(byte[] arrB, String key) throws Exception {
mEncryptCipher.init(Cipher.ENCRYPT_MODE, getKey(key.getBytes()));
return mEncryptCipher.doFinal(arrB);
}


// ****** 解密 ******

/**
* 解密 字符串
* */
public static String decrypt(String strIn, String key) throws Exception {
return new String(decrypt(hexStr2Byte(strIn), key));
}

/**
* 解密 字节数组
*/
private static byte[] decrypt(byte[] arrB, String key) throws Exception {
mDecryptCipher.init(Cipher.DECRYPT_MODE,getKey(key.getBytes()));
return mDecryptCipher.doFinal(arrB);
}


/**
* 解密用的密钥(字节数组)长度必须为8个字节否则返回null, 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
* @throws Exception
*/

private static Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];

// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}

// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

return key;
}

/**
* HEX转码 String to Byte
*/
public static byte[] hexStr2Byte(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;

// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}

/**
* HEX转码 Byte to String
*/
public static String byte2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}

public static void main(String[] args) {
try {
System.out.println("加密前:");
StringBuilder stringBuilder = new StringBuilder("asdklfjalsdkfj");
// for (int i = 0; i < 1000000; i++) {
// stringBuilder.append("abc" + i);
// }
long start = System.currentTimeMillis();
String pwd = encrypt(stringBuilder.toString(), "abc");
long encend = System.currentTimeMillis();
System.out.println("加密耗时:" + (encend -start) + "ms");
System.out.println("加密后:" + pwd);
pwd = decrypt(pwd, "abc");
System.out.println("解密耗时:" + (System.currentTimeMillis() -encend) + "ms");
System.out.println("解密密后:" + pwd);

} catch (Exception e) {
e.printStackTrace();
}
}
}

DES + Base64 加解密

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
package com.bjtcrj.gms.core.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class DesUtil {
/**
* 对称加解密DES密钥Key
*/

private static Cipher mEncryptCipher = null;
private static Cipher mDecryptCipher = null;

static {
try {
mEncryptCipher = Cipher.getInstance("DES");
mDecryptCipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}

private DesUtil() {
}

// ****** 加密 ******

/**
* 对 字符串 加密
*/
public static String encrypt(String strIn, String key) throws Exception {
mEncryptCipher.init(Cipher.ENCRYPT_MODE, getKey(key.getBytes()));
return Base64.encodeBase64String(mEncryptCipher.doFinal(strIn.getBytes()));
}

/**
* 对 字节数组 加密
*/
private static byte[] encrypt(byte[] arrB, String key) throws Exception {
mEncryptCipher.init(Cipher.ENCRYPT_MODE, getKey(key.getBytes()));
return mEncryptCipher.doFinal(arrB);
}


// ****** 解密 ******

/**
* 解密 字符串
*/
public static String decrypt(String strIn, String key) throws Exception {
mDecryptCipher.init(Cipher.DECRYPT_MODE, getKey(key.getBytes()));
return new String(mDecryptCipher.doFinal(Base64.decodeBase64(strIn)));
}


/**
* 解密用的密钥(字节数组)长度必须为8个字节否则返回null, 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
* @throws Exception
*/

private static Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];

// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}

// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");

return key;
}

/**
* HEX转码 String to Byte
*/
public static byte[] hexStr2Byte(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;

// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}

/**
* HEX转码 Byte to String
*/
public static String byte2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
try {
//密钥
String key = "abc";
//要加密的数据
String data = "123456";

System.out.println("加密前:" + data);
String pwd = encrypt(data, key);
System.out.println("加密后:" + pwd);
String decrypt_data = decrypt(pwd, key);
System.out.println("解密密后:" + decrypt_data);

} catch (Exception e) {
e.printStackTrace();
}
}