AES 加密解密

/*     */ package cn.sccl.utils;
/*     */ 
/*     */ import java.io.PrintStream;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.spec.IvParameterSpec;
/*     */ import javax.crypto.spec.SecretKeySpec;
/*     */ import org.apache.commons.codec.binary.Base64;
/*     */ import org.apache.commons.codec.digest.DigestUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AESCryptUtils
/*     */ {
/*     */   private static final String IV_PARAMETER = "176d3805f01deeab";
/*     */   private static final String CHARSET_NAME = "UTF-8";
/*     */   private static final String KEY_ALGORITHM = "AES";
/*     */   private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
/*     */   
/*     */   public AESCryptUtils() {}
/*     */   
/*     */   public static String encode(String data, String key, String iv)
/*     */   {
/*  33 */     return encrypt(data, key, iv);
/*     */   }
/*     */   
/*     */   public static String encode(String data, String key) {
/*  37 */     return encrypt(data, md5Hex16(key), IV_PARAMETER);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String decode(String data, String key, String iv)
/*     */   {
/*  49 */     return decrypt(data, key, iv);
/*     */   }
/*     */   
/*     */   public static String decode(String data, String key) {
/*  53 */     return decrypt(data, md5Hex16(key), IV_PARAMETER);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String md5Hex16(String key)
/*     */   {
/*  63 */     return DigestUtils.md5Hex(key.getBytes()).substring(8, 24);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String encrypt(String data, String md5Key, String iv)
/*     */   {
/*     */     try
/*     */     {
/*  76 */       IvParameterSpec zeroIv = new IvParameterSpec(iv.getBytes());
/*     */       
/*  78 */       SecretKeySpec key = new SecretKeySpec(md5Key.getBytes(), KEY_ALGORITHM );
/*     */       
/*  80 */       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
/*     */       
/*  82 */       cipher.init(1, key, zeroIv);
/*     */       
/*  84 */       byte[] encryptedData = cipher.doFinal(data.getBytes("UTF-8"));
/*  85 */       return Base64.encodeBase64String(encryptedData);
/*     */     } catch (Exception e) {
/*  87 */       e.printStackTrace(); }
/*  88 */     return "";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String decrypt(String data, String strKey, String iv)
/*     */   {
/*     */     try
/*     */     {
/* 102 */       byte[] byteMi = Base64.decodeBase64(data);
/*     */       
/* 104 */       IvParameterSpec zeroIv = new IvParameterSpec(iv.getBytes());
/* 105 */       SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), KEY_ALGORITHM );
/* 106 */       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
/*     */       
/* 108 */       cipher.init(2, key, zeroIv);
/* 109 */       byte[] decryptedData = cipher.doFinal(byteMi);
/* 110 */       return new String(decryptedData, "UTF-8");
/*     */     } catch (Exception e) {
/* 112 */       e.printStackTrace(); }
/* 113 */     return "";
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 125 */     String IV_PARAMETER = "941c2d70a830c950";
/* 126 */     String content = "{\"deviceId\":\"MC93c9c2d8ac435db6019ba619d6a77d\",\"sequence\":\"167783\",\"time\":1515582588066,\"version\":\"XXX.YYY.ZZZ.MMM\"}";
/* 127 */     String key = "941c2d70a830c950";
/*     */     
/* 129 */     System.out.println("16位md5密钥:" + md5Hex16(key));
/*     */     
/* 131 */     System.out.println("初始向量:" + IV_PARAMETER);
/*     */     
/* 133 */     System.out.println("加密前:" + content);
/* 134 */     String encryptResult = encode(content, key, IV_PARAMETER);
/*     */     
/* 136 */     System.out.println("加密后:" + new String(encryptResult));
/*     */     
/* 138 */     String decryptResult = decode(encryptResult, key, IV_PARAMETER);
/* 139 */     System.out.println("解密后:" + new String(decryptResult));
/*     */   }
/*     */ }

/* Location:           D:\eclipse_workspace1\iotportal\src\main\webapp\WEB-INF\lib\iotportal-core.jar
 * Qualified Name:     cn.sccl.utils.AESCryptUtils
 * Java Class Version: 7 (51.0)
 * JD-Core Version:    0.7.0.1
 */

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值