Preface
In our development, we need to consider this function. When the user forgets his password, we need to dynamically send him a 6-digit random password, through instant messaging, SMS, WeChat, etc. At the same time, the original password in the database is modified to these 6-digit random passwords. Let the user change the password again.
At the same time, the password in the database must be stored as ciphertext, so MD5 encryption is required. The generated 6-digit random password needs to be kept confidential and cannot be processed in the foreground, but can only be placed in the backend. So I chose to put it in the control layer
Sample code
//Pause in any text that needs to be encrypted for encryption for encryption public static String getMd5(String string) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(string.getBytes); byte hash = md.digest; StringBuffer sb = new StringBuffer; int i = 0; for (int offset = 0; offset < hash.length; offset++) { i = hash[offset]; if (i < 0) { i += 256; } if (i < 16) { sb.append("0"); } sb.append(Integer.toHexString(i)); } return sb.toString; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }Here is 32-bit encryption, and the difference between 16-bit and 32-bit is that the 16-bit is the 16-bit in the middle of 32-bit.
String PASSWORD_ = String.valueOf((int) (Math.random * 1000000)); if (empService.updateEmpPassword(EMP_ID_, BaseUtils.getMd5(PASSWORD_), operator) == 0) { throw new RuntimeException("Random password generation failed!"); } String xele = "<SendMessage><AM_Name>" + EMP_CODE_ + "</AM_Name><PhoneNum></PhoneNum><UserId></UserId><MessageTxt>The new password for your official document management system is:" + PASSWORD_ + "</MessageTxt><SystemName> Official document management system</SystemName><Type>Instant pass</Type><Access></Access><Email></Email><IsBack></IsBack><IsEncrypt></IsEncrypt><ISPriority></ISPriority><Ohter1></Ohter1><Ohter2></Ohter2></SendMessage><br />";Then call the instant network reserved interface
instanceMsgService.AMToMessIFCheck(xele, null, operator);
Note: It mainly depends on how to use MD5 encryption in the Java backend! !
Conclusion
The above is all about implementing MD5 encryption methods on the Java backend. I hope this article will be helpful to everyone to learn Java. If you have any questions, you can leave a message to communicate. Thank you for your support for Wulin.com.