基于Java语言MD5加密Base64转换方法
作者:随风奔跑的狼 时间:2023-11-24 00:22:41
本文提供了基于MD5加密16位和32位的方法,具体内容如下
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <p>标题:编码工具类</p>
* <p>功能:对数据进行编码转换</p>
* 作者:赵力
*/
public class EncodeUtil
{
public static void main(String[] args) throws Exception
{
System.out.println(md5Encrypt16("需要进行MD5加密的字符串"));
}
/**
* MD5加密16位
* @param encryptStr 要加密数据
* @return 返回16位加密结果
* ZhaoLi
*/
public static String md5Encrypt16(String encryptStr)
{
return md5Encrypt32(encryptStr).substring(8, 24);
}
/**
* MD5加密32位
* @param encryptStr 要加密数据
* @return 32位加密结果
* ZhaoLi
*/
public static String md5Encrypt32(String encryptStr)
{
MessageDigest md5;
try
{
md5 = MessageDigest.getInstance("MD5");
byte[] md5Bytes = md5.digest(encryptStr.getBytes());
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++)
{
int val = (md5Bytes[i]) & 0xff;
if (val < 16)
{
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString().toLowerCase();
} catch (Exception e)
{
throw new RuntimeException(e);
}
}
/**
* 结合base64实现md5加密
* @param msg 待加密字符串
* @return 获取md5后转为base64
* @throws Exception
*/
public static String md5EncryptBase64(String msg) throws Exception
{
return msg == null ? null : base64Encode(md5(msg));
}
/**
* 将byte[]转为各种进制的字符串
* @param bytes byte[]
* @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
* @return 转换后的字符串
*/
public static String binary(byte[] bytes, int radix)
{
return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
}
/**
* base 64 encode
* @param bytes 待编码的byte[]
* @return 编码后的base 64 code
*/
public static String base64Encode(byte[] bytes)
{
return new BASE64Encoder().encode(bytes);
}
/**
* base 64 decode
* @param base64Code 待解码的base 64 code
* @return 解码后的byte[]
* @throws Exception
*/
public static byte[] base64Decode(String base64Code)
{
try
{
return base64Code == null ? null : new BASE64Decoder().decodeBuffer(base64Code);
} catch (IOException e)
{
throw new RuntimeException("报错内容", e);
}
}
/**
* 获取byte[]的md5值
* @param bytes byte[]
* @return md5
* @throws Exception
*/
public static byte[] md5(byte[] bytes)
{
MessageDigest md;
try
{
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e)
{
throw new RuntimeException("报错内容", e);
}
md.update(bytes);
return md.digest();
}
/**
* 获取字符串md5值
* @param msg
* @return md5
* @throws Exception
*/
public static byte[] md5(String msg)
{
return msg == null ? null : md5(msg.getBytes());
}
}
来源:http://www.cnblogs.com/ikcai/p/7509221.html
标签:Java,MD5,加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 流与 byte[] 的互转操作
2023-06-26 11:25:46
C#读取XML的三种实现方式
2023-02-08 10:07:01
![](https://img.aspxhome.com/file/2023/8/102178_0s.png)
SpringBoot防止大量请求攻击的实现
2023-11-24 16:42:54
![](https://img.aspxhome.com/file/2023/1/59731_0s.jpg)
C#实现鼠标移动到曲线图上显示值的方法
2023-06-01 00:56:06
Android中的深度链接技术实战
2023-09-20 11:27:18
![](https://img.aspxhome.com/file/2023/4/137164_0s.jpg)
Spring+SpringMVC+MyBatis深入学习及搭建(二)之MyBatis原始Dao开发和mapper代理开发
2021-07-24 06:36:00
![](https://img.aspxhome.com/file/2023/4/67124_0s.png)
C#编程实现取整和取余的方法
2023-09-09 22:19:26
使用Java和ffmpeg把音频和视频合成视频的操作方法
2022-08-22 07:30:20
Android入门教程之ListView的具体使用详解
2022-02-12 06:35:59
![](https://img.aspxhome.com/file/2023/5/122235_0s.jpg)
Spring @Transactional注解失效解决方案
2022-10-25 05:30:30
关于MD5算法原理与常用实现方式
2023-03-18 11:09:04
![](https://img.aspxhome.com/file/2023/8/62828_0s.png)
spring boot+vue 的前后端分离与合并方案实例详解
2023-08-20 10:41:07
![](https://img.aspxhome.com/file/2023/0/72580_0s.png)
如何利用JAVA实现走迷宫程序
2022-06-23 10:52:06
![](https://img.aspxhome.com/file/2023/3/62983_0s.png)
Java使用FileInputStream流读取文件示例详解
2021-05-26 00:57:58
![](https://img.aspxhome.com/file/2023/0/64430_0s.jpg)
java eclipse 整个项目或包查找只定字符串并替换操作
2022-10-20 18:35:56
![](https://img.aspxhome.com/file/2023/1/101781_0s.jpg)
Java字符串查找的三种方式
2023-11-18 22:00:30
java中如何获取时间戳的方法实例
2021-06-14 15:29:16
深入解读Android的Volley库的功能结构
2023-02-28 00:57:12
![](https://img.aspxhome.com/file/2023/5/116005_0s.jpg)
Java线程的全方位详解
2023-04-11 14:02:55
![](https://img.aspxhome.com/file/2023/2/61042_0s.png)
Java语言之LinkedList和链表的实现方法
2023-12-19 20:18:59
![](https://img.aspxhome.com/file/2023/0/83420_0s.png)