1 /** 
 2  * AES加解密 
 3  */  
 4 public class AESHelper {  
 5     final static String AES_KEY = "43hr8fhu34b58123";  
 6   
 7     /** 
 8      * AES加密 
 9      *  
10      * @param text
11      * 待加密字符串 
12      * @return 加密后字符串 
13      */  
14     public static String AESEncrypt(String text) {  
15         try {  
16             String password = AES_KEY;  
17             SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes(), "AES");  
18             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  
19             cipher.init(Cipher.ENCRYPT_MODE, skeySpec);  
20             String strTmp = Base64.encodeToString(cipher.doFinal(text.getBytes()), Base64.DEFAULT);  
21             return strTmp;  
22         } catch (Exception e) {  
23             e.printStackTrace();  
24         }  
25         return text;  
26     }  
27   
28     /** 
29      * AES解密 
30      *  
31      * @param text
32      * 待解密字符串 
33      * @return 解密后字符串 
34      */  
35     public static String aesDecrypt(String text) {  
36         try {  
37             String password = AES_KEY;  
38             SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes(), "AES");  
39             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");  
40             cipher.init(Cipher.DECRYPT_MODE, skeySpec);  
41             String strTmp = new String(cipher.doFinal(Base64.decode(text, Base64.DEFAULT)));  
42             return strTmp;  
43         } catch (Exception ex) {  
44             ex.printStackTrace();  
45         }  
46         return text;  
47     }  
48 } 
Java

相关文章: