java使用AES加密

java使用AES加密 

java使用AES加密


[java]  view plain copy
  1. package com.jetsum.util;  
  2. import java.security.InvalidAlgorithmParameterException;  
  3. import java.security.InvalidKeyException;  
  4. import java.security.NoSuchAlgorithmException;  
  5. import java.security.SecureRandom;  
  6. import java.security.spec.AlgorithmParameterSpec;  
  7.   
  8. import javax.crypto.BadPaddingException;  
  9. import javax.crypto.Cipher;  
  10. import javax.crypto.IllegalBlockSizeException;  
  11. import javax.crypto.KeyGenerator;  
  12. import javax.crypto.NoSuchPaddingException;  
  13. import javax.crypto.SecretKey;  
  14. import javax.crypto.spec.IvParameterSpec;  
  15.   
  16. /** 
  17.  * 通过AES算法对文本进行加密解密 
  18.  * @author ShaoJiang 
  19.  * 
  20.  */  
  21. public class AESUtil {  
  22.     private static byte[] keyValue = new byte[] {       //用户密钥  
  23.         22,25,-35,-45,25,98,-55,-45,10,35,-45,25,  
  24.         26,-95,25,-65,-78,-99,85,45,-62,10,-0,11,  
  25.         -35,48,-98,65,-32,14,-78,25,36,-56,-45,-45,  
  26.         12,15,-35,-75,15,-14,62,-25,33,-45,55,68,-88  
  27.     };  
  28.     private static byte[] iv = new byte[] {             //算法参数  
  29.         -12,35,-25,65,45,-87,95,-22,-15,45,55,-66,32,5-4,84,55  
  30.     };  
  31.     private static SecretKey key;                       //加密密钥  
  32.     private static AlgorithmParameterSpec paramSpec;    //算法参数  
  33.     private static Cipher ecipher;                      //加密算法  
  34.       
  35.     static{       
  36.         KeyGenerator kgen;  
  37.         try {  
  38.             //为指定算法生成一个密钥生成器对象。  
  39.             kgen = KeyGenerator.getInstance("AES");  
  40.             //使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥长度。  
  41.             kgen.init(128new SecureRandom(keyValue));  
  42.             //使用KeyGenerator生成(对称)密钥。  
  43.             key = kgen.generateKey();  
  44.             //使用iv中的字节作为IV来构造一个 算法参数。  
  45.             paramSpec = new IvParameterSpec(iv);  
  46.             //生成一个实现指定转换的 Cipher 对象  
  47.             ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");         
  48.         } catch (NoSuchAlgorithmException e) {  
  49.             e.printStackTrace();  
  50.         } catch (NoSuchPaddingException e) {  
  51.             e.printStackTrace();  
  52.         }  
  53.     }  
  54.   
  55.     /** 
  56.      * 加密,使用指定数据源生成密钥,使用用户数据作为算法参数进行AES加密 
  57.      * @param msg 加密的数据 
  58.      * @return 
  59.      */  
  60.     public static String encrypt(String msg) {  
  61.         String str = "";  
  62.         try {             
  63.             //用密钥和一组算法参数初始化此 cipher  
  64.             ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);  
  65.             //加密并转换成16进制字符串  
  66.             str = asHex(ecipher.doFinal(msg.getBytes()));  
  67.         } catch (BadPaddingException e) {  
  68.             e.printStackTrace();  
  69.         } catch (InvalidKeyException e) {  
  70.             e.printStackTrace();  
  71.         } catch (InvalidAlgorithmParameterException e) {  
  72.             e.printStackTrace();  
  73.         } catch (IllegalBlockSizeException e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.         return str;  
  77.     }  
  78.       
  79.     /** 
  80.      * 解密,对生成的16进制的字符串进行解密 
  81.      * @param value 解密的数据 
  82.      * @return 
  83.      */  
  84.     public static String decrypt(String value) {  
  85.         try {  
  86.             ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec);  
  87.             return new String(ecipher.doFinal(asBin(value)));  
  88.         } catch (BadPaddingException e) {  
  89.             e.printStackTrace();  
  90.         } catch (InvalidKeyException e) {  
  91.             e.printStackTrace();  
  92.         } catch (InvalidAlgorithmParameterException e) {  
  93.             e.printStackTrace();  
  94.         } catch (IllegalBlockSizeException e) {  
  95.             e.printStackTrace();  
  96.         }  
  97.         return "";  
  98.     }  
  99.   
  100.     /** 
  101.      * 将字节数组转换成16进制字符串 
  102.      * @param buf 
  103.      * @return 
  104.      */  
  105.     private static String asHex(byte buf[]) {  
  106.         StringBuffer strbuf = new StringBuffer(buf.length * 2);  
  107.         int i;  
  108.         for (i = 0; i < buf.length; i++) {  
  109.             if (((int) buf[i] & 0xff) < 0x10)//小于十前面补零  
  110.                 strbuf.append("0");  
  111.             strbuf.append(Long.toString((int) buf[i] & 0xff16));  
  112.         }  
  113.         return strbuf.toString();  
  114.     }  
  115.   
  116.     /** 
  117.      * 将16进制字符串转换成字节数组 
  118.      * @param src 
  119.      * @return 
  120.      */  
  121.     private static byte[] asBin(String src) {  
  122.         if (src.length() < 1)  
  123.             return null;  
  124.         byte[] encrypted = new byte[src.length() / 2];  
  125.         for (int i = 0; i < src.length() / 2; i++) {  
  126.             int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);//取高位字节  
  127.             int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);//取低位字节  
  128.             encrypted[i] = (byte) (high * 16 + low);  
  129.         }  
  130.         return encrypted;  
  131.     }  
  132. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值