Android 实现SHA1加密算法代码

来源:互联网 发布:多普达a6288软件 编辑:程序博客网 时间:2024/05/21 06:54

实现SHA1加密代码

    public static String getSecurityAppKey() {                return encryptToSHA(RequestTools.AppId + "UZ" +                        RequestTools.AppKey + "UZ" + System.currentTimeMillis()) +                        "." + System.currentTimeMillis();            }    public static String encryptToSHA(String info) {        byte[] digesta = null;        try {            MessageDigest alga = MessageDigest.getInstance("SHA-1");            alga.update(info.getBytes());            digesta = alga.digest();        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        }        String rs = byte2hex(digesta);        return rs;    }    public static String byte2hex(byte[] b) {        String hs = "";        String stmp = "";        for (int n = 0; n < b.length; n++) {            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));            if (stmp.length() == 1) {                hs = hs + "0" + stmp;            } else {                hs = hs + stmp;            }        }        return hs;    }

使用方法按照getSecurityAppKey方法使用,将要加密的字串写到encryptToSHA中即可!

0 0