TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
沙发
发表于 2014-12-25 13:48:17
|只看该作者
- /**
' L* G1 X3 o* B* i c - * 加密解密
" I8 k4 t& K$ p' J8 p# S6 G k, v- X' T - * @author zhangZhiPeng
. O B) W* o. Z. ~% h - * @date 2014年12月24日+ z, V' h5 c9 D7 F- T2 s. |
- */
" Q$ l( [8 c) ^0 m+ j - import java.io.UnsupportedEncodingException;
: I% a9 B& b% @ M, l+ p2 z - import java.security.*;9 A/ C0 U {9 g. b; W. T
- import java.util.ResourceBundle;3 ^" t/ K2 f9 b. v4 O. G# A
- ) v# q$ h. u# k
- import javax.crypto.*;7 d% ^& g2 S# T* y8 t
- import javax.crypto.spec.SecretKeySpec;4 e6 h: I! X1 _, L+ a( S
- public class AESUtil {
2 P2 [$ @* R! m6 d4 F% P - private static String PASSWORD = "";
- `4 E6 d8 O% k - 5 m; V* B% d; A" ^4 N
- static{# p8 }" N. [* |# \
- ResourceBundle resource = ResourceBundle.getBundle("config");' z5 I5 l4 x8 u; Q+ t* s/ Z7 w3 [% L
- PASSWORD = resource.getString("PASSWORD");
2 `) f4 y' w9 m - }! j7 Q# c* C% u8 ]7 U
- public static byte[] encrypt(String content) {
5 e0 f8 d* C5 f( N8 B - try { " K, t& C+ a+ W$ m( {
- KeyGenerator kgen = KeyGenerator.getInstance("AES"); //KeyGenerator提供(对称)密钥生成器的功能。使用getInstance 类方法构造密钥生成器。- o7 H" Q5 |; f; S" `; J; @
- kgen.init(128, new SecureRandom(PASSWORD.getBytes()));//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小。
$ A b" k7 r% _4 f) D$ } - SecretKey secretKey = kgen.generateKey();
6 }( w3 t& W% D4 w0 M - byte[] enCodeFormat = secretKey.getEncoded(); 2 t& k1 h) g: {1 A
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");//使用SecretKeySpec类来根据一个字节数组构造一个 SecretKey,,而无须通过一个(基于 provider 的)SecretKeyFactory.( {$ c* G7 ?+ N1 ]8 w6 g1 U. t* ~
- Cipher cipher = Cipher.getInstance("AES");// 创建密码器 //为创建 Cipher 对象,应用程序调用 Cipher 的 getInstance 方法并将所请求转换 的名称传递给它。还可以指定提供者的名称(可选)。 8 K2 r+ X" ]4 a4 X, G" @
- byte[] byteContent = content.getBytes("utf-8");
) L& b4 b" E2 e7 H8 ?) u - cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 $ K3 W# N$ f8 X, C8 `
- byte[] result = cipher.doFinal(byteContent); //按单部分操作加密或解密数据,或者结束一个多部分操作。数据将被加密或解密(具体取决于此 Cipher 的初始化方式)。/ T3 _. ] V5 u
- return result; // 加密 2 o# U a7 r# q% Q9 v
- } catch (NoSuchAlgorithmException e) { & q) h( k2 d+ |: ]2 \3 p) F
- e.printStackTrace(); " p4 x1 B( c: J# W' X' _) w
- } catch (NoSuchPaddingException e) { 2 ^: t+ ?- p* {- J4 f' O% b
- e.printStackTrace();
! [& n7 ?, I# E& E P - } catch (InvalidKeyException e) {
3 i- F* v5 e( h% K$ L$ ~' h! I - e.printStackTrace();
8 J9 s. j0 T1 W4 h; k' ~4 `+ C! P3 w - } catch (UnsupportedEncodingException e) { . J" K5 _5 A; V. s9 r) [/ C2 J
- e.printStackTrace(); % W, q2 g, b4 c) `, k. a
- } catch (IllegalBlockSizeException e) {
; N/ M1 `. z$ B, t6 D - e.printStackTrace(); / s# Z) N. c8 N* [& l) v7 f% u$ _
- } catch (BadPaddingException e) { & E0 B" A0 o) T" ]; b, O0 ~' @
- e.printStackTrace(); : g# w* q T1 ^6 e/ G7 S* k
- } 4 r: g4 M" Z) U, D! O
- return null; s6 A7 p' U' f1 t
- } 6 p2 M$ L b; r6 F1 t7 A! \
- public static byte[] decrypt(byte[] content) {
" Q" ~7 G8 c* S5 s9 {/ W - try {
, D" J3 [# N6 j/ Y8 J9 S3 O - KeyGenerator kgen = KeyGenerator.getInstance("AES");
/ t' g) `0 f# Y/ z" J - kgen.init(128, new SecureRandom(PASSWORD.getBytes()));
) m, A8 t$ x) P8 W: N - SecretKey secretKey = kgen.generateKey();
9 v9 l6 r4 w* x- q) h1 I5 E - byte[] enCodeFormat = secretKey.getEncoded(); 8 e4 p C, i$ q2 p
- SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
: ^# Z( x4 D% x1 k# G, @. w& }8 c- r - Cipher cipher = Cipher.getInstance("AES");// 创建密码器
8 D8 w$ Z! C$ s - cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
$ X& E; Y- c) ]+ _: J8 U3 P$ h - byte[] result = cipher.doFinal(content);
3 m0 T3 @2 _; _& l- p) `. ` - return result; // 加密
) T% p* ~, S* Y0 i1 N0 }; t3 C - } catch (NoSuchAlgorithmException e) {
" S0 D4 O( a# a/ K, @ - e.printStackTrace();
6 b; {. ^& a6 y& i - } catch (NoSuchPaddingException e) {
( c" x N8 u% d* {2 p - e.printStackTrace(); 1 q5 T) H/ _3 N4 p+ V3 a- o
- } catch (InvalidKeyException e) {
3 `$ Z4 V! X+ L, d* N7 n1 ? - e.printStackTrace();
* D0 @" A6 v: f3 ?3 \3 x) @. Z - } catch (IllegalBlockSizeException e) { + p% s" u* Z, X
- e.printStackTrace(); ! s# P. N E+ j l1 g# [* v
- } catch (BadPaddingException e) {
( C9 ?1 ?$ o0 B- M+ L3 B' k - e.printStackTrace();
2 s6 D, m3 Z) L$ b4 S3 ^ - }
" x, {. U5 H3 V% `1 S - return null;
" Z/ F6 ]( z' j* ^, G - }
2 W" d4 {6 e; G- U - /**将二进制转换成16进制 6 F7 a& G! e7 O. l! }* }# B
- * @param buf
; a1 G3 ^0 G: b, W$ ^ - * @return
$ N) L8 Y6 t3 u$ j - */
( [& T' W2 z" e/ i - public static String parseByte2HexStr(byte buf[]) {
! O$ {; N+ h$ [" m' E - StringBuffer sb = new StringBuffer(); ( B/ J5 O( G ^# \' A
- for (int i = 0; i < buf.length; i++) {
+ O* u/ r" ~! S) z* c! n i - String hex = Integer.toHexString(buf[i] & 0xFF); / b, H u9 ]2 D. G# ^% k
- if (hex.length() == 1) { 3 o3 V# q8 W/ n
- hex = '0' + hex;
" p, p8 E6 ^( Q - }
2 N2 U/ Q( l$ W6 U8 F - sb.append(hex.toUpperCase());
% U! N' @* i1 Q4 ]0 Q - } 1 D1 U8 ~9 i/ {9 x+ G9 @
- return sb.toString(); 1 q* S4 b& Z4 V' l% u
- }
& D: H/ K5 I- N0 ?- H - /**将16进制转换为二进制
8 q: x) E% f& u7 f, G - * @param hexStr
0 T" S1 f, f7 w1 y! D5 o9 T+ S - * @return ) _) c8 [8 p" p: ^2 q6 _* f
- */ & j/ ?; o( c a3 y% U$ l; X
- public static byte[] parseHexStr2Byte(String hexStr) { / \$ A2 A# [: C! P
- if (hexStr.length() < 1)
4 R g# q Z9 b" X k - return null;
( U2 [" i& d ^+ \8 w - byte[] result = new byte[hexStr.length()/2];
: B5 W% \0 x7 O1 ^ h! @' ` - for (int i = 0;i< hexStr.length()/2; i++) { # u: y$ u# u0 @3 b# U
- int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); " z+ I* u, I& N9 W
- int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
2 ~1 U: }* N- b* c - result[i] = (byte) (high * 16 + low); 5 j' q+ D% V; S( n$ {/ N' L- t
- }
/ o& Z$ F, ]* z& n; E7 z& V% N - return result; ; y8 \! u5 s! h/ z& I* x8 @
- } - i8 | a& W, X% A0 ]/ ^
- //测试
# u+ _! t/ s J# t( Y9 v+ e - public static void main(String[] args) {/ _5 ^ J" w1 C
- String content = "test";
; ~, G4 z- K% v) G - //加密 , X: U, q3 V2 }' c' M
- System.out.println("加密前:" + content);
# g* Y: `, X3 A9 n5 A - byte[] encryptResult = encrypt(content);
2 Y' d0 |2 q8 k0 ]% R* ^ - String encryptResultStr = parseByte2HexStr(encryptResult);
! J+ p$ { x* }# a4 T6 K3 r - System.out.println("加密后:" + encryptResultStr); 1 R* [& ?; D, [9 I \ ]1 I
- //解密
6 g7 e+ S- ]* a# ]/ V - byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
: d" @/ j* Q3 l1 m - byte[] decryptResult = decrypt(decryptFrom); 6 {9 k! R/ A+ @$ G- T
- System.out.println("解密后:" + new String(decryptResult));
. k1 g) m1 U4 P
) W5 M# i2 c/ V: N5 `- }
% V" w3 F7 u% |6 ?+ Z - }
复制代码 |
|