Android 获取HMAC-MD5 加密

来源:互联网 发布:下载软件下载 编辑:程序博客网 时间:2024/06/04 19:02
public static String getHmacMd5Str(String s, String keyString){        String sEncodedString = null;        try        {            SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5");            Mac mac = Mac.getInstance("HmacMD5");            mac.init(key);            byte[] bytes = mac.doFinal(s.getBytes("ASCII"));            StringBuffer hash = new StringBuffer();            for (int i=0; i<bytes.length; i++) {                String hex = Integer.toHexString(0xFF &  bytes[i]);                if (hex.length() == 1) {                    hash.append('0');                }                hash.append(hex);            }            sEncodedString = hash.toString();        }        catch (UnsupportedEncodingException e) {}        catch(InvalidKeyException e){}        catch (NoSuchAlgorithmException e) {}        return sEncodedString ;}

0 0
原创粉丝点击