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.