base64加解密以及MD5加密

本文介绍了Java中使用commons-codec库进行Base64加密和解密,以及使用Apache Commons DigestUtils进行MD5加密的方法,包括单次和多次加密,以及加盐加密。示例代码详细展示了不同场景下的加密解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

base64加解密:commons-codes包

String username="root",  password="123",  accessCode="1";

//base64加密
String userBase64 = Base64.encodeBase64String(username.getBytes("UTF-8"));
HashMap<String, String> p = new HashMap<>(); p.put("UserName", userBase64); p.put("password", passwordMd5); System.out.println(username.getBytes("UTF-8")); //{password=202cb962ac59075b964b07152d234b70, UserName=cm9vdA==} System.out.println(p);

//base64解密
byte[] bytes = Base64.decodeBase64("cm9vdA==");
String user = new String(bytes);
System.out.println(user);//root

//md5加密
String passwordMd5 = DigestUtils.md5Hex(password);

MD5加密:rt.jar包

package com.xxx;
import org.apache.http.util.TextUtils;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@SpringBootTest
public class MD5Utils {

    /**
     * 计算字符串MD5值
     *
     * @param string
     * @return
     */
    @Test
    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 计算文件的 MD5 值
     *
     * @param file
     * @return
     */
    @Test
    public static String md5(File file) {
        if (file == null || !file.isFile() || !file.exists()) {
            return "";
        }
        FileInputStream in = null;
        String result = "";
        byte buffer[] = new byte[8192];
        int len;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                md5.update(buffer, 0, len);
            }
            byte[] bytes = md5.digest();

            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 采用nio的方式,计算文件的 MD5 值
     *
     * @param file
     * @return
     */
    @Test
    public static String md5Nio(File file) {
        String result = "";
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
            byte[] bytes = md5.digest();
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 对字符串多次MD5加密
     *
     * @param string
     * @param times
     * @return
     */
    @Test
    public static String md5(String string, int times) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        String md5 = md5(string);
        for (int i = 0; i < times - 1; i++) {
            md5 = md5(md5);
        }
        return md5(md5);
    }

    /**
     * MD5加盐
     * <p>
     * 加盐的方式也是多种多样
     * <p>
     * string+key(盐值key)然后进行MD5加密
     * <p>
     * 用string明文的hashcode作为盐,然后进行MD5加密
     * 随机生成一串字符串作为盐,然后进行MD5加密
     *
     * @param string
     * @param slat
     * @return
     */
    @Test
    public static String md5(String string, String slat) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest((string + slat).getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值