Deep understanding of Android MD5 Data encryption _android

Source: Internet
Author: User
Tags md5 md5 encryption

MD5 encryption

MD5 by MD2, MD3, MD4 evolved, although the MD5 encryption algorithm now some people have to untie it, but its encryption mechanism is still very strong, I think the big logarithm will not be solved. MD5 encryption algorithm is one-way encryption, is an irreversible encryption, you can only use your password to unlock, or it will decrypt the algorithm, otherwise do not want to untie.

The characteristics of MD5 encryption

Compressibility: Any length of data, calculated by the length of the MD5 value is fixed.

Easy to calculate: It is easy to calculate the MD5 value from the original data.

Anti-modification: Any changes to the original data, even if only modify 1 bytes, the resulting MD5 values are very different.

Strong anti-collision: Given the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, to falsify data).

MD5 Application Scenario

Consistency validation

Digital signatures

Secure access authentication

Implementation of MD5 encryption algorithm

1.) Compute string MD5 value

 public static string MD5 (String string) {
  if (Textutils.isempty (string)) {return
   "";
  }
  MessageDigest MD5 = NULL;
  try {
   MD5 = messagedigest.getinstance ("MD5");
   byte[] bytes = Md5.digest (String.getbytes ());
   String result = "";
   for (byte b:bytes) {
    String temp = integer.tohexstring (b & 0xff);
    if (temp.length () = = 1) {
     temp = "0" + temp;
    }
    result = temp;
   return result;
  } catch (NoSuchAlgorithmException e) {
   e.printstacktrace ();
  }
  Return "";
 }

2.) Calculate the MD5 value of the file

 MD5 value of the computed file public
 static String MD5 (file file
  = null | |!file.isfile () | |!file.exists ()) {
   R Eturn "";
  }
  FileInputStream in = null;
  String result = "";
  byte buffer[] = new byte[8192];
  int Len;
  try {
   MessageDigest MD5 = messagedigest.getinstance ("MD5");
   in = new FileInputStream (file);
   while (len = in.read (buffer))!=-1) {
    md5.update (buffer, 0, Len);
   }
   byte[] bytes = Md5.digest ();

   for (byte b:bytes) {
    String temp = integer.tohexstring (b & 0xff);
    if (temp.length () = = 1) {
     temp = "0" + temp;
    }
    Result + temp;
   }
  } catch (Exception e) {
   e.printstacktrace ();
  } Finally {
   if (null!=in) {
    try {
     in.close ();
    } catch (IOException e) {
     e.printstacktrace ();
    }
   }
  }
  return result;
 }

or using NiO as a way

 public static string MD5 (file file) {
  string result = ' ";
  FileInputStream in = null;
  try {in
   = new FileInputStream (file);
   Mappedbytebuffer Bytebuffer = In.getchannel (). Map (FileChannel.MapMode.READ_ONLY, 0, File.length ());
   MessageDigest MD5 = messagedigest.getinstance ("MD5");
   Md5.update (bytebuffer);
   byte[] bytes = Md5.digest ();
   for (byte b:bytes) {
    String temp = integer.tohexstring (b & 0xff);
    if (temp.length () = = 1) {
     temp = "0" + temp;
    }
    Result + temp;
   }
  } catch (Exception e) {
   e.printstacktrace ();
  } finally {
   if (null!= in) {
    try {
     in.close ();
    catch (IOException e) {
     e.printstacktrace ();
  }}} return result;
 }

Discussion on MD5 encryption security:

Although said MD5 encryption itself is irreversible, but not can not be deciphered, the internet about MD5 decryption site countless, cracking mechanism using the method of exhaustive, is what we usually say run dictionary. So how to increase the difficulty of MD5 crack?

1.) Multiple MD5 encryption of strings

 public static string MD5 (string string, int times) {
  if (Textutils.isempty (string)) {return
   "";
  }
  string MD5 = MD5 (string);
  for (int i = 0; i < times-1 i++) {
   MD5 = MD5 (MD5);
  }
  return MD5 (MD5);
 }

2.) MD5 plus salt

The way to add salt is also varied

String+key (salt value key) and then MD5 encryption

Use string plaintext hashcode as salt, then MD5 encryption

Randomly generates a string of strings as salt and then MD5 encryption

 public static string MD5 (string string, String slat) {
  if (Textutils.isempty (string)) {return
   "";
  }
  MessageDigest MD5 = NULL;
  try {
   MD5 = messagedigest.getinstance ("MD5");
   byte[] bytes = Md5.digest ((string + slat). GetBytes ());
   String result = "";
   for (byte b:bytes) {
    String temp = integer.tohexstring (b & 0xff);
    if (temp.length () = = 1) {
     temp = "0" + temp;
    }
    result = temp;
   return result;
  } catch (NoSuchAlgorithmException e) {
   e.printstacktrace ();
  }
  Return "";
 }

Summarize

This is all about the Android MD5 data encryption, hoping to help Android developers, if you have questions, you can leave a message to communicate.

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.