基于Java语言MD5加密Base64转换方法

2025-05-29 0 18

本文提供了基于MD5加密16位和32位的方法,具体内容如下

?

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 基于Java语言MD5加密Base64转换方法 https://www.kuaiidc.com/115041.html

相关文章

发表评论
暂无评论