public class EncryptionUtil {
private final static char[] chArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private final static byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
//md5加密
public static String Md5Encode(@NonNull String data) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
md.reset();
md.update(dataBytes);
byte[] resBytes = md.digest();
return getResultStr(resBytes);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
//获取文件的MD5
public static String getFileMD5(@NonNull String fPath) {
File file = new File(fPath);
if (!file.exists()) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(file);
int len = 0;
while ((len = fis.read(buffer)) != -1) {
md.update(buffer, 0, len);
}
fis.close();
byte[] resultBytes = md.digest();
return getResultStr(resultBytes);
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
return null;
}
private static String getResultStr(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
char[] result = new char[bytes.length * 2];
for (int i = 0, k = 0; i < bytes.length; i++) {
result[k++] = chArray[bytes[i] >>> 4 & 0xf];
result[k++] = chArray[bytes[i] & 0xf];
}
return new String(result);
}
//base64加密
public static String base64Encode(@NonNull String data) {
return Base64.encodeToString(data.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
}
//base64解密
public static String base64Decode(@NonNull String data) {
return new String(Base64.decode(data.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT));
}
//DES加密,AES算法类似
public static byte[] DESEncode(@NonNull String data, @NonNull String encryptKey) {
IvParameterSpec zeroIv = new IvParameterSpec(iv);
SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
try {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException
| BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
//DES解密,AES算法类似
public static byte[] DESDecode(String data, @NonNull String decryptKey) {
IvParameterSpec zeroIv = new IvParameterSpec(iv);
SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
try {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidAlgorithmParameterException | InvalidKeyException
| BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param data 待加密的数据
* @param publicKey 用来加密的公钥
* @return byte[]
*/
public static byte[] RASEncode(@NonNull String data, @NonNull byte[] publicKey) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
try {
PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
} catch (InvalidKeySpecException | NoSuchAlgorithmException
| NoSuchPaddingException | InvalidKeyException
| BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param data 待解密的数据
* @param privateKey 私钥,用来解密
* @return byte[]
*/
public static byte[] RASDecode(@NonNull String data, @NonNull byte[] privateKey) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(privateKey);
try {
PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
} catch (InvalidKeySpecException | NoSuchAlgorithmException
| NoSuchPaddingException | InvalidKeyException
| BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
}