---
title: "Android下面的MD5加密"
author: "zhouxiaoli521"
date: "2013-04-08T12:17:31Z"
tags: []
categories: ["移动开发"]
slug: "android-md5"
draft: false
lastmod: "2013-04-08T12:17:59Z"
id: 1843713
url: "https://mycoding.iteye.com/blog/1843713"
---

网上关于android下MD5加密的资料很多，但是测试了下总是跟网站的md5加密不一样， 后来才知道是编码方式不对，于是就自己写了一个。

 

```
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',  
        'A', 'B', 'C', 'D', 'E', 'F' };  
public static String toHexString(byte[] b) {  
    //String to  byte  
    StringBuilder sb = new StringBuilder(b.length * 2);    
    for (int i = 0; i < b.length; i++) {    
        sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);    
        sb.append(HEX_DIGITS[b[i] & 0x0f]);    
    }    
    return sb.toString();    
}  
public String md5(String s) {  
    try {  
        // Create MD5 Hash  
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");  
        digest.update(s.getBytes());  
        byte messageDigest[] = digest.digest();  
                                  
        return toHexString(messageDigest);  
    } catch (NoSuchAlgorithmException e) {  
        e.printStackTrace();  
    }  
                          
    return "";  
}  
```

 

http://www.cnblogs.com/top5/archive/2012/05/05/2484939.html