关于java字符串md5加密的信息
本篇文章给大家谈谈java字符串md5加密,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java怎么把字符串进行md5加密
给你看源代码,我自己写的
public static String md5(String src){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] output = md.digest(src.getBytes());//加密处理
//将加密结果output利用Base64转换成字符串输出
String ret = Base64.encodeBase64String(output);
return ret;
}catch(Exception e){
throw new NoteException("密码加密失败",e);
}
}
public static void main(String[] args) {
System.out.println(md5("123456"));
}
java怎么实现md5字符串加密
import java.security.MessageDigest;
public class MD5Util {
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static void main(String[] args){
String s= "20160408dehui013691632869";
System.out.println(MD5Encode(s,null));
}
}
java 中如何进行md5加密
JDK里面有一个java.security.MessageDigest类,这个类就是用来加密的。
加密代码如下:
String token = System.currentTimeMillis()+new Random().nextInt()+"";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(token.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}
这个byte类型的数组就是使用MD5加密后的结果
java字符串md5加密的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java字符串md5加密的信息别忘了在本站进行查找喔。