密码加密算法实现md5,base64

参考资料:https://www.imooc.com/learn/286
https://www.imooc.com/video/5828

md5版

用这个工具实现

<dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>18.0version>
        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();
    }

}

密码加密算法实现md5,base64_第1张图片

Base64版

用这个工具实现

<dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
        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;

    }

}

密码加密算法实现md5,base64_第2张图片

你可能感兴趣的:(工具类)