`
xiang37
  • 浏览: 415107 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

AES对称加密工具类

 
阅读更多

采用AES加密,使用同一个Key可以解密。

 

package com.xiva.common.util;

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 
 * 加密工具类
 * 
 * @author  x0000014553
 * @version  [版本号, 2013-1-8]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class EncryptUtil
{
    
    private static String strKey = "QmxvZ1N5c3RlbQ==";
    
    public static String encryptAES(String str)
    {
        byte[] encryptBytes = null;
        String encryptStr = null;
        try
        {
            Cipher cipher = Cipher.getInstance("AES");
            SecretKey sk = getKey();
            
            // 加密
            cipher.init(Cipher.ENCRYPT_MODE, sk);
            
            encryptBytes = cipher.doFinal(str.getBytes());
            if (encryptBytes != null)
            {
                encryptStr = encryptBASE64(encryptBytes);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return encryptStr;
    }
    
    public static String decryptAES(String str)
    {
        byte[] decryptBytes = null;
        String decryptStr = null;
        try
        {
            Cipher cipher = Cipher.getInstance("AES");
            SecretKey sk = getKey();
            
            // 解密
            cipher.init(Cipher.DECRYPT_MODE, sk);
            
            byte[] scrBytes = decryptBASE64(str);
            
            decryptBytes = cipher.doFinal(scrBytes);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        if (decryptBytes != null)
        {
            decryptStr = new String(decryptBytes);
        }

        return decryptStr;
    }
    
    public static SecretKey getKey()
    {
        SecretKey key = null;
        try
        {
            KeyGenerator generator = KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(strKey.getBytes());
            generator.init(128 , secureRandom);
            key = generator.generateKey();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return key;
    }
    
    /**
     * BASE64加密  
     *   
     * @param key  
     * @return  
     * @throws Exception  
     */
    public static String encryptBASE64(byte[] key)
    {
        return (new BASE64Encoder()).encodeBuffer(key);
    }
    
    /**  
     * BASE64解密
     *   
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key)
    {
        try
        {
            return (new BASE64Decoder()).decodeBuffer(key);
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void main(String[] args)
    {
        String password = "Aa123_";
        String encryptPwd = encryptAES(password);
        System.out.println(encryptPwd);
        String decryptPwd = decryptAES(encryptPwd);
        System.out.println(decryptPwd);
    }
    
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics