Android instance-Mobile security Defender (14)-MD5 encryption for passwords

Source: Internet
Author: User

First, the goal.

The set password is encrypted by MD5 and then saved.

Second, the code implementation.

1. Add a new class (named Md5utils) in the Custom Toolkit (COM.EXAMPLE.MOBILESAFE.UI). In the new Class (Md5utils), create a new static method (named Md5encryption) that returns a public of type string, passing in a parameter of type string (password).

2. In the new Method (Md5encryption):

①. Gets an instance object (named Digest) of a message digest using the getinstance (String algorithm) method of the Message digest object (messagedigest), parameter string Algorithm represents the name of the algorithm to be used. Since this procedure produces a "error without this algorithm", the Try...catch is used ... Module to capture it. Captured for the entire method, and subsequent code is in try.

②. In the try code block through the MessageDigest object's Digest (byte[] input) method completes the hash calculation and returns the byte array type (byte[]) value (named NewPassword), in the parameter (byte[] Input ), you need to turn the incoming string object through the GetBytes () method into a byte array type.

③. Create a new StringBuffer object (named buffer) to store the value of the subsequent MD5 after each byte is converted.

④. Each byte in the resulting byte array object (NewPassword) is fetched through a for loop. In the For loop, each byte and a "eight bits 11111111 (16 in 0xFF)" are performed with the operation (&), resulting in a return value of type int (named i).

In the ⑤.for loop, the integer value (i) returned in ③ is converted to a 16-binary string value (named String) by the tohexstring (int i) method of the Integer object (integer).

⑥.for Loop, determine whether the length of each byte is 1. If 1, you need to precede it with "0" and add "0" to the StringBuffer object through the append (string string) method of the StringBuffer object. If not 1, the String type value (String) is placed directly into the StringBuffer object (buffer) by means of the append (string string) method.

In the ⑦.for loop, the String type value (String) is placed in the StringBuffer object (buffer) by the append (string string) method.

⑧. Returns the StringBuffer object (buffer) after the for loop is complete and returns an empty string if there is an exception.

The new custom tool class Md5utils code is as follows:

1  Public classMd5utils {2     3      Public Staticstring md5encryption (string password) {4         Try {5MessageDigest digest = messagedigest.getinstance ("MD5");6             byte[] NewPassword =digest.digest (Password.getbytes ());7StringBuffer buffer =NewStringBuffer ();8              for(byteB:newpassword) {9                 inti = B & 0xFF;TenString string =integer.tohexstring (i); One                 if(String.Length () ==1){ ABuffer.append ("0"); -                 } - buffer.append (string); the             } -             returnbuffer.tostring ();  -}Catch(nosuchalgorithmexception e) { - e.printstacktrace (); +             return""; -         }         +     } A}
View Code 

3, modify the password to set the anti-theft password and enter the anti-Theft Password dialog box to save the way. In the main interface code:

①. In the Custom Settings Password dialog box (showsetpwddialog), the putstring (string key, String value) of the Editor object (editor) is determined when the Set Password and confirm setting password are equal. The value in the method is changed to the values that are processed by the new method (Md5encryption) in the new Class (Md5utils), with the following code:

1 editor.putstring ("Password", md5utils.md5encryption (password));
View Code

①. In the Enter Password dialog (Showinputpwddialog), when you determine that the input password and the saved password are equal, you need to first process the entered password through the new method (Md5encryption) in the new Class (md5utils) and then compare it to the saved password.

The code is as follows;

1 Else if
View Code

Third, expand.

The MD5 encrypted array can then be repeated MD5 encryption algorithm, to improve its security!

Android instance-Mobile security Defender (14)-MD5 encryption for passwords

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.