TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
沙发
发表于 2014-12-25 13:48:17
|只看该作者
- /**
1 s- L* E2 b2 [- d4 p+ p3 J- } - * 加密解密! x/ G- z% S) ]& `
- * @author zhangZhiPeng' o% k- A. _0 L0 D
- * @date 2014年12月24日
5 Z, K4 g8 h3 {7 Z' U# O - */1 G" M6 ^5 u% R) l+ e* ?7 T
- import java.io.UnsupportedEncodingException;& `+ F4 m% k7 K' H5 Y
- import java.security.*;
( n0 w7 w5 o: w4 c; T - import java.util.ResourceBundle;
7 e% @3 j. B; F8 T4 O% N( u
3 ^" W6 v$ q+ E% u" Z- import javax.crypto.*;% i6 [9 N8 W# _3 g, d/ d3 u- _
- import javax.crypto.spec.SecretKeySpec;
. ^0 m" x# I/ b - public class AESUtil {" A- ?5 L/ B! K
- private static String PASSWORD = "";( N/ B1 i- x2 j [, K- d" J; E
( u' y% _" I. j. `) R9 U: m- static{) q k* T- G( A, z' p% F
- ResourceBundle resource = ResourceBundle.getBundle("config");& |, u) G8 c2 z$ k; ]' [
- PASSWORD = resource.getString("PASSWORD");
! g3 M J2 r# X* k - }
4 y% t% E5 j& a" n) R k1 `0 I - public static byte[] encrypt(String content) { 1 S- Y/ m e$ h4 W
- try { % Q0 C8 y- [6 ]0 k3 D
- KeyGenerator kgen = KeyGenerator.getInstance("AES"); //KeyGenerator提供(对称)密钥生成器的功能。使用getInstance 类方法构造密钥生成器。* u5 E @, B( y |
- kgen.init(128, new SecureRandom(PASSWORD.getBytes()));//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。
& k$ b4 r& C1 `3 e1 g - SecretKey secretKey = kgen.generateKey();
1 y1 {: Y/ y% g - byte[] enCodeFormat = secretKey.getEncoded(); 7 ^! `) Z, X/ v
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");//使用SecretKeySpec类来根据一个字节数组构造一个 SecretKey,,而无须通过一个(基于 provider 的)SecretKeyFactory. d2 U* D$ O$ _% |+ ]; Z: e
- Cipher cipher = Cipher.getInstance("AES");// 创建密码器 //为创建 Cipher 对象,应用程序调用 Cipher 的 getInstance 方法并将所请求转换 的名称传递给它。还可以指定提供者的名称(可选)。
4 d& ~# u) T& U9 N - byte[] byteContent = content.getBytes("utf-8"); 5 ]4 H3 |3 l$ D9 f
- cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 4 d; ~7 _& W0 I6 R9 e+ Y3 Y' Q4 V) B
- byte[] result = cipher.doFinal(byteContent); //按单部分操作加密或解密数据,或者结束一个多部分操作。数据将被加密或解密(具体取决于此 Cipher 的初始化方式)。
i5 n' j9 O, S) t* ^2 @ - return result; // 加密
% x7 q# X% w3 S& a4 x - } catch (NoSuchAlgorithmException e) { ) f3 N% P8 G7 d# w& z! m
- e.printStackTrace();
9 P% z, x( ~ `. Z2 |. ^ - } catch (NoSuchPaddingException e) { ( F8 E# g+ d4 \4 l, E+ v% l
- e.printStackTrace();
. V, q* X0 R6 ^9 N, y8 t* o* O. K - } catch (InvalidKeyException e) {
" ~& l2 }1 i* U6 Z: F. S/ ? - e.printStackTrace(); 5 j2 C! P) E1 [+ P% M% B& I
- } catch (UnsupportedEncodingException e) { 3 v8 S/ ], ]' e- K
- e.printStackTrace(); ; k$ N! T5 e# ?" H
- } catch (IllegalBlockSizeException e) { ]3 y6 ?5 ?2 W3 Y" ]2 A
- e.printStackTrace(); 3 a6 e$ `2 ^- {$ E& V
- } catch (BadPaddingException e) { Z2 e3 O4 U" U4 {# I; Z0 p
- e.printStackTrace();
& }7 ^1 \8 J+ Y( { - }
5 T4 g. ~: t3 u& h+ m" U; { - return null; 2 S# y# H, q/ B7 i, W
- } ! e) a$ ^- B! }+ D; M4 I' f, W
- public static byte[] decrypt(byte[] content) {
7 _- t- L4 Q0 Q2 N2 m/ s0 n5 Q _ - try {6 Q1 A& e; C; e3 G2 N0 u$ f
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
& y$ {5 o P9 q$ Z1 u; `: P6 V - kgen.init(128, new SecureRandom(PASSWORD.getBytes())); 5 L* c- x: w, [7 S5 o g
- SecretKey secretKey = kgen.generateKey(); ) n0 N3 N9 G2 H$ ]8 U
- byte[] enCodeFormat = secretKey.getEncoded();
' R# R7 @& ~% m' i& v1 | - SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 8 j) g1 d. Q Y; X4 P' Z
- Cipher cipher = Cipher.getInstance("AES");// 创建密码器 3 g- c" {. v8 e4 f# N' l
- cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 . }, N) D s, Q# V. X3 r8 v
- byte[] result = cipher.doFinal(content);
5 b) k7 O' c5 _! d. q; h - return result; // 加密 ; M" |/ g+ y/ ^: c4 \ v/ J
- } catch (NoSuchAlgorithmException e) {
$ O2 p+ [$ p& U9 E - e.printStackTrace();
8 X- w& |* q, C0 G. V! [1 j* ^& T - } catch (NoSuchPaddingException e) {
( i% a( D4 d1 k - e.printStackTrace();
* g) t- A7 r$ ]: q3 v - } catch (InvalidKeyException e) {
! ^# J" ?# N3 u4 x - e.printStackTrace();
$ |: e3 _$ g% ~' X ] - } catch (IllegalBlockSizeException e) {
+ S9 l0 H" ~' U" T- } - e.printStackTrace();
7 i- q! r/ p$ b. D6 Y% a - } catch (BadPaddingException e) { " }4 f4 p1 `' R- e5 `
- e.printStackTrace(); ! l% o$ [: o( J" x
- }
5 k8 Y% x6 f+ J0 o- A8 r - return null; 4 y! U7 i2 C; ~4 r
- }. Q/ w- c" k- a; r8 f
- /**将二进制转换成16进制 " t, \' N6 P/ Z
- * @param buf E V. Q; ]* z7 \% a: D. K8 v* M5 ?
- * @return
- O0 v# Z. X8 H2 O- Q+ S1 j9 ` - */ # z( {+ l U% q' P5 f1 r, X
- public static String parseByte2HexStr(byte buf[]) {
+ P" s# d! T2 } F* Y - StringBuffer sb = new StringBuffer(); * O% w4 s+ W/ X6 e
- for (int i = 0; i < buf.length; i++) { 0 |1 i! A; r% b
- String hex = Integer.toHexString(buf[i] & 0xFF);
' v6 e- m, o- `5 A - if (hex.length() == 1) {
4 F' {: C! l7 A% H - hex = '0' + hex; 8 l! K. o5 m% T' x( }/ b9 m5 }
- } $ ^2 X6 e. z6 X: B% E
- sb.append(hex.toUpperCase()); * H6 W! r, r1 r& [; `
- }
8 Q7 s0 n g" f6 z* F - return sb.toString();
4 u" l J8 C# w- C: z - } 2 F4 Q+ H1 s% T; {: z4 j
- /**将16进制转换为二进制
2 A: n4 g; P; k6 Z# f" v0 i - * @param hexStr $ |, v% z- A& S& ~6 J7 J: k
- * @return
9 M; M3 G$ l0 ? }9 ~" |" _1 f - */ 5 {9 L4 P( B3 v5 F" S4 `
- public static byte[] parseHexStr2Byte(String hexStr) {
7 K+ s+ x/ @$ @4 t. w& l% w* D+ n; ~' ^4 p - if (hexStr.length() < 1) . P0 r3 c; P4 |
- return null; . s% Q1 ]' v- K. Y% }
- byte[] result = new byte[hexStr.length()/2]; " l, u$ A+ a& i, P) ~
- for (int i = 0;i< hexStr.length()/2; i++) { ( t# G* |7 o3 v- i
- int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); - j( u4 T( x0 C% Y! P0 H
- int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); ! y- t& I0 b. f6 _
- result[i] = (byte) (high * 16 + low); + u, J# Z: g8 f! q1 A1 b
- } 3 }* {% n' \8 H5 N2 c8 [$ y
- return result;
4 ^- t! `: F8 T" V/ [ - } " k. A" `6 l z& q% k. Y
- //测试
, G4 a1 P: O% ^, m1 L/ _4 I) _ - public static void main(String[] args) {
9 o) K4 q6 p1 E - String content = "test";
4 L' J9 F7 q+ x# J - //加密 * n. _. o: h$ Y9 O$ q
- System.out.println("加密前:" + content); 8 Y' I; v0 q: U9 u$ v: \
- byte[] encryptResult = encrypt(content); , p' u k( z# `! E; |0 F% W$ s9 j
- String encryptResultStr = parseByte2HexStr(encryptResult); 2 V+ O# I, n4 S4 a
- System.out.println("加密后:" + encryptResultStr);
& U, v% p1 j5 a# d$ ^$ f* q - //解密
2 F" u1 D! ]8 J' \" H - byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); & z0 Y' M, C/ q7 [+ z
- byte[] decryptResult = decrypt(decryptFrom); " l1 P9 O! G" ~( M; M) s
- System.out.println("解密后:" + new String(decryptResult));
& L/ E y1 w+ c; @; N/ a/ q - 2 J8 W6 p7 c/ R/ Z5 k6 f/ {) h$ V
- }, m+ A7 a/ B0 `; `, x, p% l: K
- }
复制代码 |
|