MD5 encryption tool

Source: Internet
Author: User
Tags array to string
Package COM. liujun. util; import Java. security. messagedigest; /*** MD5 encryption and matching tool * @ author Asus **/public class md5util {// ing array of numbers to characters in hexadecimal notation private final static string [] hexdigits = {"0 ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "B", "C", "D", "E", "F "}; /*** replace a byte with a hexadecimal string * @ Param B hexadecimal byte * @ return hexadecimal string */Private Static string bytetohexstr (byte B) {int n = B; If (n <0) {n = 256 + N;} int d1 = N/16; int d 2 = n % 16; return hexdigits [D1] + hexdigits [D2];} /*** convert a byte array to a hexadecimal string * @ Param bytes byte array * @ return a hexadecimal string */Private Static string bytearraytohexstr (byte [] bytes) {stringbuffer sb = new stringbuffer (); For (INT I = 0; I <bytes. length; I ++) {sb. append (bytetohexstr (Bytes [I]);} return sb. tostring ();}/*** MD5 encryption of the string * @ Param originstr original password * @ returnmd5 encrypted password string */public static string encodetomd5str (string Origins Tr) {If (originstr! = NULL) {try {// create MD5 Algorithm information messagedigest = messagedigest. getinstance ("MD5"); // encrypt and convert the original password to an MD5 byte array byte [] md5bytes = messagedigest. digest (originstr. getbytes (); // convert the encrypted byte array to string resultstr = bytearraytohexstr (md5bytes); // convert it to return resultstr. touppercase ();} catch (exception e) {e. printstacktrace () ;}} return NULL ;} /*** use the most string to match the MD5 password * @ Param md5str * @ Param originstr * @ return */public static Boolean validatepassword (string md5str, string originstr) {If (md5str. equals (encodetomd5str (originstr) {return true ;}else {return false ;}}}
Package COM. liujun. main; import COM. liujun. util. md5util; public class main {public static void main (string [] ARGs) {// original password string orginstr = "12345"; // MD5 encrypted password string md5str; // generate the original password md5str = md5util. encodetomd5str (orginstr); system. out. println ("original password:" + orginstr); system. out. println ("MD5 encryption password:" + md5str); // verify that the password is correct if (md5util. validatepassword (md5str, orginstr) {system. out. println ("password matched");} else {system. out. println ("password matching failed ");}}}
Source code: http://download.csdn.net/detail/u010739551/7875377



 


MD5 encryption tool

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.