Objective
In our development is to consider this feature, when the user forgets the password, we need to dynamically send him a 6-bit random password, via instant Messenger, SMS, micro-mail and so on. and also modify the original password in the database to these 6-bit random passwords. Let the user change the password again.
The password in the database must also be stored as ciphertext, so you need to use the MD5 encryption. The generated 6-bit random password needs to be kept secret, not processed in the foreground, only on the back end. So I chose to put it in the control layer.
Sample code
Encrypt public
static string GetMd5 (String string) {
try {
MessageDigest MD = by passing in any text that needs to be encrypted) 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 <) {
sb.append ("0");
}
Sb.append (integer.tohexstring (i));
}
return sb.tostring;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException (e);
}
}
The 32-bit encryption is used here, and the 16-bit and 32-bit difference is that 16-bit is the 32-bit median.
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> your document management system The new password 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 invoke the Instant Pass reservation interface
Instancemsgservice.amtomessifcheck (Xele, NULL, operator);
Note: mainly look at the Java back end how to do MD5 encryption!!
Conclusion
The above is on the Java backend implementation of the MD5 encryption method of all content, I hope this article for you to learn Java can help, if there is a problem can be a message exchange, thank you for the cloud Habitat Community support.