加密算法--AES

java实现AES加密算法,随机iv值合入到加密文件中,使得每次加密的内容不一样。
核心代码如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public static final int AES_KEY_SIZE = 256; // in bits
public static final int GCM_NONCE_LENGTH = 12; // in bytes
public static final String CIPHER = "AES";
public static final String AES_GCM_NOPADDING = "AES/GCM/NoPadding";// java 8-u162 以后版本才支持
public static final String AES_GCM_PKCS5PADDING = "AES/GCM/PKCS5Padding";// java 8-u162 以后版本才支持
public static final String AES_CBC_NOPADDING = "AES/CBC/NoPadding";
public static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5Padding";
/**
* 生成秘钥的方法
*
* @param encryptPass
* @return
*/
public static SecretKey getSecreKey(String encryptPass) {
SecretKey key = null;
try {
KeyGenerator keyGen = KeyGenerator.getInstance(CIPHER);
keyGen.init(AES_KEY_SIZE, new SecureRandom(encryptPass.getBytes(StandardCharsets.UTF_8)));
key = keyGen.generateKey();
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return key;
}
/**
* AES加密
*
* @param content
* @param encryptPass
* @return
*/
public static String encrypt(String content, String encryptPass) {
try {
Cipher cipher = Cipher.getInstance(AES_GCM_PKCS5PADDING);
cipher.init(Cipher.ENCRYPT_MODE, getSecreKey(encryptPass));
byte[] iv = cipher.getIV();
byte[] encryptData = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
byte[] message = new byte[12 + content.getBytes(StandardCharsets.UTF_8).length + 16];
System.arraycopy(iv, 0, message, 0, 12);
System.arraycopy(encryptData, 0, message, 12, encryptData.length);
return Base64.getEncoder().encodeToString(message);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException
| InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
/**
* AES解密
*
* @param base64Content
* @param encryptPass
* @return
* @throws IllegalBlockSizeException
*/
public static String decrypt(String base64Content, String encryptPass) throws IllegalBlockSizeException {
byte[] content = Base64.getDecoder().decode(base64Content);
if (content.length < 12 + 16) {
throw new IllegalBlockSizeException();
}
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, content, 0, 12);
try {
Cipher cipher = Cipher.getInstance(AES_GCM_PKCS5PADDING);
cipher.init(Cipher.DECRYPT_MODE, getSecreKey(encryptPass), spec);
byte[] decryptData = cipher.doFinal(content, 12, content.length - 12);
return new String(decryptData);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}

注意:GCM模式java 8-u162 版本以后才支持,以前的版本不能使用该加密模式。
不建议使用ECB模式,ECB模式现阶段存在破解的可能。