Android Beginner Tutorial: MD5 Encryption Tool class for files and strings

Source: Internet
Author: User
Tags md5 encryption

Reprint please indicate the source: http://blog.csdn.net/qq_32059827/article/details/52200008 Click to open the link

I have previously written a blog post that is MD5 encrypted for strings. Today, it is improved by adding MD5 encryption for a file, and getting the encrypted value. And two functions are packaged into a tool class, if there is a need for this algorithm, can be used directly.

Directly on the algorithm encapsulation of the tool class code:

Package Com.itydl.utils;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.ioexception;import java.security.messagedigest;import java.security.nosuchalgorithmexception;/** *  MD5 encryption for strings, and tool classes involving MD5 operations * @author Lenovo * */public class Md5utils {/** * Returns the MD5 value of the file * @param path * Paths of files to encrypt * @return * The MD5 value of the file */public the static string getFileMD5 (string path) {StringBuilder sb = new StringBuilder (); try {FileInputStream fis = new FileInputStream (path);//Get MD5 dongle messagedigest MD = messagedigest.getinstance ("MD5");//Read file byte[] bytes = new byte[10240];//One read write 10kint len = 0;while (len = fis.read (bytes))!=-1) {//Read data from original destination//write data to MD cipher, analogy Fos.write ( Bytes, 0, Len); md.update (bytes, 0, len);} Read the entire file data and write to the MD cipher byte[] Digest = Md.digest ();//Complete the encryption, get the MD5 value, but the byte type. I'm going to do the final conversion. for (byte b:digest) {//traversal bytes, stitching each byte together//convert each byte to 16 binary int d = b & 0xff;//Keep only the last two digits of string herstring = Integer.toh Exstring (d);//convert int type data to 16 binary string representation//If there is only one, then 0 is in front. Let it also be two-bit if (herString.Length () ==1) {//bytes high 4 bits 0herString = "0" +herstring;//stitching string, two bits representing}sb.append (herstring);}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch ( NoSuchAlgorithmException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo auto-g Enerated catch Blocke.printstacktrace ();} return sb.tostring ();} /** * MD5 encryption of strings passed in * @param str * String to be encrypted * @return * string Md5 encrypted result */public static string MD5 (String str) {Stringbuilde R sb = new StringBuilder ();//String container try {//Get MD5 dongle. public static MessageDigest getinstance (string algorithm) returns the implementation of the specified digest algorithm. The MessageDigest object. MessageDigest MD = messagedigest.getinstance ("MD5"); byte[] bytes = Str.getbytes ();//Convert the string to be encrypted into a byte array byte[] Digest = Md.digest (bytes);//Use the specified "byte array" to make a final update of the digest, and then complete the summary calculation. That is, complete the MD5 encryption for (byte b:digest) {//convert each byte to 16 binary int d = b & 0xff;//only retain the latter two digits of string herstring = Integer.tohexstring (d);// int type data to 16 binary string representation//If there is only one bit, then 0 is in front. Let it be also two-bit if (Herstring.length () ==1) {//bytes high 4 bits 0heRString = "0" +herstring;//stitching string, two bits to represent}sb.append (herstring);}} catch (NoSuchAlgorithmException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return sb.tostring ();}}



Android Beginner Tutorial: MD5 Encryption Tool class for files and strings

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.