JAVA 에서 AES 암호 화 및 복호화

3171 단어
package com.unionx.wanxue.utils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesUtil {     
	//     Key 
	public static final String localkey="q1w2e3r4t5y6u7i8";
	//   
	public static String Encrypt(String content, String key) {
		try {
			if (key == null) {
				System.out.print("Key  null");
				return null;
			}
			//   Key   16 
			if (key.length() != 16) {
				System.out.print("Key    16 ");
				return null;
			}
			if(key==localkey){
				byte[] raw = key.getBytes();
				SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				IvParameterSpec iv = new IvParameterSpec("0000000000123456".getBytes());
				cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
				byte[] encrypted = cipher.doFinal(content.getBytes());
				String hexStr =byte2hex(encrypted);
				return hexStr;
			}
			else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//   
	public static String Decrypt(String content, String key) {
		try {
			//   Key    
			if (key == null) {
				System.out.print("Key  null");
				return null;
			}
			//   Key   16 
			if (key.length() != 16) {
				System.out.print("Key    16 ");
				return null;
			}
			if(key==localkey){
				byte[] raw = key.getBytes("ASCII");
				SecretKeySpec keySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				IvParameterSpec iv = new IvParameterSpec("0000000000123456".getBytes());
				cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
				byte[] encrypted1 = hex2byte(content);
				byte[] original = cipher.doFinal(encrypted1);
				String originalString = new String(original);
				return originalString;
			}else{
				return null;
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
			return null;
		}
	}
	//    
	public static byte[] hex2byte(String strhex) {
		if (strhex == null) {
			return null;
		}
		int l = strhex.length();
		if (l % 2 == 1) {
			return null;
		}
		byte[] b = new byte[l / 2];
		for (int i = 0; i != l / 2; i++) {
			b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),
					16);
		}
		return b;
	}
	//    
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
	//    
	public static void main(String[] args) throws Exception {
		String cSrc = "[{'taskUrl':'http://192.168.1.178:8080/wanxue/switch/list'}]";
		System.out.println("    :"+cSrc);
		//  
		String enString = AesUtil.Encrypt(cSrc, localkey);
		System.out.println("       :" + enString);
		//   
		String DeString = AesUtil.Decrypt(enString, localkey);
		System.out.println("       :" + DeString);
	} 
}

좋은 웹페이지 즐겨찾기