对明文字符串进行MD5加密


public static String md5(String resource) {
// 1.判断resource是否有效
if(resource.length()==0||resource==null){
//2.抛出异常
throw new RuntimeException(CrowdConstant.MESSAGE_STRING_INVALIDATE);
}
try {
// 3.获取MessageDigest对象
String way="md5";
MessageDigest messageDigest= MessageDigest.getInstance(way);
//获取资源对应的字节数组
byte[] bytes=resource.getBytes();
//执行加密
byte[] output= messageDigest.digest(bytes);
//创建bigInteger对象
int sginnum=1;
BigInteger bigInteger=new BigInteger(sginnum,output);
//按照16进制将BigInteger对象转化成字符串
int radix=16;
String code=bigInteger.toString(radix);
return code;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
结果:

对明文字符串进行MD5加密

 

 

对明文字符串进行MD5加密

 

上一篇:3月20日, Java 10 正式发布了!


下一篇:FactoryBean 和 BeanFactory