安卓Android MD5加密算法
今天打算给项目做一下加密,优先想到的是一个MD5的加密算法。因为简单而且经典,所以就用它了。
搜索了一下,在CSDN这里没找到适用的。所以转载一下别人写的算法。
源码
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public String get_MD5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(
string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Hei, MD5 should be supported!Check it please!", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Hei, UTF-8 should be supported!Check it please!", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10)
hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
}
由于比较简单,实体化这个类进行调用就可以了