?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
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());
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/ikcai/p/7509221.html
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 47
-
2025-06-04 40
-
2025-05-25 97
-
Linux开启snmp监控后大量 Received SNMP packet(s) from UDP 的解决方法
2025-05-27 65 -
2025-06-05 65
热门评论