Loading...

aes 加密,解密

Javaaes加密:

package com.sh.auth.util;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
 * 类名:AESUtil.java 
 * 类说明: AES算法帮助类(PKCS5)
 *  Copyright: Copyright (c) 2012-2015 
 *  Company: HT
 * 
 * @author haoxz
 * @date 2015-2-4
 * @version 1.0
 */
public class AESUtil {
	static final String algorithmStr = "AES/ECB/PKCS5Padding";

	static private KeyGenerator keyGen;

	static private Cipher cipher;

	static boolean isInited = false;

	/**
	  * 方法:init 
	  * 方法说明: 初始化
	  * @author     haoxz
	  * @date       2015-2-5
	 */
	static private void init() {

		try {
			keyGen = KeyGenerator.getInstance("AES");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		keyGen.init(128);

		// 初始化cipher
		try {
			cipher = Cipher.getInstance(algorithmStr);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		isInited = true;
	}

	public static byte[] GenKey() {
		if (!isInited)// 如果没有初始化过,则初始化
		{
			init();
		}
		return keyGen.generateKey().getEncoded();
	}
	/**
	  * 方法:Encrypt 
	  * 方法说明: 加密
	  * @param content 原文
	  * @param keyBytes KEY
	  * @return
	  * @author     haoxz
	  * @date       2015-2-5
	 */
	public static byte[] Encrypt(byte[] content, byte[] keyBytes) {
		byte[] encryptedText = null;

		if (!isInited)// 为初始化
		{
			init();
		}

		Key key = new SecretKeySpec(keyBytes, "AES");

		try {
			cipher.init(Cipher.ENCRYPT_MODE, key);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			encryptedText = cipher.doFinal(content);
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return encryptedText;
	}

	/**
	  * 方法:DecryptToBytes 
	  * 方法说明: 解密
	  * @param content 密文
	  * @param keyBytes KEY
	  * @return
	  * @author     haoxz
	  * @date       2015-2-5
	 */
	public static byte[] DecryptToBytes(byte[] content, byte[] keyBytes) {
		byte[] originBytes = null;
		if (!isInited) {
			init();
		}

		Key key = new SecretKeySpec(keyBytes, "AES");

		try {
			cipher.init(Cipher.DECRYPT_MODE, key);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 解密
		try {
			originBytes = cipher.doFinal(content);
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return originBytes;
	}
	/**
	  * 方法:parseByte2HexStr 
	  * 方法说明: byte数组转16进制字符串
	  * @param byteArray
	  * @return
	  * @author     haoxz
	  * @date       2015-2-5
	 */
	public static String parseByte2HexStr(byte byteArray[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			String hex = Integer.toHexString(byteArray[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}
	
    /**将16进制转换为二进制 
     * @param hexStr 
     * @return 
     */  
    public static byte[] parseHexStr2Byte(String hexStr) {  
            if (hexStr.length() < 1)  
                    return null;  
            byte[] result = new byte[hexStr.length()/2];  
            for (int i = 0;i< hexStr.length()/2; i++) {  
                    int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
                    int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
                    result[i] = (byte) (high * 16 + low);  
            }  
            return result;  
    }  
    
    /**
     * 
      * 方法:DecryptToBytes 
      * 方法说明:解密使用
      * @param content
      * @param key
      * @return
      * @author     wangHao
      * @date       2016年4月21日
     */
    public static String DecryptToString(String content,String key){
        byte[] decryptFrom = parseHexStr2Byte(content);  
        byte[] decryptResult = DecryptToBytes(decryptFrom,key.getBytes()); 
        return new String(decryptResult);
    }
    
    /**
     * 
      * 方法:AecryptToString 
      * 方法说明:加密使用
      * @param content
      * @param key
      * @return
      * @author     wangHao
      * @date       2016年4月21日
     */
    public static String AecryptToString(String content,String key){
        byte[] encryptResult = Encrypt(content.getBytes(), key.getBytes());  
        String encryptResultStr = parseByte2HexStr(encryptResult);  
        return encryptResultStr;
    }
    
	public static void main(String[] args){
	    String content = "wanghao1234567894BmxoVFuzaa924DO";  
	    String password = "hn-cd-0731000100";  
	    //加密   
	    System.out.println("加密前:" + content);  
	    byte[] encryptResult = Encrypt(content.getBytes(), password.getBytes());  
	    String encryptResultStr = parseByte2HexStr(encryptResult);  
	    System.out.println("加密后:" + encryptResultStr);  
	    //解密   
	    byte[] decryptFrom = parseHexStr2Byte("CC743E43BFC58C3B0C2F416B76830CAC10E58116D75F08BA05BD2ECDCA09201D");  
	    byte[] decryptResult = DecryptToBytes(decryptFrom,password.getBytes());  
	    System.out.println("解密后:" + new String(decryptResult)); 
	}
}

  

互联网加密,解密地址:http://www.seacha.com/tools/aes.html?src=403C079051C8EF13DC74B155A1A51C02B6B215DD16AE9C73D693B08D63387283&mode=ECB&keylen=128&key=hn-cd-0731000100&iv=&bpkcs=pkcs5padding&session=v62z4NlztObsfBmJdh3G&aes=dfd8ec5c670388858693c33775683d99&encoding=hex&type=1

如下图:

 

posted @ 2016-05-13 10:18  微笑阳光哈*_*  阅读(290)  评论(0编辑  收藏  举报