AES加解密

    根据加密字符串生成加密密钥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    /**
* 生成加密密钥
* @param key 密钥String
* @return 密钥byte[]
*/
private static SecretKeySpec getKeyBytes(String key){
SecretKeySpec secretKeySpec = null;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
// 强随机数生成器,在Linux等系统生成的key可能是不同的
// SecureRandom secureRandom = new SecureRandom(key.getBytes(StandardCharsets.UTF_8));
// 指定算法名称,不同系统生成相同的key
SecureRandom sha1PRNG = SecureRandom.getInstance("SHA1PRNG");
sha1PRNG.setSeed(key.getBytes(StandardCharsets.UTF_8));
// AES要求密钥的长度为128
keyGenerator.init(128, sha1PRNG);
// 生成一个密钥
SecretKey secretKey = keyGenerator.generateKey();
secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return secretKeySpec;
}

    不过使用以上生成加密密钥的方式我还是出现了错误,在Android端和Linux服务器端生成的加密密钥不一致,最后是迫不得已用了这种方式

1
2
3
4
private static SecretKeySpec getKeyBytes(String key) throws UnsupportedEncodingException {
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
return secretKeySpec;
}

    字节组转换为Base64编码

1
2
3
4
5
6
7
8
/**
* 字节数组转Base64编码
* @param bytes 字节组
* @return base64编码
*/
private static String bytesToBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}

    base64编码转换为字节组

1
2
3
4
5
6
7
8
9
/**
* Base64编码转字节数组
* @param base64 base64编码
* @return 字节组
* @throws IOException 异常抛出
*/
private static byte[] base64ToBytes(String base64) throws IOException {
return Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
}

    加密方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    /**
* 加密
* @param text 需要加密的text内容
* @param key 加密密钥
* @return 加密后的值
*/
public static String encrypt(String text, String key){
String encryptValue = null;
try {
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 初始化密码器为加密模式
cipher.init(Cipher.ENCRYPT_MODE, getKeyBytes(key));
// 获取需要加密的text的字节组
byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);
// 加密
byte[] doFinal = cipher.doFinal(textBytes);
// 加密值转base64编码
encryptValue = bytesToBase64(doFinal);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return encryptValue;
}

    解密方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    /**
* 解密
* @param text 需要解密的base64编码
* @param key 解密密钥值
* @return 解密结果
*/
public static String decrypt(String text, String key){
String decryptValue = null;
try {
// 创建密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 初始化密码器为解密模式
cipher.init(Cipher.DECRYPT_MODE, getKeyBytes(key));
// 解密
byte[] doFinal = cipher.doFinal(base64ToBytes(text));
decryptValue = new String(doFinal);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | IOException e) {
e.printStackTrace();
}
return decryptValue;
}