Java AES加密实现

[java]  view plain copy
  1. import java.security.SecureRandom;  
  2.   
  3. import javax.crypto.Cipher;  
  4. import javax.crypto.KeyGenerator;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.spec.SecretKeySpec;  
  7.   
  8. /** 
  9.  * Usage: 
  10.  *  
  11.  * <pre> 
  12.  * String crypto = AESUtil.encrypt(passwd, cleartext) 
  13.  * ... 
  14.  * String cleartext = AESUtil.decrypt(passwd, crypto) 
  15.  * </pre> 
  16.  *  
  17.  * @author mrsimple 
  18.  */  
  19. public class AESUtil {  
  20.     /** 
  21.      * @Title: encrypt 
  22.      * @Description: AES加密 
  23.      * @param passwd 加密的密钥 
  24.      * @param originStr 需要加密的字符串 
  25.      * @return 返回已加密的字符串 
  26.      * @throws Exception 
  27.      * @throws 
  28.      */  
  29.     public static String encrypt(String passwd, String originStr) throws Exception {  
  30.         byte[] rawKey = getRawKey(passwd.getBytes());  
  31.         byte[] result = encrypt(rawKey, originStr.getBytes());  
  32.         return toHex(result);  
  33.     }  
  34.   
  35.     /** 
  36.      * @Title: decrypt 
  37.      * @Description: AES解密 
  38.      * @param passwd 加密的密钥 
  39.      * @param encrypted 已加密的密文 
  40.      * @return String 返回解密后的数据 
  41.      * @throws Exception 
  42.      * @throws 
  43.      */  
  44.     public static String decrypt(String passwd, String encrypted) throws Exception {  
  45.         byte[] rawKey = getRawKey(passwd.getBytes());  
  46.         byte[] enc = toByte(encrypted);  
  47.         byte[] result = decrypt(rawKey, enc);  
  48.         return new String(result);  
  49.     }  
  50.   
  51.     /** 
  52.      * @Title: getRawKey 
  53.      * @Description: 将密钥转换成字节数组 
  54.      * @param passwd 密钥字符串 
  55.      * @return byte[] 字节数组 
  56.      * @throws Exception 
  57.      * @throws 
  58.      */  
  59.     private static byte[] getRawKey(byte[] passwd) throws Exception {  
  60.         KeyGenerator kgen = KeyGenerator.getInstance("AES");  
  61.         SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");  
  62.         sr.setSeed(passwd);  
  63.         kgen.init(128, sr); // 192 and 256 bits may not be available  
  64.         SecretKey skey = kgen.generateKey();  
  65.         byte[] raw = skey.getEncoded();  
  66.         return raw;  
  67.     }  
  68.   
  69.     /** 
  70.      * @Title: encrypt 
  71.      * @Description: 加密字节数组 
  72.      * @param raw 密钥字节数组 
  73.      * @param originStr 明文字节数组 
  74.      * @return byte[] 加密后的字节数组 
  75.      * @throws Exception 
  76.      * @throws 
  77.      */  
  78.     private static byte[] encrypt(byte[] raw, byte[] originStr) throws Exception {  
  79.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");  
  80.         Cipher cipher = Cipher.getInstance("AES");  
  81.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);  
  82.         byte[] encrypted = cipher.doFinal(originStr);  
  83.         return encrypted;  
  84.     }  
  85.   
  86.     /** 
  87.      * @Title: decrypt 
  88.      * @Description: 解密字节数组 
  89.      * @param passwd 密钥字节数组 
  90.      * @param encrypted 密文字节数组 
  91.      * @return byte[] 解密的字节数组 
  92.      * @throws Exception 
  93.      * @throws 
  94.      */  
  95.     private static byte[] decrypt(byte[] passwd, byte[] encrypted) throws Exception {  
  96.         SecretKeySpec skeySpec = new SecretKeySpec(passwd, "AES");  
  97.         Cipher cipher = Cipher.getInstance("AES");  
  98.         cipher.init(Cipher.DECRYPT_MODE, skeySpec);  
  99.         byte[] decrypted = cipher.doFinal(encrypted);  
  100.         return decrypted;  
  101.     }  
  102.   
  103.     /** 
  104.      * @Title: toHex 
  105.      * @Description:将字符串转换成十六进制字符串 
  106.      * @param txt 
  107.      * @return 
  108.      * @throws 
  109.      */  
  110.     public static String toHex(String txt) {  
  111.         return toHex(txt.getBytes());  
  112.     }  
  113.   
  114.     /** 
  115.      * @Title: fromHex 
  116.      * @Description:  
  117.      *      将十六进制字符串转换成普通字符串 
  118.      *  
  119.      * @param hex 
  120.      * @return       
  121.      * @throws 
  122.      */  
  123.     public static String fromHex(String hex) {  
  124.         return new String(toByte(hex));  
  125.     }  
  126.   
  127.     /** 
  128.      * @Title: toByte 
  129.      * @Description:  
  130.      *      将字符串转换成字节数组  
  131.      *  
  132.      * @param hexString 
  133.      * @return       
  134.      * @throws 
  135.      */  
  136.     public static byte[] toByte(String hexString) {  
  137.         int len = hexString.length() / 2;  
  138.         byte[] result = new byte[len];  
  139.         for (int i = 0; i < len; i++)  
  140.             result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();  
  141.         return result;  
  142.     }  
  143.   
  144.     /** 
  145.      * @Title: toHex 
  146.      * @Description: 
  147.      * @param buf 
  148.      * @return 
  149.      * @throws 
  150.      */  
  151.     public static String toHex(byte[] buf) {  
  152.         if (buf == null)  
  153.             return "";  
  154.         StringBuffer result = new StringBuffer(2 * buf.length);  
  155.         for (int i = 0; i < buf.length; i++) {  
  156.             appendHex(result, buf[i]);  
  157.         }  
  158.         return result.toString();  
  159.     }  
  160.   
  161.     private final static String HEX = "0123456789ABCDEF";  
  162.     /** 
  163.      * @Title: appendHex 
  164.      * @Description:  
  165.      *       
  166.      *  
  167.      * @param sb 
  168.      * @param b       
  169.      * @throws 
  170.      */  
  171.     private static void appendHex(StringBuffer sb, byte b) {  
  172.         sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));  
  173.     }  
  174.   
  175. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值