AES加密

一、AES是什么

AES高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。2006年,已然成为对称密钥加密中最流行的算法之一。
主要分为五种工作体制:1.电码本模式(Electronic Codebook Book (ECB));2.密码分组链接模式(Cipher Block Chaining (CBC));3.计算器模式(Counter (CTR));4.密码反馈模式(Cipher FeedBack (CFB));5.输出反馈模式(Output FeedBack (OFB))。

二、AES如何使用

import java.util.Arrays;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;

//工具类直接调用即可
public class AESUtil {

	private static byte[] getNRawKey(String seed, int size) throws Exception {
		byte[] s = seed.getBytes("utf-8");
		Random sr = new Random();
		sr.setSeed(Arrays.hashCode(s));
		byte[] r = new byte[size];
		sr.nextBytes(r);
		return r;
	}
	//传入字符串密钥和数据加密,不需要指定Iv(偏移量),返回结果使用hex编码
	public static String nEncrypt(String seed, String cleartext) throws Exception {
		System.out.print(seed + "-");
		byte[] result = nEncrypt(getNRawKey(seed, 16), cleartext.getBytes("utf-8"));
		return Hex.encodeHexString(result);
	}
	//传入字符串密钥和加密后数据解密,不需要指定Iv(偏移量)
	public static String nDecrypt(String seed, String encrypted) throws Exception {
		byte[] r = nDecrypt(getNRawKey(seed, 16), Hex.decodeHex(encrypted.toCharArray()));
		return new String(r, "utf-8");
	}
	//传入字节密钥和数据进行加密,不需要指定Iv(偏移量),返回结果使用hex编码
	public static byte[] nEncrypt(byte[] raw, byte[] clear) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(clear);
		return encrypted;
	}
	//传入字节密钥和加密后数据解密,不需要指定Iv(偏移量
	public static byte[] nDecrypt(byte[] raw, byte[] encrypted) throws Exception {
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] decrypted = cipher.doFinal(encrypted);
		return decrypted;
	}
	//传入字节密钥和数据进行加密,需要指定Iv(偏移量),NoPadding不能自动填补iv和密钥缺少字节
	public static byte[] encryptWithZero(byte[] key, byte[] iv, byte[] input) throws Exception {
		int p = input.length % key.length;
		if (p != 0) {
			byte[] tmp = new byte[input.length + key.length - p];
			System.arraycopy(input, 0, tmp, 0, input.length);
			Arrays.fill(tmp, input.length, tmp.length, (byte) 0);
			input = tmp;
		}

		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(iv);
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
		cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
		byte[] encrypted = cipher.doFinal(input);
		return encrypted;
	}

	private static byte[] unpad0(byte[] in) {
		byte[] upData = in;
		int index = in.length - 1;
		for (; index >= 0; index--) {
			if (in[index] != 0) {
				break;
			}
		}

		if (index != in.length - 1) {
			upData = new byte[index + 1];
			System.arraycopy(in, 0, upData, 0, upData.length);
		}
		return upData;
	}
	//传入字节密钥和数据进行解密,需要指定Iv(偏移量),NoPadding不能自动填补iv和密钥缺少字节
	public static byte[] decryptWithZero(byte[] key, byte[] iv, byte[] input) throws Exception {
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(iv);
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
		byte[] decrypted = cipher.doFinal(input);
		decrypted = unpad0(decrypted);
		return decrypted;
	}
}

三、使用时踩过的坑

如果使用NOPadding,在写密钥和偏移量时,要注意字节大小是16的倍数,否则会报错,建议写的时候使用下面代码查一下字节长度。

		System.out.println("miyao".getBytes().length);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值