MD5工具类(MD5是编码方式):
public class MD5Utils {
/**
* 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合
*/
protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};//可改自己定义的
/**
* 生成字符串的md5校验值
*
* @param s
* @return
*/
public static String MD5(String s) {
return MD5(s.getBytes());
}
/**
* 生成输入流的md5校验值,注意,<b>调用该方法后应该自己关闭输入流</b>
*
* @param file
* @return
* @throws IOException
*/
public static String MD5(InputStream inputStream) {
if (inputStream != null) {
try {
MessageDigest l_messagedigest = MessageDigest.getInstance("MD5");//获取MD5的消息摘要实体
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = inputStream.read(buffer)) > 0) {
l_messagedigest.update(buffer, 0, numRead);//往摘要信息里添加数据
}
return bufferToHex(l_messagedigest.digest());//获取摘要
} catch (Exception e) {
e.printStackTrace();
}
}
return "";
}
/**
* 生成文件的md5校验值
*
* @param file
* @return
* @throws IOException
*/
public static String MD5(File file) {
InputStream fis = null;
try {
fis = new FileInputStream(file);
return MD5(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
public static String MD5(byte[] bytes) {
try {
MessageDigest l_messagedigest = MessageDigest.getInstance("MD5");
l_messagedigest.update(bytes);
return bufferToHex(l_messagedigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
}
private static String bufferToHex(byte bytes[]) {
return bufferToHex(bytes, 0, bytes.length);
}
private static String bufferToHex(byte bytes[], int m, int n) {
StringBuffer stringbuffer = new StringBuffer(2 * n);//一个字节八位 11111111对应FF FF为两个字符
int k = m + n;//n为length
for (int l = m; l < k; l++) {
appendHexPair(bytes[l], stringbuffer);
}
return stringbuffer.toString();
}
private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
char c0 = hexDigits[(bt & 0xf0) >> 4]; // 取字节中高 4 位的数字转换, >>>
// 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同
char c1 = hexDigits[bt & 0xf]; // 取字节中低 4 位的数字转换
stringbuffer.append(c0);
stringbuffer.append(c1);
}
}
AES加密:
public class AESUtils {
private Cipher cipher;//加密类
private SecretKeySpec key;//秘钥类
public static final String SEED_16_CHARACTER = "U1MjU1M0FDOUZ.Qz";//秘钥 相互协商
public AESUtils() {
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//加密算法,加密模式,填充方式
key = new SecretKeySpec(SEED_16_CHARACTER.getBytes(), "AES");
} catch (Exception e) {
e.printStackTrace();
}
}
public String encrypt(String plainText) {
try {
cipher.init(Cipher.ENCRYPT_MODE, key);//初始化加密类设置类型为加密,秘钥
byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));//获取明文的UTF-8编码进行加密
String encryptedText = new String(Base64.encode(encrypted,
Base64.DEFAULT), "UTF-8");//将密文字节转成Base64编码后再转成UTF-8编码(自行定义)并转字节为文本
return encryptedText;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public String decrypt(String cryptedText) {
try {
cipher.init(Cipher.DECRYPT_MODE, key);//解密模式 剩下的顺序与加密相反
byte[] bytes = Base64.decode(cryptedText, Base64.DEFAULT);
byte[] decrypted = cipher.doFinal(bytes);
String decryptedText = new String(decrypted, "UTF-8");
return decryptedText;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
本文介绍了一种实现MD5校验值生成的方法及AES加密解密的工具类。MD5工具类提供了生成字符串、文件及输入流的MD5校验值的功能;AES工具类实现了基于AES算法的加密与解密操作。
4899

被折叠的 条评论
为什么被折叠?



