zoukankan      html  css  js  c++  java
  • 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解密
    }
      
    
    }
  • 相关阅读:
    某电校园网
    M100(3) 无线数传
    【转】大厦将倾,互联网将如何变革传统行业(下)
    【转】大厦将倾,互联网将如何变革传统行业(上)
    【转】用户十秒离开你网站的25个原因
    web及移动应用测试知识总结
    【转】Watir, Selenium & WebDriver
    ICMP协议
    我不会OOO,仍然可以XXX_转
    查看网络连接数目(解决TIME_WAIT过多造成的问题_转)
  • 原文地址:https://www.cnblogs.com/yy123/p/5430236.html
Copyright © 2011-2022 走看看