简介
xxx
处理
package aes;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Created on 2019/3/12.
*
* @author 郑少鹏
* @desc AES处理
*/
public class AesHandle {
private static final String KEY_ALGORITHM = "AES";
/**
* 默加密算法
*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
/**
* 加密
*
* @param origin 明文字节数组
* @param password password须16字节(与iOS统一)
* @return 字节数组
*/
public static byte[] encrypt(byte[] origin, String password) {
try {
byte[] enCodeFormat = password.getBytes();
// 转为AES专用密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
// 创密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 初始化为加密模式密码器
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(origin);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param encrypt 密文字节数组
* @param password password须16字节(与iOS统一)
* @return 字节数组
*/
public static byte[] decrypt(byte[] encrypt, String password) {
try {
byte[] enCodeFormat = password.getBytes();
// 转为AES专用密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
// 创密码器
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
// 初始化为解密模式密码器
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encrypt);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}