一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語(yǔ)言 - JAVA教程 - Java加密解密工具(適用于JavaSE/JavaEE/Android)

Java加密解密工具(適用于JavaSE/JavaEE/Android)

2020-04-18 12:04捕實(shí)者_(dá)說(shuō) JAVA教程

這篇文章主要介紹了Java加密解密工具,適用于JavaSE/JavaEE/Android,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了一個(gè)適用于JavaSE/JavaEE/Android的Java加密解密工具,供大家學(xué)習(xí),具體內(nèi)容如下

?
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package longshu.utils.security;
 
import java.lang.reflect.Method;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
 
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;
 
/**
 * Java加密解密工具.
 * JavaSE/JavaEE/Android都適用
 *
 * @author longshu 2016年4月13日
 */
public class EncryptDecrypt {
 // 無(wú)需創(chuàng)建對(duì)象
 private EncryptDecrypt() {
 }
 
 /**
  * SHA1加密Bit數(shù)據(jù)
  * @param source byte數(shù)組
  * @return 加密后的byte數(shù)組
  */
 public static byte[] SHA1Bit(byte[] source) {
  try {
   MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");
   sha1Digest.update(source);
   byte targetDigest[] = sha1Digest.digest();
   return targetDigest;
  } catch (NoSuchAlgorithmException e) {
   throw new RuntimeException(e);
  }
 }
 
 /**
  * SHA1加密字符串?dāng)?shù)據(jù)
  * @param source 要加密的字符串
  * @return 加密后的字符串
  */
 public static String SHA1(String source) {
  return byte2HexStr(SHA1Bit(source.getBytes()));
 }
 
 /**
  * MD5加密Bit數(shù)據(jù)
  * @param source byte數(shù)組
  * @return 加密后的byte數(shù)組
  */
 public static byte[] MD5Bit(byte[] source) {
  try {
   // 獲得MD5摘要算法的 MessageDigest對(duì)象
   MessageDigest md5Digest = MessageDigest.getInstance("MD5");
   // 使用指定的字節(jié)更新摘要
   md5Digest.update(source);
   // 獲得密文
   return md5Digest.digest();
  } catch (NoSuchAlgorithmException e) {
   throw new RuntimeException(e);
  }
 }
 
 /**
  * MD5加密字符串,32位長(zhǎng)
  * @param source 要加密的內(nèi)容
  * @return 加密后的內(nèi)容
  */
 public static String MD5(String source) {
  return byte2HexStr(MD5Bit(source.getBytes()));
 }
 
 /**
  * BASE64編碼
  * @param source 要編碼的字符串
  * @return 編碼過(guò)的字符串
  */
 public static String encodeBASE64(String source) {
  Class<?> clazz = null;
  Method encodeMethod = null;
  try {// 優(yōu)先使用第三方庫(kù)
   clazz = Class.forName("org.apache.commons.codec.binary.Base64");
   encodeMethod = clazz.getMethod("encodeBase64", byte[].class);
   System.out.println("encodeBASE64-->" + clazz);
   System.out.println("encodeMethod-->" + encodeMethod);
   // 反射方法 靜態(tài)方法執(zhí)行無(wú)需對(duì)象
   return new String((byte[]) encodeMethod.invoke(null, source.getBytes()));
  } catch (ClassNotFoundException e) {
   String vm = System.getProperty("java.vm.name");
   System.out.println(vm);
   try {
    if ("Dalvik".equals(vm)) {// Android
     clazz = Class.forName("android.util.Base64");
     // byte[] Base64.encode(byte[] input,int flags)
     encodeMethod = clazz.getMethod("encode", byte[].class, int.class);
     System.out.println("encodeBASE64-->" + clazz);
     System.out.println("encodeMethod-->" + encodeMethod);
     return new String((byte[]) encodeMethod.invoke(null, source.getBytes(), 0));
    } else {// JavaSE/JavaEE
     clazz = Class.forName("sun.misc.BASE64Encoder");
     encodeMethod = clazz.getMethod("encode", byte[].class);
     System.out.println("encodeBASE64-->" + clazz);
     System.out.println("encodeMethod-->" + encodeMethod);
     return (String) encodeMethod.invoke(clazz.newInstance(), source.getBytes());
    }
   } catch (ClassNotFoundException e1) {
    return null;
   } catch (Exception e1) {
    return null;
   }
  } catch (Exception e) {
   return null;
  }
  /*
   * Android
   * android.util.Base64
   */
  // return Base64.encodeToString(source, Base64.DEFAULT);
  // return new String(Base64.encode(source.getBytes(), Base64.DEFAULT));
 
  /*
   * JavaSE/JavaEE
   */
  // sun.misc.BASE64Encoder
  // BASE64Encoder encoder = new BASE64Encoder();
  // return encoder.encode(source.getBytes());
 
  // org.apache.commons.codec.binary.Base64
  // return new String(Base64.encodeBase64(source.getBytes()));
 }
 
 /**
  * BASE64解碼
  * @param encodeSource 編碼過(guò)的字符串
  * @return 編碼前的字符串
  */
 public static String decodeBASE64(String encodeSource) {
  Class<?> clazz = null;
  Method decodeMethod = null;
 
  try {// 優(yōu)先使用第三方庫(kù)
   clazz = Class.forName("org.apache.commons.codec.binary.Base64");
   decodeMethod = clazz.getMethod("decodeBase64", byte[].class);
   System.out.println("decodeBASE64-->" + clazz);
   System.out.println("decodeMethod-->" + decodeMethod);
   // 反射方法 靜態(tài)方法執(zhí)行無(wú)需對(duì)象
   return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes()));
  } catch (ClassNotFoundException e) {
   String vm = System.getProperty("java.vm.name");
   System.out.println(vm);
   try {
    if ("Dalvik".equals(vm)) {// Android
     clazz = Class.forName("android.util.Base64");
     // byte[] Base64.decode(byte[] input, int flags)
     decodeMethod = clazz.getMethod("decode", byte[].class, int.class);
     System.out.println("decodeBASE64-->" + clazz);
     System.out.println("decodeMethod-->" + decodeMethod);
     return new String((byte[]) decodeMethod.invoke(null, encodeSource.getBytes(), 0));
    } else { // JavaSE/JavaEE
     clazz = Class.forName("sun.misc.BASE64Decoder");
     decodeMethod = clazz.getMethod("decodeBuffer", String.class);
     System.out.println("decodeBASE64-->" + clazz);
     System.out.println("decodeMethod-->" + decodeMethod);
     return new String((byte[]) decodeMethod.invoke(clazz.newInstance(), encodeSource));
    }
   } catch (ClassNotFoundException e1) {
    return null;
   } catch (Exception e1) {
    return null;
   }
  } catch (Exception e) {
   return null;
  }
  /*
   * Android
   * android.util.Base64
   */
  // return new
  // String(Base64.decode(encodeSource.getBytes(),Base64.DEFAULT));
 
  /*
   * JavaSE/JavaEE
   */
  // sun.misc.BASE64Decoder
  // try {
  // BASE64Decoder decoder = new BASE64Decoder();
  // return new String(decoder.decodeBuffer(encodeSource));
  // } catch (IOException e) {
  // throw new RuntimeException(e);
  // }
 
  // org.apache.commons.codec.binary.Base64
  // return new String(Base64.decodeBase64(encodeSource.getBytes()));
 }
 
 /**
  * AES加密
  * @param content 待加密的內(nèi)容
  * @param password 加密密碼
  * @return
  */
 public static byte[] encryptBitAES(byte[] content, String password) {
  try {
   Cipher encryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 創(chuàng)建密碼器
   encryptCipher.init(Cipher.ENCRYPT_MODE, getKey(password));// 初始化
   byte[] result = encryptCipher.doFinal(content);
   return result; // 加密
  } 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();
  }
  return null;
 }
 
 /**
  * AES解密
  * @param content 待解密內(nèi)容
  * @param password 解密密鑰
  * @return
  */
 public static byte[] decryptBitAES(byte[] content, String password) {
  try {
   Cipher decryptCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 創(chuàng)建密碼器
   decryptCipher.init(Cipher.DECRYPT_MODE, getKey(password));// 初始化
   byte[] result = decryptCipher.doFinal(content);
   return result; // 加密結(jié)果
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 /**
  * AES字符串加密
  * @param content 待加密的內(nèi)容
  * @param password 加密密碼
  * @return
  */
 public static String encryptAES(String content, String password) {
  return byte2HexStr(encryptBitAES(content.getBytes(), password));
 }
 
 /**
  * AES字符串解密
  * @param content 待解密內(nèi)容
  * @param password 解密密鑰
  * @return
  */
 public static String decryptAES(String content, String password) {
  return new String(decryptBitAES(hexStr2Bytes(content), password));
 }
 
 /**
  * 從指定字符串生成密鑰
  * @param password 構(gòu)成該秘鑰的字符串
  * @return 生成的密鑰
  * @throws NoSuchAlgorithmException
  */
 private static Key getKey(String password) throws NoSuchAlgorithmException {
  SecureRandom secureRandom = new SecureRandom(password.getBytes());
  // 生成KEY
  KeyGenerator kgen = KeyGenerator.getInstance("AES");
  kgen.init(128, secureRandom);
  SecretKey secretKey = kgen.generateKey();
  byte[] enCodeFormat = secretKey.getEncoded();
  // 轉(zhuǎn)換KEY
  SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
  return key;
 }
 
 /**
  * 將byte數(shù)組轉(zhuǎn)換為表示16進(jìn)制值的字符串.
  * 如:byte[]{8,18}轉(zhuǎn)換為:0812
  * 和 byte[] hexStr2Bytes(String strIn) 互為可逆的轉(zhuǎn)換過(guò)程.
  * @param bytes 需要轉(zhuǎn)換的byte數(shù)組
  * @return 轉(zhuǎn)換后的字符串
  */
 public static String byte2HexStr(byte[] bytes) {
  int bytesLen = bytes.length;
  // 每個(gè)byte用兩個(gè)字符才能表示,所以字符串的長(zhǎng)度是數(shù)組長(zhǎng)度的兩倍
  StringBuffer hexString = new StringBuffer(bytesLen * 2);
  for (int i = 0; i < bytesLen; i++) {
   // 將每個(gè)字節(jié)與0xFF進(jìn)行與運(yùn)算,然后轉(zhuǎn)化為10進(jìn)制,然后借助于Integer再轉(zhuǎn)化為16進(jìn)制
   String hex = Integer.toHexString(bytes[i] & 0xFF);
   if (hex.length() < 2) {
    hexString.append(0);// 如果為1位 前面補(bǔ)個(gè)0
   }
   hexString.append(hex);
  }
  return hexString.toString();
 }
 
 /**
  * 將表示16進(jìn)制值的字符串轉(zhuǎn)換為byte數(shù)組,
  * 和 String byte2HexStr(byte[] bytes) 互為可逆的轉(zhuǎn)換過(guò)程.
  * @param bytes
  * @return 轉(zhuǎn)換后的byte數(shù)組
  */
 public static byte[] hexStr2Bytes(String strIn) {
  byte[] arrB = strIn.getBytes();
  int iLen = arrB.length;
 
  // 兩個(gè)字符表示一個(gè)字節(jié),所以字節(jié)數(shù)組長(zhǎng)度是字符串長(zhǎng)度除以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;
 }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 手机看片黄色 | 亚洲男gay同性同志 亚洲免费在线看 | 好大好深好涨好烫还要 | 极品ts赵恩静和直男激战啪啪 | 欧美日韩国产一区二区三区在线观看 | 日本视频观看 | 996热精品视频在线观看 | 国产香蕉一区二区在线观看 | 齐天大性之七仙女欲春迅雷链接 | 国色天香论坛社区在线视频 | 国产99er66在线视频 | 色婷在线 | 亚洲麻豆精品果冻传媒 | 国产一区二区播放 | 成人网18免费网 | 日韩先锋 | 欧美日韩亚洲一区二区三区在线观看 | ange venus与黑人 | sex5·性屋娱乐 | 狠狠五月天中文字幕 | 日本四虎影院 | 国产欧美日韩亚洲精品区2345 | 欧美在线播放一区二区 | 99精品免费在线 | 亚洲精品一二三四 | spank日本网站脱裤子打屁股 | 亚洲成人视屏 | 国产精品一区二区不卡的视频 | 精品一区二区三区在线成人 | 欧美靠逼 | 紧身牛仔裤美女被啪啪久久网 | 男人边吃奶边做好爽视频免费 | 日本一本草久p | 日本高清色视影www日本 | 星星动漫在线观看无删减 | 亚洲2017天堂色无码 | 日韩成人精品在线 | 好猛好紧好硬使劲好大刺激视频 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 国产成人综合一区人人 | 国产精品视频在线观看 |