在java和SHA256中签名一个字符串

时间:2017-02-01 12:43:45

标签: java encryption sha256

我正在尝试几天(!)用SHA256用密钥签名以下字符串:“aaa”结果应该是(URL endcoded): kvoJVZMPomuGyYP3cmiJkYz2L3usuZNfh2f9LaUxO3U%3D

有人可以帮忙吗?

GET
sellercentral.amazon.com
/gp/mws/registration/register.html
AWSAccessKeyId=AKIAFJPPO5KLY6G4XO7Q&SignatureMethod=HmacSHA256&SignatureVersion=2&id=1014f5ad-c359-4e86-8e50-bb8f8e431a9e&returnPathAndParameters=%2Forders%2FlistRecentOrders.jsp%3FsessionId%3D123

这是我正在使用的代码,但我无法得到相同的结果:

private static final String QUERY = "GET\n"+
        "sellercentral.amazon.com\n"+
        "/gp/mws/registration/register.html\n"+
        "AWSAccessKeyId=AKIAFJPPO5KLY6G4XO7Q&SignatureMethod=HmacSHA256&SignatureVersion=2&id=1014f5ad-c359-4e86-8e50-bb8f8e431a9e&returnPathAndParameters=%2Forders%2FlistRecentOrders.jsp%3FsessionId%3D123";


    public static void main(String[] args) throws Exception {
         System.out.println(encode("aaa", QUERY));
    }

public static String encode(String key, String data) throws Exception {
      Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
      SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
      sha256_HMAC.init(secret_key);
      return Base64.encodeBase64String(sha256_HMAC.doFinal(data.getBytes()));
}

1 个答案:

答案 0 :(得分:0)

如果他们获得IRP(集成注册管道)的亚马逊文档,只是为了帮助他人 - 请注意他们签署查询字符串的示例结果是错误的。