java编程下字符串的16位,32位md5加密实现方法
程序员文章站
2024-03-12 17:56:50
如下所示:
package cn.sunzn.md5;
import java.security.messagedigest;
import java.s...
如下所示:
package cn.sunzn.md5; import java.security.messagedigest; import java.security.nosuchalgorithmexception; public class str2md5 { public static void main(string[] args) { md5("sunzn"); } private static string md5(string sourcestr) { string result = ""; try { messagedigest md = messagedigest.getinstance("md5"); md.update(sourcestr.getbytes()); byte b[] = md.digest(); int i; stringbuffer buf = new stringbuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(integer.tohexstring(i)); } result = buf.tostring(); system.out.println("md5(" + sourcestr + ",32) = " + result); system.out.println("md5(" + sourcestr + ",16) = " + buf.tostring().substring(8, 24)); } catch (nosuchalgorithmexception e) { system.out.println(e); } return result; } }
md5(sunzn,32) = 40379db889f9124819228947faaeb1f7 md5(sunzn,16) = 89f9124819228947
以上就是小编为大家带来的java编程下字符串的16位,32位md5加密实现方法的全部内容了,希望对大家有所帮助,多多支持~