如下所示:
パッケージcn.sunzn.md5; import java.security.messagegest; import java.security.nosuchalgorithmexception; public class str2md5 {public static void main(string [] args){md5( "sunzn"); } private static string md5(string sourcestr){string result = ""; try {MessagedGest MD = MessagedGest.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()。サブストリング(8、24)); } catch(nosuchalgorithmexception e){system.out.println(e); } return result; }}MD5(Sunzn、32)= 40379DB889F9124819228947FAAEB1F7MD5(SUNZN、16)= 89F9124819228947
以上就是小编为大家带来的java编程下字符串的16位、32位md5加密实现方法的全部内容了、希望对大家有所帮助、多多支持武林网〜