网上关于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 "";  
}  

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-01-07
  • 2022-01-16
  • 2021-12-15
相关资源
相似解决方案