口令MD5加解密及测试方法

md5解密  时间:2021-01-31  阅读:()

Java实现MD5加密解密类收藏

转载请注明:来自http://b lo g.c sdn.net/M_ChangGong/作者:张燕广Java实现MD5加密以及解密类 附带测试类具体见代码。

MD5加密解密类——MyMD5Util,代码如下view plaincopy to clipboardprint?

01.p ackage c om.zyg.s ecurity.md5;

02.

03.imp ort java.io.UnsupportedEnc odingExc eption;

04.import java.security.Mes sageDigest;

05.imp ort java.s ecurity.No SuchAlgorithmExc eption;

06.import java.security.S ecureRandom;

07.import java.util.Arrays;

08.

09.public class MyMD5Util {

10.

11. private static final StringHEX_NUMS_S TR="0123456789ABC DEF";

12. private static final Integer SALT_LENGTH=12;

13.

14. /**

15. *将16进制字符串转换成字节数组

16. *@p aram hex

17. *@return

18. */

19. public static byte[]hexStringToByte(String hex) {

20. int len=(hex.le ngth() /2);

21. byte[]result=new byte[len];

22. c har[]hexChars=hex.toCharArray();

23. for(int i=0; i<len; i++) {

24. intpos=i*2;

25. re s ult[i] = (b yte)(HEX_NUMS_STR.ind exO f(he xChars[p os])<<4

26. |HEX_NUMS_S TR.indexO f(hexChars[p os+1]));

27. }

28. return result;

29. }

30.

31.

32. /**

33. *将指定byte数组转换成16进制字符串

34. *@param b

35. *@return

36. */

37. public static String byteToHexString(byte[]b) {

38. S tring Buffer hexS tr in g=new S trin gBuffer();

39. for(int i=0; i<b.length; i++) {

40. S tring hex=Inte ger.to HexS tring(b[i]&0xF F);

41. if(he x.leng th()==1) {

42. hex='0'+hex;

43. }

44. hexS trin g.ap p end(hex.to Up p erC as e());

45. }

46. return hexString.to String();

47. }

48.

49. /**

50. *验证口令是否合法

51. *@param password

52. *@param passwordInDb

53. *@return

54. *@throws No S uc hAlgorithmExc ep tion

55. *@throws UnsupportedEnc odingExc eption

56. */

57. public static boolean validPassword(String password, Stringp as s wo rdInDb)

58. throws NoSuchAlgorithmException,UnsupportedEnc odingExc eption{

59. //将16进制字符串格式口令转换成字节数组

60. b yte[]p wdInDb=hexS tringTo Byte(p as s wo rdInDb);

61. //声明盐变量

62. b yte[] s alt=new b yte[S ALT_LENGTH];

63. //将盐从数据库中保存的口令字节数组中提取出来

64. S ys tem.arrayc o p y(p wd InDb,0, s alt,0,S ALT_LENG T H);

65. //创建消息摘要对象

66. Mess ageD ig es t md=Mess ageD iges t.getIns tanc e("MD 5");

67. //将盐数据传入消息摘要对象

68. md.up d ate(s alt);

69. //将口令的数据传给消息摘要对象

70. md.up date(p as s wo rd.getBytes("UTF-8"));

71. //生成输入口令的消息摘要

72. byte[]digest=md.digest();

73. //声明一个保存数据库中口令消息摘要的变量

74. byte[] digestInDb = new byte[pwdInDb.length -S ALT_LENG T H];

75. //取得数据库中口令的消息摘要

76. S ys tem.arrayc o p y(p wd InDb, S ALT_LENG T H, d ige s tInDb,0,dige s tInDb.leng th);

77. //比较根据输入口令生成的消息摘要和数据库中消息摘要是否相同

78. if(Arr ays.equals(diges t,diges tInDb)) {

79. //口令正确返回口令匹配消息

80. return true;

81. } els e {

82. //口令不正确返回口令不匹配消息

83. return fals e;

84. }

85. }

86.

87.

88. /**

89. *获得加密后的16进制形式口令

90. *@param password

91. *@return

92. *@throws No S uc hAlgorithmExc ep tion

93. *@throws UnsupportedEnc odingExc eption

94. */

95. public static String getEncryptedPwd(String password)

96. throws NoSuchAlgorithmException,UnsupportedEnc odingExc eption{

97. //声明加密后的口令数组变量

98. b yte[]p wd=nul l;

99. //随机数生成器

100. S ecureRandom random=new S ecureRandom();

101. //声明盐数组变量

102. b yte[] s alt=new b yte[S ALT_LENG T H];

103. //将随机数放入盐变量中

104. rand o m.nextByt e s(s alt);

105.

106. //声明消息摘要对象

107. Mes sageDigest md=null;

108. //创建消息摘要

109. md=Mes s ageDig es t.getIns tanc e("MD5");

110. //将盐数据传入消息摘要对象

111. md.up d ate(s alt);

112. //将口令的数据传给消息摘要对象

113. md.update(p ass word.getByt es("UTF-8"));

114. //获得消息摘要的字节数组

115. byte[]digest=md.digest();

116.

117. //因为要在口令的字节数组中存放盐所以加上盐的字节长度

118. p wd=new b yte[diges t.length+S ALT_LENG TH];

119. //将盐的字节拷贝到生成的加密口令字节数组的前12个字节 以便在验证口令时取出盐

120. S ys tem.arrayc o p y(s alt,0,p wd,0,S ALT_LENG T H);

121. //将消息摘要拷贝到加密口令字节数组从第13个字节开始的字节

122. S ys tem.arrayc op y(diges t, 0, p wd, S ALT_LENGTH,diges t.length);

123. //将字节数组格式加密后的口令转化为16进制字符串格式的口令

124. return byteToHexS tring(pwd);

125. }

126.}p ackage com.zyg.s ecurity.md 5;imp ort java.io.Unsupp ortedEnco dingExc eption;import java.security.Mes sageDigest;import java.s ecurity.No SuchAlgorithmExc eption;import java.security.S ecureRandom;

import java.util.Arrays;public class MyMD5Util {private static final StringHEX_NUMS_S TR="0123456789ABC DEF";private static final Integer SALT_LENGTH=12;

/**

*将16进制字符串转换成字节数组

*@p aram hex

*@return

*/public static byte[]hexStringToByte(String hex) {int len=(hex.le ngth() /2);byte[]result=new byte[len];c har[]hexChars=hex.toCharArray();for(int i=0; i<len; i++) {int pos=i*2;result[i]=(byte) (HEX_NUMS_STR.indexO f(he xChars[p o s])<<4

|HEX_NUMS_STR.indexO f(hexChars[p o s +

1]));

}return result;

}

/**

*将指定byte数组转换成16进制字符串

*@param b

*@return

*/public static String byteToHexString(byte[]b) {S tringBuffer hexS tring=new S tringBuffer();for(int i=0; i<b.length; i++) {

S tring h ex=Intege r.to HexS tring(b[i]&0xF F);if(hex.le ngth()==1) {hex='0'+hex;

}hexS tr ing.ap p end(hex.to Up p erC as e());

}return hexS tring.to S tring();

}

NameCheap新注册.COM域名$5.98

随着自媒体和短视频的发展,确实对于传统的PC独立网站影响比较大的。我们可以看到云服务器商家的各种促销折扣活动,我们也看到传统域名商的轮番新注册和转入的促销,到现在这个状态已经不能说这些商家的为用户考虑,而是在不断的抢夺同行的客户。我们看到Namecheap商家新注册域名和转入活动一个接一个。如果我们有需要新注册.COM域名的,只需要5.98美元。优惠码:NEWCOM598。同时有赠送2个月免费域名...

hostkey俄罗斯、荷兰GPU显卡服务器/免费Windows Server

Hostkey.com成立于2007年的荷兰公司,主要运营服务器出租与托管,其次是VPS、域名、域名证书,各种软件授权等。hostkey当前运作荷兰阿姆斯特丹、俄罗斯莫斯科、美国纽约等数据中心。支持Paypal,信用卡,Webmoney,以及支付宝等付款方式。禁止VPN,代理,Tor,网络诈骗,儿童色情,Spam,网络扫描,俄罗斯色情,俄罗斯电影,俄罗斯MP3,俄罗斯Trackers,以及俄罗斯法...

PIGYun月付14.4元起,美国洛杉矶/韩国VPS七月6折

PIGYun是成立于2019年的国人商家,提供香港、韩国和美西CUVIP-9929等机房线路基于KVM架构的VPS主机,本月商家针对韩国首尔、美国洛杉矶CUVIP-AS29、GIA回程带防御等多条线路VPS提供6-8.5折优惠码,优惠后韩国首尔CN2混合BGP特惠型/美国洛杉矶GIA回程带10Gbps攻击防御VPS主机最低每月14.4元起。下面列出几款不同机房VPS主机配置信息,请留意不同优惠码。...

md5解密为你推荐
软银亏损65亿美元马云仅仅持有阿里8.9%的股份,就有270多亿美元,孙正义的软银占有那么多股份,怎么还不是亚洲首富聚酯纤维和棉哪个好聚酯纤维和棉 那个比较暖和啊天气预报哪个好用哪个最准确分小时的那种天气预报app,哪个准确方便使用985和211哪个好211的院校和985的那个好?手动挡和自动挡哪个好自动挡手动挡哪个好?宝来和朗逸哪个好大众朗逸和宝来,哪个好点?ps软件哪个好什么PS软件好手机炒股软件哪个好什么手机炒股软件好用,你们都用哪个dnf魔枪士转职哪个好魔枪转职哪个适合搬砖qq空间登录不上为什么我的qq空间登不上去
安徽双线服务器租用 合租服务器 免费域名跳转 2019年感恩节 外贸主机 美国主机网 iis安装教程 国外免费空间 创梦 网络空间租赁 阿里云邮箱登陆 apnic 移动王卡 远程登录 pptpvpn 电信测速器在线测网速 挂马检测工具 wordpress安装 qq部落24-5 大硬盘补丁 更多