使用Apache digest做md5加密的范例

活用Apache commons,使用Apache digest做md5加密的范例。

import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.digest.DigestUtils; /** * @author loveapple * @since 2009-01-15 * @version $Revision$ */ public class MD5Test { //加密后的文字串 static final String CHANGEME_MD5_STR = "4cb9c8a8048fd02294477fcb1a41191a"; //被加密的文字串 static final String TARGET = "changeme"; public static void main(String[] argvs){ try { MessageDigestAdapter adapter = new MessageDigestAdapter("MD5"); String md5str = adapter.digest(TARGET); System.out.println(md5str); if(CHANGEME_MD5_STR.equals(md5str)){ System.out.println(md5str + "->" + CHANGEME_MD5_STR); System.out.println("same with mysql request. length is [" + md5str.length() + "]"); }else{ System.out.println("NOT SAME! with mysql requse"); } System.out.println("common codec result: " + DigestUtils.md5Hex(TARGET)); if(CHANGEME_MD5_STR.equals(DigestUtils.md5Hex(TARGET))){ System.out.println("code result same with mysql request too. length is [" + DigestUtils.md5Hex(TARGET) + "]"); } }catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } }

 


转贴请注明出处:http://blog.csdn.net/froole

 

你可能感兴趣的:(Jakarta,Java)