HmacSHA256簽名加Base64編碼加URL編碼


/**
*先使用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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM