直接上代码
importClass(javax.crypto.spec.SecretKeySpec);
importClass(javax.crypto.Mac);
/*
*@param encryptText 待加密的字符串
*@param encryptKey 秘钥
*@return 返回加密后的字符串
*/
function HmacSHA256Encrypt(encryptText, encryptKey) {
let rawHmac;
log("encryptText = " + encryptText);
log("encryptKey = " + encryptKey);
encryptText = java.lang.String(encryptText);
encryptKey = java.lang.String(encryptKey);
let data = encryptKey.getBytes("UTF-8");
let secretKey = new SecretKeySpec(data, "hmacsha256");
let mac = Mac.getInstance("hmacsha256");
mac.init(secretKey);
let text = encryptText.getBytes("UTF-8");
rawHmac = mac.doFinal(text);
let oauth = android.util.Base64.encodeToString(rawHmac, 2);
return oauth;
}
log(HmacSHA256Encrypt("测试","QQ7323650"))