MD5加密

来源:互联网 发布:java红黑树删除 编辑:程序博客网 时间:2024/05/29 13:19
MD5算法是:
    (1)相同的2个String.必须生产相同的结果;

    (2)不相同的2个String.必须生产不相同的结果  --->> 不同的2个String,也可能生成相同的结果。

package md5;
import sun.misc.BASE64Encoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//直接使用bytes.toString() 打印出来的是数组的地址而不是内容
public class MD5 {
    MessageDigest md5=null;
   public String getMd5(String s) {
      byte[] bytes=s.getBytes();
              try {
                 md5=MessageDigest.getInstance("MD5");
                 //使用指定的 byte 数组更新摘要。
                 md5.update(s.getBytes());
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              byte[] newBytes=md5.digest(bytes);
            //直接使用bytes.toString() 打印出来的是数组的地址而不是内容
            //  System.out.println(" newBytes.toString() "+newBytes.toString());
              System.out.println("new String(newBytes)"+ new String(newBytes));
              return new String(newBytes);  
   }
   public static void main(String args[]){
       MD5 app=new MD5();
       String target="hello world!!";
       String result1=app.getMd5(target);
       String result2=app.getMd5(target);
       System.out.println(result1.equals(result2)?true:false);
   }
}




0 0