MD5加密(字符串文件)工具类,直接用!!!
Posted 胖子luffy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MD5加密(字符串文件)工具类,直接用!!!相关的知识,希望对你有一定的参考价值。
public static String getFileMD5(File file)
if (!file.isFile())
return null;
MessageDigest digest;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1)
digest.update(buffer, 0, len);
catch (Exception e)
e.printStackTrace();
return null;
finally
if (in != null)
try
in.close();
catch (IOException e)
e.printStackTrace();
BigInteger bigint = new BigInteger(1, digest.digest());
StringBuilder md5 = new StringBuilder(bigint.toString(16));
return md5.toString();
public static String MD5(String sourceStr)
String result = "";
try
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++)
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
result = buf.toString();
catch (NoSuchAlgorithmException e)
System.out.println(e);
return result;
以上是关于MD5加密(字符串文件)工具类,直接用!!!的主要内容,如果未能解决你的问题,请参考以下文章