1. 程式人生 > >HmacSHA256簽名加Base64編碼加URL編碼

HmacSHA256簽名加Base64編碼加URL編碼

invalid algo () public 密鑰 body Go div nature

/**
*先使用HmacSHA256簽名,再使用Base64編碼,最後進行URL 編碼
*signatureReqStr : 待加密data
* secretKey : 密鑰
*/
public static String getSignature(String signatureReqStr,String secretKey){
Mac sha256_HMAC ;
String result = "";
try {
sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
result = Base64.encodeBase64String(sha256_HMAC.doFinal(signatureReqStr.getBytes()));
result = URLEncoder.encode(result);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}

HmacSHA256簽名加Base64編碼加URL編碼