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