MD5 Simple Example

Source: Internet
Author: User

When the button is clicked, it will first determine whether the first landing, if it is the first landing will pop up the password to set the popup window, if the landing is pop-up window

The password entered will be encrypted with MD5

 PackageCom.org.demo.wangfeng.demo;ImportCOM.ORG.WANGFENG.R;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classHomeactivityextendsActivity {PrivateButton Bbbutton; Privatesharedpreferences Mpref; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.homeactivity); Mpref= getsharedpreferences ("config", mode_private); Bbbutton=(Button) Findviewbyid (r.id.bb_home); Bbbutton.setonclicklistener (NewOnclickitem ()); }    Private classOnclickitemImplementsView.onclicklistener {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubString Savedpassword = mpref.getstring ("Password",NULL); if(!Textutils.isempty (Savedpassword)) {                //Enter password pop -up windowShowpasswordinputdialog (); }Else{                //pop-up password popup if not setShowpasswordsetdialog (); }        }    }    /*** Set the popup window of the password*/    Private voidShowpasswordsetdialog () {Alertdialog.builder Builder=NewAlertdialog.builder ( This); FinalAlertdialog dialog =builder.create (); View View= View.inflate ( This, R.layout.dialog_set_password,NULL); Dialog.setview (View,0, 0, 0, 0);//set the margin to 0, guaranteed to run on the 2.x version no problem        FinalEditText Edpassword =(EditText) view. Findviewbyid (R.id.et_password); FinalEditText edpasswordconfirm =(EditText) view. Findviewbyid (r.id.et_password_confirm); Button Btnok=(Button) View.findviewbyid (R.ID.BTN_OK); Button Btncancel=(Button) View.findviewbyid (r.id.btn_cancel); Btnok.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubString Password =Edpassword.gettext (). toString (); String passwordconfirm=Edpasswordconfirm.gettext (). toString (); if(! Textutils.isempty (password) &&!Passwordconfirm.isempty ()) {                    //when you enter the same 2 content                    if(Password.equals (passwordconfirm)) {Toast.maketext (homeactivity. This, "Successful Landing", Toast.length_short). Show (); //Save the password.Mpref.edit (). putstring ("Password", Md5utils.encode (password)). commit ();                    Dialog.dismiss (); } Else{toast.maketext (homeactivity). This, "2 This input password is inconsistent", Toast.length_short). Show (); }                } Else{toast.maketext (homeactivity). This, "Input cannot be empty", Toast.length_short). Show ();        }            }        }); Btncancel.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubDialog.dismiss ();        }        });    Dialog.show (); }    /*** Enter password pop -up window*/    Private voidShowpasswordinputdialog () {Alertdialog.builder Builder=NewAlertdialog.builder ( This); FinalAlertdialog dialog =builder.create (); View View= View.inflate ( This, R.layout.dialog_input_password,NULL); Dialog.setview (View,0, 0, 0, 0); FinalEditText Etpassword =(EditText) view. Findviewbyid (R.id.et_password); Button Btnok=(Button) View.findviewbyid (R.ID.BTN_OK); Button Btncancel=(Button) View.findviewbyid (r.id.btn_cancel); Btnok.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubString Password =Etpassword.gettext (). toString (); if(!textutils.isempty (password)) {String Savedpassword= mpref.getstring ("Password",NULL); if(Md5utils.encode (password). Equals (Savedpassword)) {Toast.maketext (homeactivity. This, "Successful Landing", Toast.length_short). Show ();                    Dialog.dismiss (); } Else{toast.maketext (homeactivity). This, "Bad password", Toast.length_short). Show (); }                } Else{toast.maketext (homeactivity). This, "Input cannot be empty", Toast.length_short). Show ();        }            }        }); Btncancel.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubDialog.dismiss ();        }        });    Dialog.show (); }}
 PackageCom.org.demo.wangfeng.demo;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException; Public classMd5utils {/*** MD5 Encryption * *@paramPassword *@return     */     Public Staticstring Encode (string password) {Try{MessageDigest instance= Messagedigest.getinstance ("MD5");//get MD5 Algorithm Object            byte[] Digest = Instance.digest (Password.getbytes ());//encrypt string, return byte arrayStringBuffer SB=NewStringBuffer ();  for(byteb:digest) {                inti = b & 0xFF;//gets the low eight-bit valid value of the byteString hexstring = integer.tohexstring (i);//Convert integer to 16 binary                if(Hexstring.length () < 2) {hexstring= "0" + hexstring;//if it's a 1-bit, fill 0.} sb.append (hexstring); }            returnsb.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); //without the algorithm, throw an exception and not go here        }        return""; }}
<?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:background= "#fff"android:orientation= "Vertical" > <TextView Android:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "#66ff6600"android:gravity= "Center"android:padding= "10DP"Android:text= "Set Password"Android:textcolor= "@android: Color/black"android:textsize= "20sp"/> <EditText Android:id= "@+id/et_password"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password"Android:inputtype= "Textpassword" > </EditText> <EditText Android:id= "@+id/et_password_confirm"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter your password again"Android:inputtype= "Textpassword" > </EditText> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button Android:id= "@+id/btn_ok"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "OK"/> <Button Android:id= "@+id/btn_cancel"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Cancel"/> </LinearLayout></LinearLayout>
View Code

In fact, the IF (Text ==null | | | text.length () ==0) in Android is encapsulated.
In the android.text.TextUtils.

public static Boolean isEmpty (Charsequence str) {        if (str = = NULL | | str.length () = = 0)            return true;        else            return false;    } So we can use
Textutils.isempty (text)

Replace

if (text = = NULL | | text.length () = = 0)

MD5 Simple Example

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.