参考资料:https://www.imooc.com/learn/286
https://www.imooc.com/video/5828
md5版
用这个工具实现
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
package com.example.demo;
import java.nio.charset.Charset;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
public class md5 {
//测试
public static void main(String[] args) {
String encryPassword = md5.encryPassword("wxwxwxwx");
System.out.println(encryPassword);
}
private static final HashFunction FUNCTION = Hashing.md5();
//加盐操作,使密码更安全
private static final String SALT = "xueer.com";
public static String encryPassword(String password) {
HashCode hashCode = FUNCTION.hashString(password + SALT, Charset.forName("UTF-8"));
return hashCode.toString();
}
}
Base64版
用这个工具实现
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
package com.bootdo.util;
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static void main(String[] args) {
String base64 = Base64Util.getBase64("nihaoxueer");
System.out.println(base64);
//解密算法
byte[] decode = new Base64().decode(base64);
System.out.println(new String(decode));
}
public static String getBase64(String password) {
// 加密算法
byte[] encodeBase64 = Base64.encodeBase64(password.getBytes());
String base64Password = new String(encodeBase64);
return base64Password;
}
}