The code is as follows:
1 /**2 * MD5 One-way encryption, 32-bit, used to encrypt passwords, because plaintext passwords are not safe to transmit in the channel, and plaintext is not safe to be stored locally3 *4 * @paramStr5 * @return6 */7 Public Staticstring MD5 (String str) {8MessageDigest MD5 =NULL;9 Try {TenMD5 = messagedigest.getinstance ("MD5"); One}Catch(Exception e) { A e.printstacktrace (); - return""; - } the - Char[] Chararray =Str.tochararray (); - byte[] ByteArray =New byte[chararray.length]; - + for(inti = 0; i < chararray.length; i++) { -Bytearray[i] = (byte) chararray[i]; + } A byte[] Md5bytes =md5.digest (byteArray); at -StringBuffer Hexvalue =NewStringBuffer (); - for(inti = 0; i < md5bytes.length; i++) { - intval = ((int) Md5bytes[i]) & 0xFF; - if(Val < 16) { -Hexvalue.append ("0"); in } - Hexvalue.append (Integer.tohexstring (Val)); to } + returnhexvalue.tostring (); -}
Android Learning Note----Java string MD5 encryption