Share an MD5 encryption Java tool class

Source: Internet
Author: User

I have always shared the idea of this tool class, just because I don't have time to sort it out, so I haven't posted the code.

This is an easy-to-use method.MD5+ Salt-encrypted tool class. Some codes are searched on the Internet, and some of them are modified and added. This tool class is very simple. You can get the passwd password from the front-end and directly use hexutil. getencryptedpwd (passwd) returns a string of 56 and can be saved to the database. On the contrary, when logging on, becauseMD5 EncryptionIt is an irreversible operation. You can only use the password entered by the user to perform the MD5 + salt encryption and then compare it with the passwd in the database to see if it is consistent. When it is consistent, the password is the same and the logon is successful, by calling hexutil. validpasswd (string
Passwd, string dbpasswd) method.

Okay, paste the code and write some comments. If it is not good, I hope to leave a message saying:

/*** MD5 encryption and decryption and string comparison tool class * @ author jacb_ye * http://www.blog.csdn.net/zranye */public class hexutil {private final static string hex_nums_str = "0123456789 abcdef "; private Final Static integer salt_length = 12; /*** convert a hexadecimal string to an array ** @ return byte [] * @ author Jacob **/public static byte [] hexstringtobyte (string HEX) {/* Why is Len hex. length ()/2? * First, Hex is a string in which the content is a char array like a hex array. * Two hexadecimal numbers can be used to represent one byte, so we need to determine what kind of byte [] the char [] can be converted into. The first thing we can determine is that the length is half of the char [] */INT Len = (hex. length ()/2); byte [] result = new byte [Len]; char [] hexchars = hex. tochararray (); For (INT I = 0; I <Len; I ++) {int Pos = I * 2; Result [I] = (byte) (hex_nums_str.indexof (hexchars [POS]) <4 | hex_nums_str.indexof (hexchars [POS + 1]);} return result ;} /*** convert the array to a hexadecimal string ** @ return string * @ author Jacob ***/public static string bytetohexstring (byte [] salt) {stringbuffer hexstring = new stringbuffer (); For (INT I = 0; I <salt. length; I ++) {string hex = integer. tohexstring (Salt [I] & 0xff); If (hex. length () = 1) {hex = '0' + hex;} hexstring. append (hex. touppercase ();} return hexstring. tostring ();} /*** password verification ** @ Param passwd user input password * @ Param dbpasswd database Save Password * @ return * @ throws nosuchalgorithmexception * @ throws unsupportedencodingexception */public static Boolean validpasswd (string passwd, string dbpasswd) throws failed, unsupportedencodingexception {byte [] pwindb = hexstringtobyte (dbpasswd); // defines saltbyte [] salt = new byte [salt_length]; system. arraycopy (pwindb, 0, salt, 0, salt_length); // create a message digest object messagedigest MD = messagedigest. getinstance ("MD5"); // input the salt data to the message digest object Md. update (SALT); Md. update (passwd. getbytes ("UTF-8"); byte [] digest = md. digest (); // declare the password message digest byte [] digestindb = new byte [pwindb. length-salt_length]; // obtain the summary system of the password in the database. arraycopy (pwindb, salt_length, digestindb, 0, digestindb. length); // compare whether the message digest generated based on the input password is the same as the password Digest in the database if (arrays. equals (Digest, digestindb) {// return true if the password matches the same;} else {return false ;}} /*** hexadecimal character * @ Param passwd user input password character * @ return string MD5 encrypted password character * @ throws nosuchalgorithmexception * @ throws unsupportedencodingexception */Public static string getencryptedpwd (string passwd) throws nosuchalgorithmexception, unsupportedencodingexception {// get a random array as salt byte [] Pwd = NULL; securerandom SC = new securerandom (); byte [] salt = new byte [salt_length]; SC. nextbytes (SALT); // declare the abstract object and generate messagedigest MD = messagedigest. getinstance ("MD5"); Md. update (SALT); Md. update (passwd. getbytes ("UTF-8"); byte [] digest = md. digest (); Pwd = new byte [salt. length + digest. length]; system. arraycopy (salt, 0, PWD, 0, salt_length); system. arraycopy (Digest, 0, PWD, salt_length, digest. length); Return bytetohexstring (PWD );}

From: http://blog.csdn.net/zranye/article/details/8234480

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.