Android MD5加密

来源:互联网 发布:网络招聘司机 编辑:程序博客网 时间:2024/06/10 10:04
public class MD5Encryptor {    private static final String TAG = "MD5Encryptor";//信息输出头    private Context mContext;//上下方    public MD5Encryptor(Context context) {        this.mContext = context;    }    public String MD5_Encryptor(String string) {        byte[] hash;        try {            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));        } catch (NoSuchAlgorithmException e) {            throw new RuntimeException("Huh, MD5 should be supported?", e);        } catch (UnsupportedEncodingException e) {            throw new RuntimeException("Huh, UTF-8 should be supported?", e);        }        StringBuilder hex = new StringBuilder(hash.length * 2);        for (byte b : hash) {            if ((b & 0xFF) < 0x10) hex.append("0");            hex.append(Integer.toHexString(b & 0xFF));        }        return hex.toString();    }

}

//调用

public MD5Encryptor mMD5Encryptor;
mMD5Encryptor = new MD5Encryptor(mContext);//添加MD5对象
String md5str = mMD5Encryptor.MD5_Encryptor("123");Log.e(TAG,"Md5加密结果:"+md5str.toUpperCase());



0 0
原创粉丝点击