Java back-end implementation of MD5 encryption method _java

Source: Internet
Author: User
Tags md5 md5 encryption stringbuffer

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.