Today bloggers have also started to update their blogs. After more than ten days to contribute to the country, it was really exciting to go to work on the first day! Simply talk about today -Java uses MD5 algorithm encryption to support salt.
salt is called “SALT value”. This value is randomly generated by the system, and only the system knows. Even if the two users use the same password, the salt values generated by the system are different, and the distribution values are different.
The principle of
MD5 algorithm can be briefly described as: MD5 yards process the input information in 512 bits, and each group is divided into 16 32 -bit groups. After a series of processing, the output of the algorithm consists of four 32 -bit groups. After the four 32 -bit packets, a 128 -bit scattered value will be generated.
MD5’s improvement compared to MD4:
- increased the fourth round.
- Each step has the only additional constant.
- Low the symmetry of the function in the second round.
- The first step adds the result of the previous step, which will cause a faster avalanche effect (that is, changing 1bit to the bright text or key will cause a huge difference in ciphertext).
The
- changed the order of the interviewing message group in the second and third rounds, making it even less similar.
- approximately optimized the circular left shift displacement in each round to achieve a faster avalanche effect, and the displacement of each round is different.
is a simple version, you can check and improve it with experience.
package com.cyj.admincenter.utils;
Import java.security.MessageDigest;
/**
* @Descripting: Use MD5 algorithm encryption to support salt
* @BelongSproject: Family
* @Belongspackage: com.cyj.admincenter.utils
* @Author: CHenyongjia
* @Createtime: 2020-02-11 09:46
* @Email:[email protected]* @Version: 1.0
*/
Public Class PasswordEncoder {
// Sixteen -in numbers
Private Final Static String [] Hexdigits = {"0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
Private Object Salt;
Private String Algorithm;
Public PasswordEncoder (Object Salt, String Algorithm) {{
this.salt = SALT;
this.algorithm = algorithm;
}
/**
* Conversion bytes are hexadecimal string
*
* @param B byte array
* @Return Hexishelon String
*/
Private Static String bytearraytohexstring (byte [] b) {
StringBuffer Resultsb = New StringBuffer ();
for (int i = 0; I <b.Length; i ++) {
Resultsb.append (bytetohexstring (b [i]);
}
return resultsb.tostring ();
}
Private Static String bytetohexstring (byte B) {{
int n = b;
if (n <0)
n = 256 + n;
int D1 = n / 16;
int d2 = n % 16;
Return hexdigits [d1] + hexdigits [d2];
}
Public String Encode (String Rawpass) {{
String result = null;
try {
MessageDigest MD = MessageDigest.getInstance (algorithm);
// The encrypted string
result = bytearraytohexstring (MD.Digest (Mergepasswordandsalt (
rawpass) .getBytes ("UTF-8");
} Catch (Exception EX) {
}
Return result;
}
Public Boolean IspasswordValid (String Encpass, String Rawpass) {
String pass1 = "" + encpass;
String pass2 = ENCODE (rawpass);
Return Pass1.equals (PASS2);
}
Private String Mergepasswordsalt (String Password) {{
if (password == null) {
password = "";
}
if ((SALT == NULL) || ".equals (SALT)) {{
Return password;
} else {
return password + "{" + Salt.tostring () + "}";
}
}
Public Static void main (string [] args) {
// Salt value fixed
String Salt = "HelloWorld";
PasswordEncoder encodermd5 = New PasswordEncoder (SALT, "MD5");
String encode = encodermd5.encode ("test");
// MD5 encryption with salt value
System.out.println ("MD5 encryption with salt value" + encode);
// Empty salt value MD5 encryption
System.out.println ("MD5 encryption of empty salt value"
+ New PasswordEncoder (null, "md5"). Encode ("test");
Boolean PasswordValid = encodermd5.ispasswordValid (
"1BD98ED329AEBC7B2F89424B5A38926e", "test");
System.out.println (passwordvalid);
PasswordEncoder encodersha = New PasswordEncoder (SALT, "Sha");
String pass2 = encodersha.encode ("test");
System.out.println (pass2);
Boolean PasswordValid2 = ENCODERSHA.ISPASSWORDVALID (
"1BD98ED329AEBC7B2F89424B5A38926e", "test");
System.out.println (PasswordValid2);
}
}
result is as follows:
-
For more reference blog posts, please see here:“Chen Yongjia’s Blog”
-
Friends who like bloggers can pay attention and like, continue to update, hehe!