java加密工具类

java加密工具类

包含AES、DES、Base32、Base64

AES代码

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
package com.common.utils.encrypt;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES对称加密和解密
* @auth wangming
*/
public class AESUtil {
public static String encodeRules = "123456789";
/**
* 加密
* 1.构造密钥生成器
* 2.根据ecnodeRules规则初始化密钥生成器
* 3.产生密钥
* 4.创建和初始化密码器
* 5.内容加密
* 6.返回字符串
*/
public static String AESEncode(String encodeRules,String content){
try {
//1.构造密钥生成器,指定为AES算法,不区分大小写
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//2.根据ecnodeRules规则初始化密钥生成器
//生成一个128位的随机源,根据传入的字节数组
keygen.init(128, new SecureRandom(encodeRules.getBytes()));
//3.产生原始对称密钥
SecretKey original_key = keygen.generateKey();
//4.获得原始对称密钥的字节数组
byte [] raw = original_key.getEncoded();
//5.根据字节数组生成AES密钥
SecretKey key = new SecretKeySpec(raw, "AES");
//6.根据指定算法AES自成密码器
Cipher cipher = Cipher.getInstance("AES");
//7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
cipher.init(Cipher.ENCRYPT_MODE, key);
//8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
byte [] byte_encode = content.getBytes("utf-8");
//9.根据密码器的初始化方式--加密:将数据加密
byte [] byte_AES = cipher.doFinal(byte_encode);
//10.将加密后的数据转换为字符串
//这里用Base64Encoder中会找不到包
//解决办法:
//在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
String AES_encode = new String(new BASE64Encoder().encode(byte_AES));
//11.将字符串返回
return AES_encode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

//如果有错就返加nulll
return null;
}
/**
* 解密
* 解密过程:
* 1.同加密1-4步
* 2.将加密后的字符串反纺成byte[]数组
* 3.将加密内容解密
*/
public static String AESDncode(String encodeRules,String content){
try {
//1.构造密钥生成器,指定为AES算法,不区分大小写
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//2.根据ecnodeRules规则初始化密钥生成器
//生成一个128位的随机源,根据传入的字节数组
keygen.init(128, new SecureRandom(encodeRules.getBytes()));
//3.产生原始对称密钥
SecretKey original_key = keygen.generateKey();
//4.获得原始对称密钥的字节数组
byte [] raw=original_key.getEncoded();
//5.根据字节数组生成AES密钥
SecretKey key = new SecretKeySpec(raw, "AES");
//6.根据指定算法AES自成密码器
Cipher cipher = Cipher.getInstance("AES");
//7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
cipher.init(Cipher.DECRYPT_MODE, key);
//8.将加密并编码后的内容解码成字节数组
byte [] byte_content = new BASE64Decoder().decodeBuffer(content);
/**
* 解密
*/
byte [] byte_decode = cipher.doFinal(byte_content);
String AES_decode = new String(byte_decode,"utf-8");
return AES_decode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

//如果有错就返加nulll
return null;
}

public static void main(String[] args) {
AESUtil se = new AESUtil();
Scanner scanner = new Scanner(System.in);
/**
* 加密
*/
System.out.println("使用AES对称加密,请输入加密的规则");
String encodeRules = scanner.next();
System.out.println("请输入要加密的内容:");
String content = scanner.next();
System.out.println("根据输入的规则"+encodeRules+"加密后的密文是:"+se.AESEncode(encodeRules, content));

/**
* 解密
*/
System.out.println("使用AES对称解密,请输入加密的规则:(须与加密相同)");
encodeRules = scanner.next();
System.out.println("请输入要解密的内容(密文):");
content = scanner.next();
System.out.println("根据输入的规则"+encodeRules+"解密后的明文是:"+se.AESDncode(encodeRules, content));
}

}

MD5代码

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
package com.common.utils.encrypt;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 根据字符串md5加密
* 字符串长度小于1024*2
* @author wangming
*
*/
public class MD5Util {
private static final int MD5_LENGTH = 1024 * 2;//1024 * 2 - 512
public static String generateMD5(String plainText) {
try {
if(null == plainText || plainText.length() == 0) {
return null;
}
plainText = (plainText.length() > MD5_LENGTH)? plainText.substring(0, MD5_LENGTH) : plainText;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = plainText.getBytes(Charset.forName("UTF-8"));
md.update(bytes);
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0, j = b.length; offset < j; offset++) {
i = b[offset];
if (i < 0) {
i += 256;
}
if (i < 16) {
buf.append("0");
}
buf.append(Integer.toHexString(i));
}
return buf.toString().toLowerCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}

Base32代码

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.common.utils.encrypt;
/**
* Base32的编码和解码
*/
public final class Base32 {
private static final String ERR_CANONICAL = "Invalid Base32 string";

private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".toCharArray();

private static final byte[] LOOKUP = {
26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, // 0123456789:;<=>?
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // @ABCDEFGHIJKLMNO
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // PQRSTUVWXYZ[\]^_
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // `abcdefghijklmno
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 // pqrstuvwxyz
};

/**
* 使用Base32进行编码
*
* @param bytes
* @return
*/
public final static String encode(final byte[] bytes) {
StringBuilder base32 = new StringBuilder((bytes.length * 8 + 4) / 5);

int currByte, digit, i = 0;
while (i < bytes.length) {

// STEP 0; insert new 5 bits, leave 3 bits
currByte = bytes[i++] & 255;
base32.append(CHARS[currByte >> 3]);
digit = (currByte & 7) << 2;
if (i >= bytes.length) {
base32.append(CHARS[digit]);
break;
}

// STEP 3: insert 2 new bits, then 5 bits, leave 1 bit
currByte = bytes[i++] & 255;
base32.append(CHARS[digit | (currByte >> 6)]);
base32.append(CHARS[(currByte >> 1) & 31]);
digit = (currByte & 1) << 4;
if (i >= bytes.length) {
base32.append(CHARS[digit]);
break;
}

// STEP 1: insert 4 new bits, leave 4 bit
currByte = bytes[i++] & 255;
base32.append(CHARS[digit | (currByte >> 4)]);
digit = (currByte & 15) << 1;
if (i >= bytes.length) {
base32.append(CHARS[digit]);
break;
}

// STEP 4: insert 1 new bit, then 5 bits, leave 2 bits
currByte = bytes[i++] & 255;
base32.append(CHARS[digit | (currByte >> 7)]);
base32.append(CHARS[(currByte >> 2) & 31]);
digit = (currByte & 3) << 3;
if (i >= bytes.length) {
base32.append(CHARS[digit]);
break;
}

// STEP 2: insert 3 new bits, then 5 bits, leave 0 bit
currByte = bytes[i++] & 255;
base32.append(CHARS[digit | (currByte >> 5)]);
base32.append(CHARS[currByte & 31]);
}
return base32.toString();
}


/**
* 用Base32进行解码
*
* @param base32
* @return
* @throws IllegalArgumentException
*/
public final static String decode(final String base32) throws IllegalArgumentException {
switch (base32.length() % 8) {
case 1:
case 3:
case 6:
throw new IllegalArgumentException(ERR_CANONICAL);
}

byte[] bytes = new byte[base32.length() * 5 / 8];
int offset = 0, i = 0, lookup;
byte nextByte, digit;

while (i < base32.length()) {
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 0: leave 5 bits
nextByte = (byte) (digit << 3);
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 5: insert 3 bits, leave 2 bits
bytes[offset++] = (byte) (nextByte | (digit >> 2));
nextByte = (byte) ((digit & 3) << 6);
if (i >= base32.length()) {
if (nextByte != (byte) 0) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
break;
}
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 2: leave 7 bits
nextByte |= (byte) (digit << 1);
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 7: insert 1 bit, leave 4 bits
bytes[offset++] = (byte) (nextByte | (digit >> 4));
nextByte = (byte) ((digit & 15) << 4);
if (i >= base32.length()) {
if (nextByte != (byte) 0) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
break;
}
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 4: insert 4 bits, leave 1 bit
bytes[offset++] = (byte) (nextByte | (digit >> 1));
nextByte = (byte) ((digit & 1) << 7);
if (i >= base32.length()) {
if (nextByte != (byte) 0) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
break;
}
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 1: leave 6 bits
nextByte |= (byte) (digit << 2);
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 6: insert 2 bits, leave 3 bits
bytes[offset++] = (byte) (nextByte | (digit >> 3));
nextByte = (byte) ((digit & 7) << 5);
if (i >= base32.length()) {
if (nextByte != (byte) 0) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
break;
}
lookup = base32.charAt(i++) - '2';
if (lookup < 0 || lookup >= LOOKUP.length) {
throw new IllegalArgumentException(ERR_CANONICAL);
}
digit = LOOKUP[lookup];
if (digit == -1) {
throw new IllegalArgumentException(ERR_CANONICAL);
}

// STEP n = 3: insert 5 bits, leave 0 bit
bytes[offset++] = (byte) (nextByte | digit);
}
return new String(bytes);
}
}

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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.common.utils.encrypt;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* Base64的编码和解码
*/
public final class Base64 {
private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
private static final int[] INV = new int[256];

static {
Arrays.fill(INV, -1);
for (int i = 0, iS = CHARS.length; i < iS; i++) {
INV[CHARS[i]] = i;
}
INV['='] = 0;
}

/**
* Encodes a raw byte array into a BASE64 <code>char[]</code>.
*
* @param lineSeparator optional CRLF after 76 chars, unless EOF.
*/
public final static char[] encodeToChar(byte[] arr, boolean lineSeparator) {
int len = arr != null ? arr.length : 0;
if (len == 0) {
return new char[0];
}

int evenlen = (len / 3) * 3;
int cnt = ((len - 1) / 3 + 1) << 2;
int destLen = cnt + (lineSeparator ? (cnt - 1) / 76 << 1 : 0);
char[] dest = new char[destLen];

for (int s = 0, d = 0, cc = 0; s < evenlen; ) {
int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);

dest[d++] = CHARS[(i >>> 18) & 0x3f];
dest[d++] = CHARS[(i >>> 12) & 0x3f];
dest[d++] = CHARS[(i >>> 6) & 0x3f];
dest[d++] = CHARS[i & 0x3f];

if (lineSeparator && (++cc == 19) && (d < (destLen - 2))) {
dest[d++] = '\r';
dest[d++] = '\n';
cc = 0;
}
}

int left = len - evenlen; // 0 - 2.
if (left > 0) {
int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);

dest[destLen - 4] = CHARS[i >> 12];
dest[destLen - 3] = CHARS[(i >>> 6) & 0x3f];
dest[destLen - 2] = left == 2 ? CHARS[i & 0x3f] : '=';
dest[destLen - 1] = '=';
}
return dest;
}

public final static byte[] encodeToByte(String s) {
try {
return encodeToByte(s.getBytes("UTF_8"), false);
} catch (UnsupportedEncodingException ignore) {
return new byte[]{};
}
}

public final static byte[] encodeToByte(String s, boolean lineSep) {
try {
return encodeToByte(s.getBytes("UTF_8"), lineSep);
} catch (UnsupportedEncodingException ignore) {
return new byte[]{};
}
}

public final static byte[] encodeToByte(byte[] arr) {
return encodeToByte(arr, false);
}

/**
* Encodes a raw byte array into a BASE64 <code>char[]</code>.
*
* @param lineSep optional CRLF after 76 chars, unless EOF.
*/
public final static byte[] encodeToByte(byte[] arr, boolean lineSep) {
int len = arr != null ? arr.length : 0;
if (len == 0) {
return new byte[0];
}

int evenlen = (len / 3) * 3;
int cnt = ((len - 1) / 3 + 1) << 2;
int destlen = cnt + (lineSep ? (cnt - 1) / 76 << 1 : 0);
byte[] dest = new byte[destlen];

for (int s = 0, d = 0, cc = 0; s < evenlen; ) {
int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);

dest[d++] = (byte) CHARS[(i >>> 18) & 0x3f];
dest[d++] = (byte) CHARS[(i >>> 12) & 0x3f];
dest[d++] = (byte) CHARS[(i >>> 6) & 0x3f];
dest[d++] = (byte) CHARS[i & 0x3f];

if (lineSep && ++cc == 19 && d < destlen - 2) {
dest[d++] = '\r';
dest[d++] = '\n';
cc = 0;
}
}

int left = len - evenlen;
if (left > 0) {
int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);

dest[destlen - 4] = (byte) CHARS[i >> 12];
dest[destlen - 3] = (byte) CHARS[(i >>> 6) & 0x3f];
dest[destlen - 2] = left == 2 ? (byte) CHARS[i & 0x3f] : (byte) '=';
dest[destlen - 1] = '=';
}
return dest;
}

public final static String decodeToString(byte[] arr) {
try {
return new String(decode(arr), "UTF_8");
} catch (UnsupportedEncodingException ignore) {
return null;
}
}

/**
* Decodes BASE64 encoded byte array.
*/
public final static byte[] decode(byte[] arr) {
int length = arr.length;
if (length == 0) {
return new byte[0];
}

int sndx = 0, endx = length - 1;
int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0;
int cnt = endx - sndx + 1;
int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0;
int len = ((cnt - sepCnt) * 6 >> 3) - pad;
byte[] dest = new byte[len];

int d = 0;
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[arr[sndx++]];

dest[d++] = (byte) (i >> 16);
dest[d++] = (byte) (i >> 8);
dest[d++] = (byte) i;

if (sepCnt > 0 && ++cc == 19) {
sndx += 2;
cc = 0;
}
}

if (d < len) {
int i = 0;
for (int j = 0; sndx <= endx - pad; j++) {
i |= INV[arr[sndx++]] << (18 - j * 6);
}
for (int r = 16; d < len; r -= 8) {
dest[d++] = (byte) (i >> r);
}
}

return dest;
}

public final static String encodeToString(String s) {
try {
return new String(encodeToChar(s.getBytes("UTF_8"), false));
} catch (UnsupportedEncodingException ignore) {
return null;
}
}

public final static String encodeToString(String s, boolean lineSep) {
try {
return new String(encodeToChar(s.getBytes("UTF_8"), lineSep));
} catch (UnsupportedEncodingException ignore) {
return null;
}
}

public final static String encodeToString(byte[] arr) {
return new String(encodeToChar(arr, false));
}

/**
* Encodes a raw byte array into a BASE64 <code>String</code>.
*/
public final static String encodeToString(byte[] arr, boolean lineSep) {
return new String(encodeToChar(arr, lineSep));
}

public final static String decodeToString(String s) {
try {
return new String(decode(s), "UTF_8");
} catch (UnsupportedEncodingException ignore) {
return null;
}
}

/**
* Decodes a BASE64 encoded string.
*/
public final static byte[] decode(String s) {
int length = s.length();
if (length == 0) {
return new byte[0];
}

int sndx = 0, endx = length - 1;
int pad = s.charAt(endx) == '=' ? (s.charAt(endx - 1) == '=' ? 2 : 1) : 0;
int cnt = endx - sndx + 1;
int sepCnt = length > 76 ? (s.charAt(76) == '\r' ? cnt / 78 : 0) << 1 : 0;
int len = ((cnt - sepCnt) * 6 >> 3) - pad;
byte[] dest = new byte[len];

int d = 0;
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
int i = INV[s.charAt(sndx++)] << 18 | INV[s.charAt(sndx++)] << 12 | INV[s.charAt(sndx++)] << 6 | INV[s.charAt(sndx++)];

dest[d++] = (byte) (i >> 16);
dest[d++] = (byte) (i >> 8);
dest[d++] = (byte) i;

if (sepCnt > 0 && ++cc == 19) {
sndx += 2;
cc = 0;
}
}

if (d < len) {
int i = 0;
for (int j = 0; sndx <= endx - pad; j++) {
i |= INV[s.charAt(sndx++)] << (18 - j * 6);
}
for (int r = 16; d < len; r -= 8) {
dest[d++] = (byte) (i >> r);
}
}

return dest;
}

/**
* Decodes a BASE64 encoded char array.
*/
public final byte[] decode(char[] arr) {
int length = arr.length;
if (length == 0) {
return new byte[0];
}

int sndx = 0, endx = length - 1;
int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0;
int cnt = endx - sndx + 1;
int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0;
int len = ((cnt - sepCnt) * 6 >> 3) - pad;
byte[] dest = new byte[len];

int d = 0;
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[arr[sndx++]];

dest[d++] = (byte) (i >> 16);
dest[d++] = (byte) (i >> 8);
dest[d++] = (byte) i;

if (sepCnt > 0 && ++cc == 19) {
sndx += 2;
cc = 0;
}
}

if (d < len) {
int i = 0;
for (int j = 0; sndx <= endx - pad; j++) {
i |= INV[arr[sndx++]] << (18 - j * 6);
}
for (int r = 16; d < len; r -= 8) {
dest[d++] = (byte) (i >> r);
}
}

return dest;
}

}

本文标题:java加密工具类

文章作者:wangming

发布时间:2019年01月21日 - 21:36:54

最后更新:2019年01月22日 - 23:36:59

原始链接:https://syxiaowanzi.github.io/2019/01/21/java加密工具类/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%