MD5给密码加密

String password = xxx.getPassword("123456");

  /**  * 获取加密后的密码  *  * @param info  * @return  */  public String getPassword(String info) {
        if (TextUtils.isEmpty(info)) {
            throw new NullPointerException("password can not be null");
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append(info).append(getSHA1(info)).append(info);
        return getMD5(buffer.toString());
    }
/**  * MD5加密  *  * @param info  * @return  */ private String getMD5(String info) {
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(info.getBytes("UTF-8"));
        byte[] hash = md5.digest();
        StringBuffer buffer = new StringBuffer();
        for (byte b : hash) {
            if (Integer.toHexString(0xFF & b).length() == 1) {
                buffer.append("0");
            }
            buffer.append(Integer.toHexString(0xFF & b));
        }
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}
/**  * SHA1加密  *  * @param info  * @return  */ private String getSHA1(String info) {

    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(info.getBytes());
        byte[] hash = sha1.digest();
        StringBuffer buffer = new StringBuffer();
        for (byte b : hash) {
            if (Integer.toHexString(0xFF & b).length() == 1) {
                buffer.append("0");
            }
            buffer.append(Integer.toHexString(0xFF & b));
        }
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

你可能感兴趣的:(MD5给密码加密)