AES加密解密工具类

package com.java.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.io.Charsets;
import org.apache.tomcat.util.codec.binary.Base64;

/**
 * @author jiangli
 * AES加密解密工具类
 * @date 2018/5/8 10:46
 */

public class AESUtils {

	//加密  input要加密的明文数据  key加密规则
	public static String encrypt(String input, String key) {
		byte[] crypted = null;
		try {
			SecretKeySpec skey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skey);
			crypted = cipher.doFinal(input.getBytes(Charsets.UTF_8));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(Base64.encodeBase64(crypted));
	}

	//解密  input要解密的密文数据  key解密规则
	public static String decrypt(String input, String key) {
		byte[] output = null;
		try {
			SecretKeySpec skey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skey);
			output = cipher.doFinal(Base64.decodeBase64(input));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(output);
	}

}
	public static void main(String[] args) {
		String key = "802CE4BBD34F1B65";
		JSONObject jo = new JSONObject();
		jo.put("couponNo", "858301521039");   //卡卷兑换码
		jo.put("partnerOrderId", "8583418572059");  //订单号
		jo.put("outletId", "719863470568243456");  //网点id
		String data = jo.toString();
		data = AESUtils.encrypt(data, key);
		System.out.println(data);
	}


阅读更多
换一批

没有更多推荐了,返回首页