java的md5加密字符串超简单

阅读更多
直接上代码,以后备用:
       
 /**
	 * md5 string
	 * @param String str
	 * @return String
	 */
	private String _md5( String str ) {
		String ret = null;
		
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("md5");			
			//byte array.  
			messageDigest.update( str.getBytes(), 0, str.length() );			
			//algoritmo MD5 String.
			ret = new BigInteger(1, messageDigest.digest()).toString(16);
						
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return ret;
	}

调用实例:
public static void main(String[] arg) {
		String str = "123";
		{Current Class} newClass = new {Current Class}();
		System.out.println( "123 _md5 is " + newClass._md5(str) );
}

输出:
123 _md5 is 202cb962ac59075b964b07152d234b70

你可能感兴趣的:(java,md5)