JAVA MD5 Encryption

Source: Internet
Author: User
Tags encode string md5 encryption sha1

packagecom.demo;importjava.security.MessageDigest;publicclassMD5Util {    privatestaticfinalString ALGORITHM = "MD5";    privatestaticfinalchar[] HEX_DIGITS = { ‘0‘‘1‘‘2‘‘3‘‘4‘‘5‘,            ‘6‘‘7‘‘8‘‘9‘‘a‘‘b‘‘c‘‘d‘‘e‘‘f‘};    /**     * encode string     *     * @param algorithm     * @param str     * @return String     */    publicstaticString encode(String algorithm, String str) {        if(str == null) {            returnnull;        }        try{            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);            messageDigest.update(str.getBytes());            returngetFormattedText(messageDigest.digest());        catch(Exception e) {            thrownewRuntimeException(e);        }    }    /**     * encode By MD5     *     * @param str     * @return String     */    publicstaticString getStringMD5String(String str) {        if(str == null) {            returnnull;        }        try{            MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);            messageDigest.update(str.getBytes());            return getFormattedText(messageDigest.digest());        catch(Exception e) {            thrownewRuntimeException(e);        }    }     /**     * Takes the raw bytes from the digest and formats them correct.     *     * @param bytes     *            the raw bytes from the digest.     * @return the formatted bytes.     */    privatestaticString getFormattedText(byte[] bytes) {        intlen = bytes.length;        StringBuilder buf = new StringBuilder(len * 2);        // 把密文转换成十六进制的字符串形式        for(intj = 0; j < len; j++) {                     buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);        }        returnbuf.toString();    }    publicstaticvoidmain(String[] args) {        System.out.println(MD5Util.getStringMD5String("123"));//      System.out.println("111111 MD5  :"//              + EncoderHandler.encode("MD5", "111111"));//      System.out.println("111111 SHA1 :"//              + EncoderHandler.encode("SHA1", "111111"));    }}

JAVA MD5 Encryption

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.