public class CommonUtil {
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
// System.out.println("load BouncyCastleProvider failed: " + e);
}
}
/**
* 生成随机数
*
* @return int 随机数
*/
public static int rand() {
SecureRandom sr = new SecureRandom();
return sr.nextInt();
}
/**
* BASE64 编码
*
* @param data 原始数据
* @return String 编码后的字符串
* @throws Exception 字符串转换时可能引发的异常
*/
public static String encode(byte[] data) throws Exception {
return new String(Base64.encode(data));
}
/**
* BASE64 解码
*
* @param text BASE64编码的数据
* @return byte[] 解码后的数据
* @throws Exception 进行BASE64解码时可能产生异常
*/
public static byte[] decode(String text) throws Exception {
return Base64.decode(text);
}
/**
* HEX(16进制) 编码
*
* @param data 原始数据
* @return String 编码后的字符串
* @throws Exception 进行字符串转换时可能产生异常
*/
public static String hex(byte[] data) throws Exception {
return new String(Hex.encode(data));
}
/**
* HEX(16进制) 解码
*
* @param text HEX字符串
* @return byte[] 解码后的数据
* @throws Exception 进行BASE64解码时或者字符串转换时可能产生异常
*/
public static byte[] unhex(String text) throws Exception {
return Hex.decode(text.getBytes());
}
/**
* MD5 摘要算法
*
* @param data 原始数据
* @return byte[] 摘要数据
* @throws Exception 可能抛出参数无效或算法相关的异常
*/
public static byte[] md5(byte[] data) throws Exception {
MD5Digest md5 = new MD5Digest();
int outLen = md5.getDigestSize();
byte[] out = new byte[outLen];
md5.update(data, 0, data.length);
md5.doFinal(out, 0);
return out;
}
/**
* SHA1 摘要算法
*
* @param data 原始数据
* @return byte[] 摘要数据
* @throws Exception 可能抛出参数无效或算法相关的异常
*/
public static byte[] sha1(byte[] data) throws Exception {
SHA1Digest sha1 = new SHA1Digest();
int outLen = sha1.getDigestSize();
byte[] out = new byte[outLen];
sha1.update(data, 0, data.length);
sha1.doFinal(out, 0);
return out;
}
/**
* AES 加密算法
*
* @param plainText 明文
* @param password 密码
* @return byte[] 密文
* @throws Exception 数据、算法相关的异常
*/
public static byte[] aesEncrypt(byte[] plainText, String password) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// kgen.init(128, new SecureRandom(password.getBytes()));
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plainText);
}
/**
* AES 解密算法
*
* @param cipherData 密文
* @param password 密码
* @return byte[] 明文
* @throws Exception 数据、算法相关的异常
*/
public static byte[] aesDecrypt(byte[] cipherData, String password) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
//kgen.init(128, new SecureRandom(password.getBytes()));
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(cipherData);
}
/**
* 读取文件所有内容
*
* @param filename 文件名
* @return String 文件内容
* @throws Exception 可能触发文件访问相关的异常
*/
public static byte[] getFileContents(String filename) throws Exception {
FileInputStream fis = new FileInputStream(filename);
int count = fis.available();
byte[] data = new byte[count];
fis.read(data);
fis.close();
return data;
}
}