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);
    }
    
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值