Android encryption Tool class

Source: Internet
Author: User

Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;/*** Cryptographic Tool class * Created by Administrator on 2015/10/21 0021.*/ Public classEncryptutils {/*** String encryption using the MD5 algorithm*/     Public Final Staticstring encryptMD5 (string source) {if(Source = =NULL) {Source= ""; The String result= ""; Try{result= Encrypt (source, "MD5"); } Catch(NoSuchAlgorithmException ex) {//generally does not occur            Throw NewRuntimeException (ex); }        returnresult.tolowercase (); }    /*** String encryption using the SHA algorithm*/     Public Final Staticstring Encryptsha (string source) {if(Source = =NULL) {Source= ""; The String result= ""; Try{result= Encrypt (source, "SHA"); } Catch(NoSuchAlgorithmException ex) {//generally does not occur            Throw NewRuntimeException (ex); }        returnresult; }    /*** String Encryption*/    Private Final Staticstring Encrypt (string source, string algorithm)throwsnosuchalgorithmexception {byte[] Resbytearray =Encrypt (Source.getbytes (), algorithm); returntohexstring (Resbytearray); }    /*** byte array encryption*/    Private Final Static byte[] Encrypt (byte[] source, String algorithm)throwsnosuchalgorithmexception {messagedigest MD=messagedigest.getinstance (algorithm);        Md.reset ();        Md.update (source); returnmd.digest (); }    /*** Get hex string from byte array*/    Private Final StaticString tohexstring (byte[] res) {StringBuffer SB=NewStringBuffer (res.length << 1);  for(inti = 0; i < res.length; i++) {String digit= Integer.tohexstring (0xFF &Res[i]); if(digit.length () = = 1) {digit= ' 0 ' +Digit;        } sb.append (digit); }        returnsb.tostring (). toUpperCase (); }}

Android encryption Tool class

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.