Android mobile guard-md5 encryption process, android-md5
In the previous article, we stored the user's password using SharedPreferences, and opened/data/com. wuyudong. the config. import the xml file to the local device to view the content:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map> <string name="mobile_safe_psd">123</string> <boolean name="open_update" value="false" /></map>
The password is in plain text, which is very insecure. Md5 encryption is used here
For more information, see http://www.cnblogs.com/wuyudong/p/5941131.html.
Compile the Md5Util tool class with the following code:
Package com. wuyudong. mobilesafe. utils;/*** Created by wuyudong on 2016/10/9. */import java. security. messageDigest; import java. security. noSuchAlgorithmException; public class Md5Util {/*** encrypt the specified String according to the md5 Algorithm ** @ param psd requires encryption of the password and salt Processing * @ return md5 String */public static String encoder (String psd) {try {// salt Processing psd = psd + "mobilesafe"; // 1, specify the encryption algorithm type MessageDigest = MessageDigest. getInstance ("MD5"); // 2, convert the string to be encrypted to an array of the byte type, and then perform the random hash process byte [] bs = digest. digest (psd. getBytes (); // 3. cyclically traverse bs, and then let it generate a 32-Bit String, fixed writing method // 4. During String concatenation, StringBuffer stringBuffer = new StringBuffer (); for (byte B: bs) {int I = B & 0xff; // int type I needs to be converted to a 16-mechanism character String hexString = Integer. toHexString (I); if (hexString. length () <2) {hexString = "0" + hexString;} stringBuffer. append (hexString);} // 5, print the test System. out. println (stringBuffer. toString (); return stringBuffer. toString ();} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} return "";}}
Md5 encryption: converts a string to a 32-Bit String (hexadecimal character (0 ~ F) irreversible
For example, after 123 encryption: 202cb962ac59075b964b07152d234b70
Next, you can directly call the Encryption Class and perform encryption comparison in the "set password" and "Confirm Password" dialog boxes. The specific code is as follows:
/*** Confirm Password dialog box */private void showConfirmPsdDialog () {// you need to define the display style of the dialog box by yourself, so you need to call dialog. setView (view); Builder builder = new Builder (this); final AlertDialog dialog = builder. create (); final View view = inflate (this, R. layout. dialog_confirm_psd, null); // Let the dialog box display a custom dialog box interface effect dialog. setView (view); dialog. show (); Button bt_submit = (Button) view. findViewById (R. id. bt_submit); Button bt_cancel = (Button) View. findViewById (R. id. bt_cancel); bt_submit.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {EditText et_confirm_psd = (EditText) view. findViewById (R. id. et_confirm_psd); String confirmPsd = et_confirm_psd.getText (). toString (); if (! TextUtils. isEmpty (confirmPsd) {// obtain the 32-bit password stored in the sp, and then md5 the entered password, then compare it with the storage password in the sp to String psd = SpUtil. getString (getApplicationContext (), ConstantValue. MOBILE_SAFE_PSD, ""); if (psd. equals (Md5Util. encoder (confirmPsd) {// enter the user's mobile phone anti-theft module and enable a new activity Intent intent = new Intent (getApplicationContext (), testActivity. class); startActivity (intent); // after you jump to the new interface, you need to hide the dialog box dialog. dismiss ();} else {ToastUtil. Show (getApplicationContext (), "Incorrect password entered") ;}} else {// prompt ToastUtil when the user password is empty. show (getApplicationContext (), "Enter Password") ;}}); bt_cancel.setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {dialog. dismiss () ;}}) ;}/ *** SET Password dialog box */private void showSetPsdDialog () {// you need to define the display style of the dialog box, so you need to call dialog. setView (view); Builder builder = new Builder (this); final AlertDialog Dialog = builder. create (); final View view = inflate (this, R. layout. dialog_set_psd, null); // Let the dialog box display a custom dialog box interface effect dialog. setView (view); dialog. show (); Button bt_submit = (Button) view. findViewById (R. id. bt_submit); Button bt_cancel = (Button) view. findViewById (R. id. bt_cancel); bt_submit.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {EditText et_set_psd = (EditTex T) view. findViewById (R. id. et_set_psd); EditText et_confirm_psd = (EditText) view. findViewById (R. id. et_confirm_psd); String psd = et_set_psd.getText (). toString (); String confirmPsd = et_confirm_psd.getText (). toString (); if (! TextUtils. isEmpty (psd )&&! TextUtils. isEmpty (confirmPsd) {// enter the user's mobile phone anti-theft module if (psd. equals (confirmPsd) {Intent intent = new Intent (getApplicationContext (), testActivity. class); startActivity (intent); // after you jump to the new interface, you need to hide the dialog box dialog. dismiss (); SpUtil. putString (getApplicationContext (), ConstantValue. MOBILE_SAFE_PSD, Md5Util. encoder (psd);} else {ToastUtil. show (getApplicationContext (), "inconsistent passwords") ;}} else {// prompt that the user password is empty ToastUtil. show (getApplicationContext (), "Enter Password") ;}}); bt_cancel.setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {dialog. dismiss ();}});}