Java MD5 SHA encryption using the Method tool class MessageDigest

Source: Internet
Author: User

Package Test;import Java.security.messagedigest;import java.security.nosuchalgorithmexception;/** * Cryptographic Tool class * @author GXD * */public class Encriptutil {public static void main (string[] args) {String str = "I love you 23"; System.out.println (ENCRIPTUTIL.GETMD5 ("MD5", str));} public static string GetMd5 (String type, String str) {string md5str = null; try {//Get quote digest to algorithm that hashing algorithm the incoming string is not case-sensitive called Getinst Ance will return the initialized MessageDigest object. Therefore, it does not require further initialization. Note: This is not a singleton pattern generated object two times get the object instance is different!==//sun provides the common algorithm name has: MD2 MD5 SHA-1 SHA-256 SHA-384 SHA-512 messagedigest md = Mess Agedigest.getinstance (type);//MessageDigest object by calling the Update method the data to be computed by default takes the current system encoding GBK different encoding results are different md.update ( Str.getbytes ());//md.update (Str.getbytes ("UTF8"));//Call the Digest (summary) method to calculate the message digest (that is, generate the hash code) byte[] Digest = Md.digest ();// System.out.println (Arrays.tostring (Digest));//use Sun.misc.BASE64Encoder for simple encryption of the MD5 generated by the calculation. Base64encoder be = new Base64encoder ();//md5str = Be.encode (digest);//system.out.println (MD5STR);// MD5 deep processing of the generated stringbuffer SB = new STRIngbuffer ();  for (int i = 0; i < digest.length; i++) {Sb.append (integer.tohexstring (0xFF & Digest[i])); } md5str = Sb.tostring ();} catch (NoSuchAlgorithmException e) {e.printstacktrace (); throw new RuntimeException ("The specified encryption method does not exist");} System.out.println (Md5str.length ()); return md5str;}}

Java MD5 SHA encryption using the Method tool class MessageDigest

Related Article

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.