public static String initMacKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); SecretKey secretKey = keyGenerator.generateKey(); return Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT); } public String encrypt_HMAC(String source, String key) throws Exception { SecretKey secretKey = new SecretKeySpec(Base64.decode(key, Base64.DEFAULT), "HmacMD5"); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return Base64.encodeToString(mac.doFinal(source.getBytes()), Base64.DEFAULT); } //使用 try{ String key=initMacKey(); String result=encrypt_HMAC("哈哈哈", key); }catch(Exception e){ }
原文地址:http://blog.csdn.net/u013425527/article/details/41924473