随笔-48  评论-26  文章-0  trackbacks-0
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
 * 運用系統類進行的加密算法
 
*/

public class MessageDigestDemo {

    
    String str
=new String("userpassword");
    String newStr
="";
    MessageDigest md5
=null;
    
public MessageDigestDemo(){
        System.out.println(str);
        System.out.println(getMd5(str));
        System.out.println(str.equals(getMd5(str)));
        
        
    }

    
public String getMd5(String str) {
        
try {
            md5
=MessageDigest.getInstance("MD5");
            newStr
=new String(md5.digest(str.getBytes("UTF-8")));
        }
 catch (NoSuchAlgorithmException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }
 catch (UnsupportedEncodingException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
return newStr;
    }

    
public static void main(String[] args) {
        
new MessageDigestDemo();

    }


}

 Result :
userpassword
;澿?/鱦
渣甇鰪R??
false

文章来源:http://www.blogjava.net/NicholasEcho/archive/2008/05/21/202051.html
posted on 2008-05-21 22:44 Worker 阅读(53) 评论(0)  编辑  收藏 所属分类: J2SE/J2EE

只有注册用户登录后才能发表评论。


网站导航: