1 package cn.itcast.encode; 2 3 import org.apache.commons.lang3.RandomStringUtils; 4 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 5 import org.springframework.util.DigestUtils; 6 7 import java.io.UnsupportedEncodingException; 8 9 public class MD5Demo { 10 public static void main(String[] args) throws UnsupportedEncodingException { 11 // String password = "liu123"; 12 // String password = "mozq123"; 13 String password = "liubei123"; 14 // String s = DigestUtils.md5DigestAsHex(password.getBytes()); 15 16 //进行加盐 17 // String salt = "mozq"; 18 19 //生成随机盐值 20 // String salt = RandomStringUtils.random(10); 21 // String salt = RandomStringUtils.randomAlphabetic(10); 22 String salt = RandomStringUtils.randomAlphabetic(5, 8); 23 System.out.println(salt); 24 System.out.println(salt.length()); 25 26 String passwordSalt = salt + password; 27 //获取MD5加密工具,进行加密 28 // String s = DigestUtils.md5DigestAsHex(passwordSalt.getBytes()); 29 30 31 /* 32 Spring给出的加密解决方案 33 */ 34 BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 35 String s = encoder.encode(password); 36 //$2a$10$RcVrPsuSd3BPiBWdXF.ncO96Ls99VSIY92zZ0.z7F6jFPmjsRzaOC 37 //$2a$10$M.fk6zjeH/7By7Krcem0u.wWsdSiorJ2do98QLZJjssUWwx2eCfeW 38 System.out.println(s); 39 System.out.println(s.length()); 40 } 41 }