java计算文本MD5值,用于加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class GetMd5 {
public static void main(String[] args) {
String a="123";
System.out.println(getMd5(a));
} /**
* 获取文本字段的MD5值
* @param txt
* @return
*/
public static String getMd5(String txt){
String rs = "";
String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] b = messageDigest.digest(txt.getBytes());
StringBuffer resultSb = new StringBuffer();
for (int i = 0;
i < b.length;
i++) {
int n = b[i];
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
resultSb.append(hexDigits[d1] + hexDigits[d2]);
}
rs = resultSb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return rs;
}
}
输出值:202cb962ac59075b964b07152d234b70
【java计算文本MD5值】
推荐阅读
- 学习笔记|uni-app开发小程序
- Android圆形进度条控件-CircleSeekBar
- 学习笔记|安卓中一些界面过场动画的实现
- MongoDB-存储
- 学习笔记|Burnside引理和polay计数学习笔记
- Python|【网易2019年秋招笔试题】编程题第二题(香槟塔里倒香槟——参考代码和编程思路)
- Android|安装APP损坏,出现[INSTALL_FAILED_DEXOPT]的解决办法
- Android|java日期格式化
- Android|android 读取json数据(遍历JSONObject和JSONArray