使用MessageDigest进行摘要练习

(MyNotes)做笔记

import java.security.MessageDigest;
import java.util.Arrays;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Base64.Decoder; 
public class Class_MessageDigest{
	public static void main(String[] args){
		String password = "admin123456";
		try{
	//通过md5进行摘要 还有sha-1;sha-256		
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] by = md.digest(password.getBytes("UTF-8"));
			System.out.println(Arrays.toString(by));
	//将字节转换成字符串 用String构造方法 String(byte[] bytes);	
			String str1 = new String(by);
			System.out.println(str1);
	//用Base64 将上方所求摘要进行编码 A-Z a-z 0-9 / *
			String str2 = Base64.getEncoder().encodeToString(by);
			System.out.println(str2);
	//解码
			byte[] by2 = Base64.getDecoder().decode(str2);
			System.out.println(Arrays.toString(by2)); 
	//尝试比较一下by与by2	结果是true
			boolean judge = Arrays.equals(by,by2);
			System.out.println(judge);
		}catch(NoSuchAlgorithmException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
}

这里是结果

Process started (PID=13096) >>>
[-90, 106, -69, 86, -124, -60, 89, 98, -40, -121, 86, 79, 8, 52, 110, -115]
籚勀Yb貒V4n?
pmq7VoTEWWLYh1ZPCDRujQ==
[-90, 106, -69, 86, -124, -60, 89, 98, -40, -121, 86, 79, 8, 52, 110, -115]
true
<<< Process finished (PID=13096). (Exit code 0)
================ READY ================

你可能感兴趣的:(MessageDigest)