TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
沙发
发表于 2014-12-25 13:48:17
|只看该作者
- /**
# O' j- c% e9 \ - * 加密解密
! u- O/ T- C8 ^ - * @author zhangZhiPeng
- D. Z9 n. [# q3 H3 @ - * @date 2014年12月24日5 N; Z" N. @ E' D3 J& g# Y) e: _
- */
: ? ?) B0 b# e! b: a - import java.io.UnsupportedEncodingException;. g7 p7 Z* P5 O2 q% s+ G
- import java.security.*;) j3 w+ ]9 _& q
- import java.util.ResourceBundle;
! f5 M! F+ Q# Z5 M
, k2 ]# E- Q& B) {- [- import javax.crypto.*;! a, \) s; M& [; |: ^0 ]
- import javax.crypto.spec.SecretKeySpec;
( _$ c5 U! {: s! V - public class AESUtil {3 J7 D0 v9 [4 k/ \3 ?
- private static String PASSWORD = ""; c a0 [$ t& q# G8 `, @" w8 S8 K
- ! ^; S" g# [# W+ |
- static{1 T: I) ^# z+ L6 V" M
- ResourceBundle resource = ResourceBundle.getBundle("config");
/ z- `+ D% v( H8 T - PASSWORD = resource.getString("PASSWORD");& P5 E0 k/ @! N# i
- }3 n9 G. j' Z ~/ Z/ t
- public static byte[] encrypt(String content) { 1 h$ ]: ?* e. L4 y! }/ x
- try {
0 z' z" o. R) \2 A$ I' a - KeyGenerator kgen = KeyGenerator.getInstance("AES"); //KeyGenerator提供(对称)密钥生成器的功能。使用getInstance 类方法构造密钥生成器。& v; [: [. L/ d6 P y5 n8 c
- kgen.init(128, new SecureRandom(PASSWORD.getBytes()));//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。
3 h! R: q2 E1 w) d - SecretKey secretKey = kgen.generateKey();
2 D: \$ W2 O) R! E w# u - byte[] enCodeFormat = secretKey.getEncoded(); ! c* ]4 @% W3 V% H; b
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");//使用SecretKeySpec类来根据一个字节数组构造一个 SecretKey,,而无须通过一个(基于 provider 的)SecretKeyFactory.0 v$ | N0 r& z& J( b Q1 Z4 O
- Cipher cipher = Cipher.getInstance("AES");// 创建密码器 //为创建 Cipher 对象,应用程序调用 Cipher 的 getInstance 方法并将所请求转换 的名称传递给它。还可以指定提供者的名称(可选)。 ( H5 w5 d& o g, ~1 q/ x
- byte[] byteContent = content.getBytes("utf-8"); " f! M2 l$ C+ w; {
- cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 8 g; Z" I+ h+ c" ~ V
- byte[] result = cipher.doFinal(byteContent); //按单部分操作加密或解密数据,或者结束一个多部分操作。数据将被加密或解密(具体取决于此 Cipher 的初始化方式)。
0 i; n& v& F. d3 G! e - return result; // 加密
7 q3 R: o/ ^6 s( j - } catch (NoSuchAlgorithmException e) { , M7 m d6 R9 v8 k8 |
- e.printStackTrace();
/ A+ @% Y0 `# m0 ~ t# P - } catch (NoSuchPaddingException e) { 7 R9 g6 z, D6 G9 C
- e.printStackTrace();
) }% ~- v: x% h9 M! |/ _% w7 l! t% H( O/ ] - } catch (InvalidKeyException e) { 4 D2 s$ p* M& {4 `
- e.printStackTrace(); 7 e- X9 [/ [. R" o. @& l! B! l* a
- } catch (UnsupportedEncodingException e) {
- A( N/ O+ d5 {! F% H. z - e.printStackTrace();
" W% ]% | k+ J& O - } catch (IllegalBlockSizeException e) { % X( H% V% N- U( c# a# V# }5 B K
- e.printStackTrace(); 4 A9 @8 _+ O) O7 I) e, V! b
- } catch (BadPaddingException e) { ( l; _5 l$ o' m3 O) b% C
- e.printStackTrace(); " _) W1 t7 g" M/ a
- }
5 e: B$ }) Z2 K1 d1 d9 b f - return null;
; z4 C, i& b; a1 Z Z' C - }
6 H# Q3 [7 [# A x0 q* J# }4 Y - public static byte[] decrypt(byte[] content) { 8 k D" Z" x( ?- P
- try {4 o0 j: o* |. l) A9 Z. u
- KeyGenerator kgen = KeyGenerator.getInstance("AES"); 4 n/ O$ {. w% ]0 ?
- kgen.init(128, new SecureRandom(PASSWORD.getBytes())); ; D1 q) | y) p1 q
- SecretKey secretKey = kgen.generateKey(); 1 u8 k0 e; D T& Q
- byte[] enCodeFormat = secretKey.getEncoded(); , @9 n9 ?% B2 R2 n$ x6 J
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
8 ~$ U: O8 w6 `% {; f/ N - Cipher cipher = Cipher.getInstance("AES");// 创建密码器
0 F0 A8 M( v1 ?/ Z5 H! Z - cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 ( j' \# _ G, [- W/ G
- byte[] result = cipher.doFinal(content);
1 u: \2 y! Q% c$ r$ C - return result; // 加密 7 s' I7 o6 B) m4 }2 w! X7 z2 F
- } catch (NoSuchAlgorithmException e) {
3 i/ f; e# g S; t3 Y* v - e.printStackTrace(); " }" `! b- S. L, S8 l' i8 d
- } catch (NoSuchPaddingException e) {
2 `) B2 r) I5 p# V6 d - e.printStackTrace(); # v# i& P# m3 t, Q' [6 J/ L
- } catch (InvalidKeyException e) {
8 h& ?2 G2 R. Z, M+ _5 H - e.printStackTrace();
1 f* d3 [6 f2 k - } catch (IllegalBlockSizeException e) {
) b: ?8 N A& F, y - e.printStackTrace(); ! N0 ?/ V4 |6 [% x( ?+ s
- } catch (BadPaddingException e) {
$ i1 k+ M% [3 M. v2 V# F3 ^ - e.printStackTrace();
5 ?; ~9 i# E$ u6 d, ~, l - } , H. X* n i5 R9 ]6 O' ~+ k
- return null;
1 x8 Y2 w; A2 f [! m! @ - }
# d6 }/ C1 ~1 \) `" R - /**将二进制转换成16进制 9 D; p) G/ b. s0 r
- * @param buf , O. M! _ h8 w2 W) h9 m6 Y
- * @return + l( }9 |% u' F# l8 {
- */
9 ?3 h, f$ g5 ^+ L0 f; k/ f2 m9 S - public static String parseByte2HexStr(byte buf[]) { * r0 A2 O; g) F0 T( T8 U
- StringBuffer sb = new StringBuffer();
# R: t# V2 n2 c. h! Y D- Y - for (int i = 0; i < buf.length; i++) { - ~7 Y$ }+ ?: a6 r! d+ ~2 J
- String hex = Integer.toHexString(buf[i] & 0xFF); ) a/ A: K* v4 C" n N# K& Y
- if (hex.length() == 1) {
6 c/ Z; b7 X I6 I - hex = '0' + hex;
* g( _: @# U) u! O - }
2 M1 p: V1 L7 O0 e2 ~( B! w - sb.append(hex.toUpperCase());
" L4 j3 z$ |- @1 f - } " Z0 }% S1 I$ d: G' a' f. }
- return sb.toString(); 5 F5 k+ U" X7 H: J
- }
8 s% {: N, G$ ]/ R# c - /**将16进制转换为二进制 % C9 T9 X5 s. \' j7 b {
- * @param hexStr / k/ V5 c& J m6 w% _0 [
- * @return
% `) g4 y+ n' J8 S9 m& b' \6 J( F" M - */
; t/ u( }8 W$ Z$ v7 @ - public static byte[] parseHexStr2Byte(String hexStr) {
; _! A4 r5 y* g! J4 L$ o9 U1 a9 P - if (hexStr.length() < 1) 8 Z4 @: l, q% f. M0 P7 ~) R% a
- return null; ( n! D2 N( o/ C, x" X, H2 p% |
- byte[] result = new byte[hexStr.length()/2];
# z: \" [; x6 G/ t) W: s+ ~ - for (int i = 0;i< hexStr.length()/2; i++) {
0 C d: c) S$ B$ {3 @ - int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
% M) Y7 k2 p) W/ z' w; U( ` - int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); ' Q; m; R8 X N$ C, [3 s
- result[i] = (byte) (high * 16 + low); 9 |! l! b2 v. H, U; ?1 _
- }
. H; U, {% {8 K* t' |; a( }7 _6 G - return result;
% N; y$ Y" e, ^* f P! x! j - }
) a- \: q! n8 {! P - //测试
( _! x$ n2 V7 j5 w - public static void main(String[] args) {
$ K1 n/ o% J+ d/ R# w* G' k - String content = "test";
+ I" t, x8 h* h" ~# W/ W" u - //加密 + |5 U3 h! }' c7 ^' h# b8 W, ~
- System.out.println("加密前:" + content); 3 m, A* j3 r7 ]0 f
- byte[] encryptResult = encrypt(content); 5 `6 Y+ |2 s) Z; i
- String encryptResultStr = parseByte2HexStr(encryptResult);
/ w4 d2 E i4 f: k1 Z7 q; e - System.out.println("加密后:" + encryptResultStr); 8 B2 b" \9 v0 [2 p. Y
- //解密 : P. u8 ^! F6 {( x5 `. x" J3 T
- byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
- a! n4 {! K1 V$ k% }% n - byte[] decryptResult = decrypt(decryptFrom);
, X! f8 P o* ^ - System.out.println("解密后:" + new String(decryptResult));
`& ~+ r1 R) z& } - 9 U- D0 `. Q6 d4 t
- }
1 |' p. s* q+ E0 V - }
复制代码 |
|