AES加密解密 AES/CBC/PKCS5Padding

View Code
 1 import javax.crypto.Cipher;  
2 import javax.crypto.spec.IvParameterSpec;
3 import javax.crypto.spec.SecretKeySpec;
4
5 import sun.misc.BASE64Decoder;
6 import sun.misc.BASE64Encoder;
7
8 /*******************************************************************************
9 * AES加解密算法
10 *
11 * @author arix04
12 *
13 */
14
15 public class AES {
16
17 // 加密
18 public static String Encrypt(String sSrc, String sKey) throws Exception {
19 if (sKey == null) {
20 System.out.print("Key为空null");
21 return null;
22 }
23 // 判断Key是否为16位
24 if (sKey.length() != 16) {
25 System.out.print("Key长度不是16位");
26 return null;
27 }
28 byte[] raw = sKey.getBytes();
29 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
30 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
31 IvParameterSpec iv = new IvParameterSpec("0304050607080910".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
32 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
33 byte[] encrypted = cipher.doFinal(sSrc.getBytes());
34
35 return new BASE64Encoder().encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。
36 }
37
38 // 解密
39 public static String Decrypt(String sSrc, String sKey) throws Exception {
40 try {
41 // 判断Key是否正确
42 if (sKey == null) {
43 System.out.print("Key为空null");
44 return null;
45 }
46 // 判断Key是否为16位
47 if (sKey.length() != 16) {
48 System.out.print("Key长度不是16位");
49 return null;
50 }
51 byte[] raw = sKey.getBytes("ASCII");
52 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
53 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
54 IvParameterSpec iv = new IvParameterSpec("0304050607080910".getBytes());
55 cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
56 byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
57 try {
58 byte[] original = cipher.doFinal(encrypted1);
59 String originalString = new String(original);
60 return originalString;
61 } catch (Exception e) {
62 System.out.println(e.toString());
63 return null;
64 }
65 } catch (Exception ex) {
66 System.out.println(ex.toString());
67 return null;
68 }
69 }
70
71 public static void main(String[] args) throws Exception {
72 /*
73 * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
74 * 此处使用AES-128-CBC加密模式,key需要为16位。
75 */
76 String cKey = "1234567890123456";
77 // 需要加密的字串
78 String cSrc = "Email : arix04@xxx.com";
79 System.out.println(cSrc);
80 // 加密
81 long lStart = System.currentTimeMillis();
82 String enString = AES.Encrypt(cSrc, cKey);
83 System.out.println("加密后的字串是:" + enString);
84
85 long lUseTime = System.currentTimeMillis() - lStart;
86 System.out.println("加密耗时:" + lUseTime + "毫秒");
87 // 解密
88 lStart = System.currentTimeMillis();
89 String DeString = AES.Decrypt(enString, cKey);
90 System.out.println("解密后的字串是:" + DeString);
91 lUseTime = System.currentTimeMillis() - lStart;
92 System.out.println("解密耗时:" + lUseTime + "毫秒");
93 }
94 }

转载自:http://jeffyding.iteye.com/blog/1038616

posted @ 2012-02-29 11:42  月亮的影子  阅读(16709)  评论(1编辑  收藏  举报