Android MD5 encryption of Sensitive data (Basic review)

Source: Internet
Author: User
Tags arithmetic md5 encryption

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)

Related Article

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.