Android开发之MD5加密

将字符串进行MD5加密,返回加密后的字符串

 1 public static String encode(String password) {
 2         try {
 3             StringBuffer sb = new StringBuffer();
 4             MessageDigest instance = MessageDigest.getInstance("MD5");
 5             byte[] digest = instance.digest(password.getBytes());
 6             for (byte b : digest) {
 7                 int i = b & 0xff; // 获取到第八位有效值,加盐处理
 8                 String hexString = Integer.toHexString(i);
 9                 if (hexString.length() < 2) {
10                     hexString = "0" + hexString;
11                 }
12                 sb.append(hexString);
13             }
14             return sb.toString();
15         } catch (NoSuchAlgorithmException e) {
16             e.printStackTrace();
17             return "";
18         }
19     }

在线测试:http://www.cmd5.com/

参考:http://www.tuicool.com/articles/Q7RNJbu

posted @ 2016-03-24 16:39  熠然  阅读(799)  评论(0编辑  收藏  举报