MessageDigestDEMO

Posted on 2008-05-21 22:44 橡皮人 阅读(151) 评论(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

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


网站导航:
 

posts - 28, comments - 5, trackbacks - 0, articles - 0

Copyright © 橡皮人