Java利用 AES/ECB/PKCS5Padding 算法加解密

Java利用  AES/ECB/PKCS5Padding 算法加解密。

[java]  view plain  copy
  1. package com.newland.njwpayment.utils;  
  2.   
  3. import javax.crypto.Cipher;  
  4. import javax.crypto.spec.SecretKeySpec;  
  5.   
  6. import Decoder.BASE64Decoder;  
  7. import Decoder.BASE64Encoder;  
  8.   
  9. /** 
  10.  * AES加解密 
  11.  * 
  12.  * Created by yyh on 2015/10/9. 
  13.  */  
  14. public class AESUtils {  
  15.   
  16.     /** 
  17.      * 密钥算法 
  18.      */  
  19.     private static final String ALGORITHM = "AES";  
  20.     /** 
  21.      * 加解密算法/工作模式/填充方式 
  22.      */  
  23.     private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";  
  24.   
  25.     /** 
  26.      * SecretKeySpec类是KeySpec接口的实现类,用于构建秘密密钥规范 
  27.      */  
  28.     private SecretKeySpec key;  
  29.   
  30.     public AESUtils(String hexKey) {  
  31.         key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM);  
  32.     }  
  33.   
  34.     /** 
  35.      * AES加密 
  36.      * @param data 
  37.      * @return 
  38.      * @throws Exception 
  39.      */  
  40.     public String encryptData(String data) throws Exception {  
  41.         Cipher cipher = Cipher.getInstance(ALGORITHM_STR); // 创建密码器  
  42.         cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化  
  43.         return new BASE64Encoder().encode(cipher.doFinal(data.getBytes()));  
  44.     }  
  45.   
  46.     /** 
  47.      * AES解密 
  48.      * @param base64Data 
  49.      * @return 
  50.      * @throws Exception 
  51.      */  
  52.     public String decryptData(String base64Data) throws Exception{  
  53.         Cipher cipher = Cipher.getInstance(ALGORITHM_STR);  
  54.         cipher.init(Cipher.DECRYPT_MODE, key);  
  55.         return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(base64Data)));  
  56.     }  
  57.   
  58.     /** 
  59.      * hex字符串 转 byte数组 
  60.      * @param s 
  61.      * @return 
  62.      */  
  63.     private static byte[] hex2byte(String s) {  
  64.         if (s.length() % 2 == 0) {  
  65.             return hex2byte (s.getBytes(), 0, s.length() >> 1);  
  66.         } else {  
  67.             return hex2byte("0"+s);  
  68.         }  
  69.     }  
  70.   
  71.     private static byte[] hex2byte (byte[] b, int offset, int len) {  
  72.         byte[] d = new byte[len];  
  73.         for (int i=0; i<len*2; i++) {  
  74.             int shift = i%2 == 1 ? 0 : 4;  
  75.             d[i>>1] |= Character.digit((char) b[offset+i], 16) << shift;  
  76.         }  
  77.         return d;  
  78.     }  
  79.   
  80.     public static void main(String[] args) throws Exception {  
  81.         AESUtil util = new AESUtil("abcdefghijklmnop"); // 密钥  
  82.         System.out.println("cardNo:"+util.encryptData("1234")); // 加密  
  83.         System.out.println("exp:"+util.decryptData("34+Jzs4KkwaCQWVyyAgwLA==")); // 解密  
  84.     }  
  85. }  

其中,BASE64Decoder与BASE64Encoder这两个类可以通过Jre System Library下面的rt.jar找到,这里顺便提及一下,sun.misc.BASE64Encoder找不到jar包的两种解决方法:

1、直接添加JRE System Library,在Eclipse里面项目右击->Java build path->JRE System Library...

2、导入已提取封装好的jar包。下载地址:sun.misc.BASE64Decoder.jar 下载

另外,还需要注意的是,AES密钥一般是16个字节为一块,然后对这一整块进行加密,如果不够16个字节,就需要补位,也就是16字节对齐。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值