AES加密使用

AES加密使用

import java.security.SecureRandom;

import javax.crypto.*;
import javax.crypto.spec.*;

public class AesCrypto {
	
	/**将二进制转换成16进制
	* @param buf
	* @return
	*/
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[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;
	}	

	
	/** 
     * 加密 
     * @param content 需要加密的内容 
     * @param password  加密密码 
     * @return String 十进制数字字符串
     */  
    public static String encrypt(String content, String password) {
    	String result = null;
    	try {
    		// Get the KeyGenerator
    		KeyGenerator kgen = KeyGenerator.getInstance("AES");
    		//kgen.init(128); // 192 and 256 bits may not be available
    		kgen.init(128, new SecureRandom(password.getBytes()));
    		
    		// Generate the secret key specs.
    		SecretKey skey = kgen.generateKey();
    		byte[] raw = skey.getEncoded();
    		
    		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    		
    		// Instantiate the cipher(创建密码器)
    		Cipher cipher = Cipher.getInstance("AES");
    		
    		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    		byte[] encrypted = cipher.doFinal(content.getBytes());
    		
    		result =parseByte2HexStr(encrypted);
    		
    		//System.out.println("加密的结果:" + result);
    		
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("加密:出现错误!!!");
		}
    	
		return result;
    }
    
    
    /**解密 
     * @param content  待解密内容 
     * @param password 解密密钥 
     * @return String 字符串
     */  
    public static String decrypt(String content, String password) {
    	String result = null;
    	try {
    		// Get the KeyGenerator
    		KeyGenerator kgen = KeyGenerator.getInstance("AES");
    		//kgen.init(128); // 192 and 256 bits may not be available
    		kgen.init(128, new SecureRandom(password.getBytes()));
    		
    		// Generate the secret key specs.
    		SecretKey skey = kgen.generateKey();
    		byte[] raw = skey.getEncoded();
    		
    		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    		
    		// Instantiate the cipher(创建密码器)
    		Cipher cipher = Cipher.getInstance("AES");
    		
    		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    		
    		byte[] encrypted = cipher.doFinal(parseHexStr2Byte(content));
    		
    		result =new String(encrypted);    		
    		//System.out.println("解密结果:" + result);
    		
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("解密:出现错误!!!");
		}
		return result;
    }
    
	public static void main(String[] args) throws Exception {

		String content ="TEST";
		String password ="123455";
		
		System.out.println("====================================");
		System.out.println("原文:" + content);
		System.out.println("密匙:" + password);
		
		System.out.println("====================================");
		String en =AesCrypto.encrypt(content, password);
		System.out.println("加密:" + en);
	
		System.out.println("====================================");
		String de =AesCrypto.decrypt(en, "123455");
		System.out.println("解密:" + de);
		
		//System.out.println("原文长度: " + content.length());
		

		
		
	}
	
    

}

 

 

 

你可能感兴趣的:(aes)