现在密码使用的几乎都是用md5加密的,如qq密码, 银行密码等。是不可逆的,难破解。
//这是个工具包
public class Md5Util {
/**
* 根据给定的文件路径 生成该文件的特征码 md5
*
* @param filePath
* @return
*/
public static String encodeFile(String filePath) {
try {
FileInputStream in = new FileInputStream(filePath);
// 得到消息摘要对象
MessageDigest digester = MessageDigest.getInstance("MD5");
byte[] bytes = new byte[8192];
int byteCount;
while ((byteCount = in.read(bytes)) > 0) {
digester.update(bytes, 0, byteCount);
}
byte[] digest = digester.digest();
//32位的16进制
StringBuffer stringBuffer = new StringBuffer();
for (byte b : digest) {
// 0 - 9 a - f
String s = Integer.toHexString(b&0xff);
if(s.length() == 1){
s = "0"+s;
}
stringBuffer.append(s);
}
in.close();
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encodeString(String str) {
try {
// 得到消息摘要对象
MessageDigest digester = MessageDigest.getInstance("MD5");
//更新消息摘要
digester.update(str.getBytes());
//提取消息摘要数据
byte[] digest = digester.digest();
//32位的16进制
StringBuffer stringBuffer = new StringBuffer();
for (byte b : digest) {
// 0 - 9 a - f
String s = Integer.toHexString(b&0xff);
if(s.length() == 1){
s = "0"+s;
}
stringBuffer.append(s);
}
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}