Android AES加密解密

补齐方式采用的是:不足16字节,补齐内容为差值(比如数据是10个字节,补齐的内容就是6)。

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

/**
 * AES加密解密工具
 * 
 * @author yangle
 */
public class AESUtils {

	/**
	 * AES加密
	 * 
	 * @param data
	 *            将要加密的内容
	 * @param key
	 *            密钥
	 * @return 已经加密的内容
	 */
	public static byte[] encrypt(byte[] data, byte[] key) {
		//不足16字节,补齐内容为差值
		int len = 16 - data.length % 16;
		for (int i = 0; i < len; i++) {
			byte[] bytes = { (byte) len };
			data = ArrayUtils.concat(data, bytes);
		}
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
			return cipher.doFinal(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new byte[] {};
	}

	/**
	 * AES解密
	 * 
	 * @param data
	 *            将要解密的内容
	 * @param key
	 *            密钥
	 * @return 已经解密的内容
	 */
	public static byte[] decrypt(byte[] data, byte[] key) {
		data = ArrayUtils.noPadding(data, -1);
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] decryptData = cipher.doFinal(data);
			int len = 2 + ByteUtils.byteToInt(decryptData[4]) + 3;
			return ArrayUtils.noPadding(decryptData, len);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new byte[] {};
	}
}
合并数组与去除补齐工具
/**
 * 数组工具
 * 
 * @author yangle
 */
public class ArrayUtils {

	/**
	 * 合并数组
	 * 
	 * @param firstArray
	 *            第一个数组
	 * @param secondArray
	 *            第二个数组
	 * @return 合并后的数组
	 */
	public static byte[] concat(byte[] firstArray, byte[] secondArray) {
		if (firstArray == null || secondArray == null) {
			return null;
		}
		byte[] bytes = new byte[firstArray.length + secondArray.length];
		System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
		System.arraycopy(secondArray, 0, bytes, firstArray.length,
				secondArray.length);
		return bytes;
	}

	/**
	 * 去除数组中的补齐
	 * 
	 * @param paddingBytes
	 *            源数组
	 * @param dataLength
	 *            去除补齐后的数据长度
	 * @return 去除补齐后的数组
	 */
	public static byte[] noPadding(byte[] paddingBytes, int dataLength) {
		if (paddingBytes == null) {
			return null;
		}

		byte[] noPaddingBytes = null;
		if (dataLength > 0) {
			if (paddingBytes.length > dataLength) {
				noPaddingBytes = new byte[dataLength];
				System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, dataLength);
			} else {
				noPaddingBytes = paddingBytes;
			}
		} else {
			int index = paddingIndex(paddingBytes);
			if (index > 0) {
				noPaddingBytes = new byte[index];
				System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, index);
			}
		}

		return noPaddingBytes;
	}

	/**
	 * 获取补齐的位置
	 * 
	 * @param paddingBytes
	 *            源数组
	 * @return 补齐的位置
	 */
	private static int paddingIndex(byte[] paddingBytes) {
		for (int i = paddingBytes.length - 1; i >= 0; i--) {
			if (paddingBytes[i] != 0) {
				return i + 1;
			}
		}
		return -1;
	}
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值