Database password encryption processing

1. MD5&Salt&BCrybt

MD5 & MD5 salt value encryption
 Message Digest algorithm5, message digest algorithm:

  • Compressibility: For data of any length, the length of the calculated MD5 value is fixed
  • Easy calculation: It is easy to calculate the MD5 value from the original data
  • Anti-modification: Any modification to the original data, even if only one byte is modified, the resulting MD5 value will be very different. Strong anti-collision: If you want to find two different data so that they have the same MD5 value, is very difficult

With salt:

  • Combining generated random numbers with MD5 generated strings
  • The database stores the MD5 value and the salt value at the same time. When verifying the correctness, use the salt to perform MD5
    @Test
    public void test01(){
//        String s= DigestUtils.md5Hex("123456");
//        String s1= Md5Crypt.md5Crypt("123456".getBytes(),"$1$qqqqqq");
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encode=passwordEncoder.encode("123456");
        boolean matches=passwordEncoder.matches("123456","$2a$10$1wj4ZKm2khXSP7KVB3Efp.lQHXLtSZcLpbOLjhvukypPaW9vqs6Cq");
        System.out.println(encode);
        System.out.println(matches);
//        System.out.println(s);
//        System.out.println(s1);
    }

According to the above code, you can use this line of code to encrypt, that is, when storing user data in the database.

String encode=passwordEncoder.encode("123456");

When you need to log in, you can decrypt it with the following line of code.

boolean matches=passwordEncoder.matches("123456","$2a$10$1wj4ZKm2khXSP7KVB3Efp.lQHXLtSZcLpbOLjhvukypPaW9vqs6Cq");

It will continue to be updated in the future. . . . . La la la ah. . . . . .

Guess you like

Origin blog.csdn.net/Hubery_sky/article/details/131868359