An example analysis of MD5 encryption algorithm for Android programming _android

Source: Internet
Author: User
Tags md5 md5 encryption stringbuffer

This paper analyzes the MD5 encryption algorithm of Android programming. Share to everyone for your reference, specific as follows:

Android MD5 encryption is exactly the same as the J2SE platform, because the Android platform supports the Java.security.MessageDigest package. is actually exactly the same as the J2SE platform.

Algorithm Signature:

Copy Code code as follows:
String GetMD5 (String val) throws NoSuchAlgorithmException

Enter a string (text that needs to be encrypted), and get an encrypted output string (the encrypted text)

Package com.tencent.utils;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/** 
 * External supply getMD5 (String) method 
 * @author Randyjia * * */Public 
class MD5 {public
 static String GetMD5 (String val) throws nosuchalgorithmexception{
  messagedigest MD5 = messagedigest.getinstance ("MD5");
  Md5.update (Val.getbytes ());
  byte[] m = md5.digest ()//Encrypted return
  getString (m);
 private static String getString (byte[] b) {
  StringBuffer sb = new StringBuffer ();
   for (int i = 0; i < b.length i + +) {
   sb.append (b[i]);
   }
   return sb.tostring ();
}

End

 
* * MD5 Encryption 
* 
/private string Getmd5str (String str) {
  messagedigest messagedigest = null;
  try {
   messagedigest = messagedigest.getinstance ("MD5");
   Messagedigest.reset ();
   Messagedigest.update (Str.getbytes ("UTF-8"));
  } catch (NoSuchAlgorithmException e) {
   System.out.println ("NoSuchAlgorithmException caught!");
   System.exit ( -1);
  } catch (Unsupportedencodingexception e) {
   e.printstacktrace ();
  }
  byte[] ByteArray = Messagedigest.digest ();
  StringBuffer Md5strbuff = new StringBuffer ();
  for (int i = 0; i < bytearray.length i++) {
   if (integer.tohexstring (0xFF & Bytearray[i)). Length () = 1)
    Md5strbuff.append ("0"). Append (integer.tohexstring (0xFF & Bytearray[i));
   else
    md5strbuff.append (integer.tohexstring (0xFF & Bytearray[i]));
 16-bit encryption, from 9th to 25-bit return
  md5strbuff.substring (8,). ToString (). toUpperCase ();
}

Add: Android MD5 Cryptographic String Example

This encrypts the string MD5, returning the encrypted string (actually the message digest of the string).

public static string MD5 (String string) {
 byte[] hash;
 try {
  hash = messagedigest.getinstance ("MD5"). Digest (String.getbytes ("UTF-8"));
 NoSuchAlgorithmException e) {
  throw new RuntimeException ("Huh, MD5 should be supported?", E);
 } catch (Unsupporte Dencodingexception e) {
  throw new RuntimeException ("Huh, UTF-8 should be supported?", E);
 }
 StringBuilder hex = new StringBuilder (Hash.length * 2);
 for (byte b:hash) {
  if ((b & 0xFF) < 0x10) Hex.append ("0");
  Hex.append (integer.tohexstring (b & 0xFF));
 }
 return hex.tostring ();
}

I hope this article will help you with the Android program.

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.