一、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;
}
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);
}
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");
}
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;
}
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;
}
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;
}
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);
|