TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
沙发
发表于 2014-12-25 13:48:17
|只看该作者
- /**" i1 G: ?4 K/ P" c: L, y
- * 加密解密
/ ]# f# E' l$ P0 _5 B - * @author zhangZhiPeng
# F# f% B4 h. r7 \1 t: t - * @date 2014年12月24日
/ {, W; g( Q- A - */
6 c1 l9 E% H; P" D- G" Q0 J - import java.io.UnsupportedEncodingException;
: V* ^: ~8 ~# R. }1 h3 L - import java.security.*;8 W% z" W0 x( R# e0 Y
- import java.util.ResourceBundle;
" g: s& F" r! h$ G0 D# n5 Y
) d6 _5 j7 i, k* Q$ I- import javax.crypto.*;
! K$ l! K( H8 _( | - import javax.crypto.spec.SecretKeySpec;8 P5 a' V' H2 v9 I* n B
- public class AESUtil {
3 Y: z- `: S- x: @ - private static String PASSWORD = "";8 x3 q: h8 H, W* @
- 8 f2 l3 @. k) j! A$ B! J1 J+ W
- static{. p; }) J5 e/ |5 }
- ResourceBundle resource = ResourceBundle.getBundle("config");
* j4 r+ z0 U' ]8 a( h - PASSWORD = resource.getString("PASSWORD");
& E( H0 N: J0 y, C - }. H# t' Y% a! S+ S
- public static byte[] encrypt(String content) {
8 w1 ^) L7 B; v0 _4 S( y1 t! x - try {
$ y! k& M# c% J! d, f# l - KeyGenerator kgen = KeyGenerator.getInstance("AES"); //KeyGenerator提供(对称)密钥生成器的功能。使用getInstance 类方法构造密钥生成器。
% o" F; g9 ~& G - kgen.init(128, new SecureRandom(PASSWORD.getBytes()));//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。" B% K e" { R" ?1 E- ^0 j
- SecretKey secretKey = kgen.generateKey(); . s% S* I3 }: M* g T7 W
- byte[] enCodeFormat = secretKey.getEncoded();
7 K4 l! @+ j2 c0 g8 y - SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");//使用SecretKeySpec类来根据一个字节数组构造一个 SecretKey,,而无须通过一个(基于 provider 的)SecretKeyFactory.
: Z+ C$ j! p" e' l' I - Cipher cipher = Cipher.getInstance("AES");// 创建密码器 //为创建 Cipher 对象,应用程序调用 Cipher 的 getInstance 方法并将所请求转换 的名称传递给它。还可以指定提供者的名称(可选)。
( H" c. ~9 u0 q- s4 g( f - byte[] byteContent = content.getBytes("utf-8"); 6 Y5 r9 f, h D& ~+ p/ k
- cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
/ w" n: Z* p8 U: r, F - byte[] result = cipher.doFinal(byteContent); //按单部分操作加密或解密数据,或者结束一个多部分操作。数据将被加密或解密(具体取决于此 Cipher 的初始化方式)。/ C2 r* ]1 P/ l7 y2 ?6 W+ D; w
- return result; // 加密 # ?$ G2 w# F6 h: z9 e
- } catch (NoSuchAlgorithmException e) {
* M, G: l3 ?6 v - e.printStackTrace(); ; @, F' F1 X9 @" P% L- N
- } catch (NoSuchPaddingException e) {
; [" M* f0 P# t t- G; y, } _ - e.printStackTrace(); - A* G4 W% X5 X+ S$ F' R5 j
- } catch (InvalidKeyException e) { ( T/ u2 X0 G5 i/ g" @0 l; G
- e.printStackTrace();
, j( o) \6 x+ M4 d- a q - } catch (UnsupportedEncodingException e) { : E4 T& u8 H5 I
- e.printStackTrace();
' d+ r) m1 N9 q8 c+ Y, L& R4 e - } catch (IllegalBlockSizeException e) { ' I* l: t) m! S4 Y
- e.printStackTrace(); * q! N! b1 F g
- } catch (BadPaddingException e) { 0 {* q7 _! }1 P2 y* D* a
- e.printStackTrace();
; [# s( ~8 l2 [ p1 M9 O/ K - } + s4 {: s8 n3 A1 C3 U
- return null; * H7 \6 Z9 I5 X. T( d1 ^: f
- }
, g1 s" a: h2 h) t' a' [4 A" M - public static byte[] decrypt(byte[] content) { 1 L5 P% T2 V& H: ~, ]/ U
- try {
5 @( q. @' T6 v" J* @; F' N - KeyGenerator kgen = KeyGenerator.getInstance("AES");
/ i! w7 B9 }: o9 ?8 | - kgen.init(128, new SecureRandom(PASSWORD.getBytes()));
$ m* I5 W8 F8 {6 I6 j4 W2 J - SecretKey secretKey = kgen.generateKey(); - M7 L3 _( X% u
- byte[] enCodeFormat = secretKey.getEncoded();
4 g) y% I' o; f - SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); & ?" t! l/ ~0 j* C/ M- \$ {
- Cipher cipher = Cipher.getInstance("AES");// 创建密码器
5 n K/ W0 E$ D/ \' x - cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 7 \6 ?7 |5 O4 x1 p) P# m. F
- byte[] result = cipher.doFinal(content); 6 Q( C! X- m4 Y" f/ E
- return result; // 加密 ( q8 A; P+ `/ i5 R% F( F0 q1 F
- } catch (NoSuchAlgorithmException e) {
/ W5 k$ T" k* j& Z! j) P* b - e.printStackTrace();
2 E% m2 \0 d. t% Q" I( }3 |# S& ? - } catch (NoSuchPaddingException e) { 1 ]1 ~; U9 O5 R% T; }( b
- e.printStackTrace(); . b e8 r( s. m! r) o2 \- U' Z
- } catch (InvalidKeyException e) {
( w8 w" b) U& ?8 ^ - e.printStackTrace(); Q7 ^1 M4 n9 k \2 C \
- } catch (IllegalBlockSizeException e) {
- d3 W7 Q$ `1 V" V4 p: s$ d) D+ m - e.printStackTrace(); 7 u3 H, M D2 n5 z, m
- } catch (BadPaddingException e) { " d6 D" q5 I* O0 Y# Q; A. B
- e.printStackTrace();
4 M b0 ~; {, u4 ]& ~8 |4 L$ u: C$ Y - }
0 o# _6 N) j4 Q9 a - return null; . y) H/ e+ o6 G/ d9 i+ u, K
- }
0 ?- E! M4 o; S: ]. f) t, |/ @$ b - /**将二进制转换成16进制
9 H" q3 Y7 I) p2 B - * @param buf 7 r; v7 \+ }6 a# D" S) f
- * @return
8 S+ M9 y/ W n6 e( {- a) y - */ - e1 M+ y4 I( ~' i6 ~4 a7 G6 t
- public static String parseByte2HexStr(byte buf[]) { 4 g! u" H5 t; [- t! [7 O. g5 p
- StringBuffer sb = new StringBuffer(); 5 [/ R4 q$ z( j
- for (int i = 0; i < buf.length; i++) { # }3 k! H- r6 h- ?( g, ^- m4 U
- String hex = Integer.toHexString(buf[i] & 0xFF);
5 z8 l4 ~" T( |+ M, K - if (hex.length() == 1) { + Z0 y4 t+ u7 M; e9 j F
- hex = '0' + hex;
7 L) w) u/ x3 w' N5 L - } 2 Q3 w: G( _5 K* I( e
- sb.append(hex.toUpperCase());
# {# m) @6 ~ J - } 7 u+ Z, P4 x: H, k7 w
- return sb.toString();
7 Z$ w: y) J5 o z - } 0 q5 n. m) n' j6 Z- k( }
- /**将16进制转换为二进制
/ u1 C9 h1 P. [* | - * @param hexStr ' ^/ n( f7 W$ O6 ~& d c% ?
- * @return % [6 [. h- _& S/ ^4 X+ y) s
- */ 1 _3 z& d% u9 v! A% Y: Z
- public static byte[] parseHexStr2Byte(String hexStr) { " \8 x' u2 [ @
- if (hexStr.length() < 1) ( _/ D/ g, Z; _& L2 ^! z4 g
- return null;
" O- T. d/ G. i# o: z5 F+ c) U! j - byte[] result = new byte[hexStr.length()/2];
* o8 W; T2 T8 g. x- _ - for (int i = 0;i< hexStr.length()/2; i++) { ( q! N8 H( d& t- o
- int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
" f- P6 J* m( V. O+ E# U - int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); / o) t/ t% o" u, m
- result[i] = (byte) (high * 16 + low); ! a( W- }5 q4 R+ p
- } 3 s6 G( `) j: o& s8 f( k
- return result; 2 x1 v! P; c7 R$ T# _
- } , R! E$ ~$ H9 h, g# }: ~5 `5 |
- //测试
! E& T0 X! [, k& E) H! |0 r4 T/ a! Q7 h - public static void main(String[] args) {
- W0 v: Z" g9 n# V3 ^ - String content = "test";
, V% H, S3 f9 _' I - //加密 $ G( N2 u& i: A2 y& V8 n( a
- System.out.println("加密前:" + content);
, i3 U. z5 ]# z" O - byte[] encryptResult = encrypt(content);
4 u$ a V+ F5 s( | - String encryptResultStr = parseByte2HexStr(encryptResult); ) C h6 e" w' X: a. [& ?4 O } G% L
- System.out.println("加密后:" + encryptResultStr); " {0 e# ^% _( T3 m
- //解密
+ N( R" f& o( V - byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
. p& @8 |' O) }7 o! F9 P2 a" z2 w - byte[] decryptResult = decrypt(decryptFrom); 0 H+ N9 }; h& E2 r4 ]
- System.out.println("解密后:" + new String(decryptResult)); {1 A& ?& u! m+ f
- : N* Z( T; i: V/ r& R
- }( m: M _1 i: J
- }
复制代码 |
|