MD5加密代码
/**
* @ClassName MD5Utils
* @Description: TODO
* @Author ZK
* @Date 2020/8/3 13:56
* @Version V1.0
**/
package com.zk.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String enctypeCode(String oldWord) {
StringBuffer newWord = new StringBuffer();
try {
//1. 获取加密对象
MessageDigest md5 = MessageDigest.getInstance("md5");
//2.把原始数据转成byte数组
byte[] bc = md5.digest(oldWord.getBytes());
//newWord = new String(bc);
//设置加密规则
String temp = "";
for (byte b:bc){
int a=b;
if(a<0){
a+=256; //这种值可以任意(不要超过int型数值的最大值)
}
temp = Integer.toHexString(a);
if(temp.length()<2){
temp="0"+temp;
}
newWord.append(temp);
}
// for (int i = 0; i <bc.length ; i++) {
// if(bc[i]<0){
// bc[i]+=128;
// System.out.println(bc[i]);
// }
// }
// newWord = new String(bc);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return newWord.toString();
}
public static void main(String[] args) {
System.out.println(enctypeCode("123456"));
}
}