• Java-AES加密解密


    package com.jy.demo.web;
    
    
    import java.security.Provider;
    import java.security.SecureRandom;
    import java.security.Security;
    import java.util.Arrays;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class AESUtils{
      public static final String IV = "2015030120123456";
      private static final Logger logger = LoggerFactory.getLogger(AESUtils.class);
    
      public static byte[] aesEncrypt(String content, String pkey)
      {
        try
        {
          SecretKeySpec key = new SecretKeySpec(pkey.getBytes(), "AES");
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          IvParameterSpec iv = new IvParameterSpec("2015030120123456".getBytes());
          cipher.init(1, key, iv);
          return cipher.doFinal(content.getBytes("UTF-8"));
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        return null;
      }
    
      private static SecretKey generateKey(String secretKey)
        throws Exception
      {
        Provider p = Security.getProvider("SUN");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", p);
        secureRandom.setSeed(secretKey.getBytes());
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(secureRandom);
    
        return kg.generateKey();
      }
    
      public static String aesEncryptStr(String content, String pkey)
      {
        byte[] aesEncrypt = aesEncrypt(content, pkey);
        logger.debug("aesEncrypt String:" + Arrays.toString(aesEncrypt));
        String base64EncodeStr = Base64.encodeBase64String(aesEncrypt);
        logger.debug("aesEncrypt base64EncodeStr:" + base64EncodeStr);
        return base64EncodeStr;
      }
    
      public static String aesDecodeStr(String content, String pkey)
        throws Exception
      {
        byte[] base64DecodeStr = Base64.decodeBase64(content);
        logger.debug("base64DecodeStr String:" + Arrays.toString(base64DecodeStr));
        byte[] aesDecode = aesDecode(base64DecodeStr, pkey);
        if (aesDecode == null) {
          return null;
        }
        String result = new String(aesDecode, "UTF-8");
        logger.debug("aesDecode result:" + result);
        return result;
      }
    
      public static byte[] aesDecode(byte[] content, String pkey)
      {
        try
        {
          SecretKeySpec key = new SecretKeySpec(pkey.getBytes(), "AES");
          IvParameterSpec iv = new IvParameterSpec("2015030120123456".getBytes("UTF-8"));
          Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
          cipher.init(2, key, iv);
          return cipher.doFinal(content);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        return null;
      }
      
      public static void main(String[] args) throws Exception {
          System.out.println( AESUtils.aesEncryptStr("123456", "abcdnnnnnn123456"));//根据key加密
          System.out.println( AESUtils.aesDecodeStr("i/urUHvLlepO+NWthBU7AA==", "abcdnnnnnn123456"));//根据加密后密码还有加密key解密
    }
      
    
    }
  • 相关阅读:
    《程序设计与数据结构》(上)课程总结
    20172321 2017-2018-2 《程序设计与数据结构》实验五报告
    20172321 2017-2018-2 《程序设计与数据结构》实验4报告
    20172321 2017-2018-2 《程序设计与数据结构》第11周学习总结
    暑假App
    20172314 2018-2019-1《程序设计与数据结构》查找与排序实验报告
    团队作业——第二周
    安卓游戏开发——团队作业第一周
    20172314 2018-2019-1《程序设计与数据结构》第九周学习总结
    20172314 《程序设计与数据结构》实验报告——树
  • 原文地址:https://www.cnblogs.com/yy123/p/5430236.html
Copyright © 2020-2023  润新知