2012/08/20

Java使用MessageDigest加密

這個類別是Java Api的類別,算是一個可以快速生成MD5或是SHA-1的加密技術
不過我個人認為不是很好用,似乎也只能加密,不過有就貼出來給大家使用吧…
會使用到sun.misc.BASE64Decoder和sun.misc.BASE64Encoder兩個類別,不過根據這篇文章的說明似乎不是很好用這樣

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class Password {
    public static void main(String[] args) {
        try {
            String str = "Cheng You Feng";
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(str.getBytes());
            System.out.println(digest.digest().toString());

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}



參考文章:
http://docs.oracle.com/javase/1.4.2/docs/api/java/security/MessageDigest.html
http://blog.csdn.net/qiao000_000/article/details/5493838
http://benx.iteye.com/blog/1047151
http://blog.csdn.net/caidaow/article/details/3346009
http://topic.csdn.net/u/20080925/14/1dd02ef7-f3fd-4f03-b70b-bdd9e0c5c2ea.html
http://www.anyexample.com/programming/java/java_simple_class_to_compute_sha_1_hash.xml
http://jimb.iteye.com/blog/269302