Mobile Security Guard ------ custom dialog box on the mobile anti-theft page & amp; MD5 encryption, security guard ------

Source: Internet
Author: User

Mobile Security Guard ------ custom dialog box on the mobile anti-theft page & MD5 encryption, security guard ------

Functional requirements:

  • When you click the "mobile anti-theft" button on the home page, you can determine whether the user has set a password.
  • If no password is set, the Enter Password dialog box is displayed.
  • If the password is set, the Set Password dialog box is displayed.
  • The user's password must be MD5 encrypted before being stored in the memory.

Technical points:
-Use of the custom dialog box
-MD5 Encryption
-Read and Write operations of SharedPreferences

Custom dialog box
1. Create a layout file under the layout directory and set the custom dialog box layout successfully.
The Code is as follows:

The layout code of the "set password" dialog box is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/TV _dialogsetuppwd_title" android: text = "set password" android: textSize = "36sp" android: textColor = "#000" android: background = "#66ff6600" android: gravity = "center_horizontal"/> <EditText android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter the password" android: inputType = "textPassword" android: id = "@ + id/et_dialogsetuppwd_pwd" android: textSize = "30sp" android: textColor = "#000"/> <EditText android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "enter the password" android: inputType = "textPassword" android: id = "@ + id/et_dialogsetuppwd_checkpwd" android: textSize = "30sp" android: textColor = "#000"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: id = "@ + id/btn_dialogsetuppwd_ OK" android: text = "OK" android: layout_marginRight = "10dp" android: layout_marginLeft = "20dp" android: textSize = "28sp"/> <Button android: layout_width = "0dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: id = "@ + id/btn_dialogsetuppwd_cancel" android: textSize = "28sp" android: text = "cancel" android: layout_marginRight = "20dp" android: layout_marginLeft = "10dp"/> </LinearLayout>

The layout file of the Enter Password dialog box is not described in detail ..

2. Attach the custom layout to the dialog box:

AlertDialog. builder builder = new AlertDialog. builder (HomeActivity. this); View view = View. inflate (HomeActivity. this, R. layout. dialog_input_password, null); builder. setView (view); mEtPwd = (EditText) view. findViewById (R. id. et_dialoginputpwd_pwd); mBtnOk = (Button) view. findViewById (R. id. btn_dialoginputpwd_ OK); mBtnCancel = (Button) view. findViewById (R. id. btn_dialoginputpwd_cancel); mBtnOk. setOnClickListener (new View. onClickListener () {public void onClick (View v) {String password = mEtPwd. getText (). toString (). trim (); if (TextUtils. isEmpty (password) {Toast. makeText (HomeActivity. this, "the password cannot be blank. Please enter it again", Toast. LENGTH_SHORT ). show ();} else {String checkPassword = sp. getString ("password", null); if (password. equals (checkPassword) {// enter the dalog on the home page. dismiss ();} else {Toast. makeText (HomeActivity. this, "Incorrect password, please enter again", Toast. LENGTH_SHORT ). show () ;}}}); mBtnCancel. setOnClickListener (new View. onClickListener () {public void onClick (View v) {dialog. dismiss () ;}}); dialog = builder. show ();}

Logic:

  • Determines whether the password value is null. If it is null, the Set password dialog box is displayed. Otherwise, the Enter password dialog box is displayed.
  • In the dialog box that appears, when the two passwords are the same and are not empty, the password is stored in SharedPreferences.
  • If the dialog box is displayed, when the password entered by the user is the same as the password stored in SharedPreferences, go to the next page. Otherwise, an error is reported.

MD5 Encryption
MD5 encryption is irreversible, that is, the encrypted ciphertext cannot be converted back to the original content.

How to Implement MD5 encryption:

  • Get an information digest MessageDigest
  • Instantiate the information digest and determine the MD5 encryption method.
  • Converts a string to a byte array.
  • Converts a byte array to an MD5-encrypted byte array using the digest () method.
  • Traverse Arrays
  • Returns the int type for each byte in the byte array.
  • Call Integer. toHexString (int) to convert the int type to a hexadecimal string.
  • If the length of a string is 1, the zero-padding operation is performed.
  • Add a string to StringBuffer
  • StringBuffer calls toString () to convert it into a string, that is, the result.

Sample Code:

// Encrypt data with MD5 public String getTextByMD5 (String text) {StringBuffer buffer = new StringBuffer (); try {// 1. create an information digest MessageDigest digest = MessageDigest. getInstance ("md5"); // 2. call the digest Method to encrypt the data for MD5 for the first time. byte [] bytes = digest. digest (text. getBytes (); // 3. traverse the array for (byte B: bytes) {// 4. and int number = B & 0xff; // 5. convert the operation result to a hexadecimal String hex = Integer. toHexString (number); // 6. add a string to buffer if (hex. length () = 1) {buffer. append ("0");} buffer. append (hex) ;}} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} return buffer. toString ();}

Unconsciously, it's dawn ~~ Sleepy, continue fighting in five hours !!

Copyright statement: the original content that just came out of the box. I hope it will help you ~

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.