JAVA ASE 数据加密, 数据解密

之前也是在网上找了很久, 但是终不能用, 后来突然发现根本不要这么复杂, 今天把这个记下, 方便大家参考:

// 代码由于是公司里的, 有些不能贴上来, 但是核心代码是独立可用的
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//上面是引用的类


/**
	 * 加密数据
	 * @param content 需要加密的内容
	 * @param password 加密密码
	 * @return
	 */
	public static byte[] encryptForAES(byte[] content, String password) {
		if(content == null || content.length == 0 || password == null) return null;
		try {//不需要这么复杂的key
			//KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//kgen.init(128, new SecureRandom(password.getBytes("utf-8")));
			//SecretKey secretKey = kgen.generateKey();
			//byte[] enCodeFormat = secretKey.getEncoded();
			byte[] enCodeFormat = getBytesForUTF8(password);
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器,ECB模式,PKCS5Padding填充方式 AES/ECB/PKCS5Padding
			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密数据
	 * @param content  待解密内容
	 * @param password 解密密钥
	 * @return
	 */
	public static byte[] decryptForAES(byte[] content, String password) {
		if(content == null || content.length == 0 || password == null) return null;
		try {//不需要这么复杂的key
			//KeyGenerator kgen = KeyGenerator.getInstance("AES");
			//kgen.init(128, new SecureRandom(password.getBytes("utf-8")));
			//SecretKey secretKey = kgen.generateKey();
			//byte[] enCodeFormat = secretKey.getEncoded();
			byte[] enCodeFormat = getBytesForUTF8(password);
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器 AES/ECB/PKCS5Padding
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式
			byte[] result = cipher.doFinal(content);
			return result; // 解密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

 

你可能感兴趣的:(java)