1, in the tool class under the package to create a new MD5 encryption tool class Md5utils.java
1 Packagecom.example.mobilesafe.utils; 2 Importjava.security.MessageDigest; 3 Importjava.security.NoSuchAlgorithmException; 4 Public classMd5utils {5 /** 6 * MD5 Encryption Method7 * @paramPassword8 * @return 9 */ Ten Public Staticstring Md5password (string password) { One Try { A //get a message digest -MessageDigest digest = messagedigest.getinstance ("MD5"); - byte[] result =digest.digest (Password.getbytes ()); theStringBuffer buffer =NewStringBuffer (); - //to make each byte one with the arithmetic 0xff,0xff is hexadecimal, decimal is 255 - for(byteB:result) { - //and arithmetic + intNumber = B & 0xFF; -String str =integer.tohexstring (number); + //System.out.println (str); A //if the number of digits is not enough, add a 0 . at if(Str.length () ==1){ -Buffer.append ("0"); - } - buffer.append (str); - } - //the result of standard MD5 encryption in returnbuffer.tostring (); -}Catch(nosuchalgorithmexception e) { to //TODO auto-generated Catch block + E.printstacktrace (); - return""; the } * } $}
2, in the Homeactivity.java to use sharedpreferences saved password data to call encryption
1 a change to MD5 encryption when you save a password to a file for the first time you set a password:2 if(Password.equals (password_confirm)) {3 //In the same way, save the password, the dialog box to eliminate, but also to enter the mobile phone anti-theft page4Editor Editor =Sp.edit (); 5Editor.putstring ("Password", Md5utils.md5password (password)); 6 Editor.commit (); 7 Dialog.dismiss (); 8LOG.I (TAG, "in the same words, save the Password, the dialog box out, but also to enter the mobile phone anti-theft page"); 9 } Ten One The other is to determine whether the MD5 encrypted data read from the file is the same as the current input data MD5 encrypted A if(Md5utils.md5password (password). Equals (Savepassword)) { - //The password you entered is the one I set before - //Erase the dialog box and go to the main page the Dialog.dismiss (); -LOG.I (TAG, "Erase the dialog box and go to the main page.")); -}
Android MD5 encryption of Sensitive data (Basic review)