【问题标题】:BigInteger HMAC conversion anomalieBigInteger HMAC 转换异常
【发布时间】:2013-06-26 10:23:57
【问题描述】:

我有钥匙:

byte: 0011100101010111000101111100101101100010100010111001010010000000

一条消息:

byte: 10110001

hmac 十六进制输出:

HMAC-SHA256:    ca3871e40207fc0cd66558e4e4fa2817d283da605c15e4c941ff7945ad4be29f

因此,如果我创建一个新的 BigInteger(测试),例如:

BigInteger test = new BigInteger(hmac);

如果我想取回十六进制值:

System.out.println("\noutput: " + test.toString(16));

我明白了:

output: -35c78e1bfdf803f3299aa71b1b05d7e82d7c259fa3ea1b36be0086ba52b41d61

问题出在哪里?或者我如何从消息中计算 hmac-sha256 并从一开始就使用 BigIntegers。

key、message 和 hmac 是字节数组。

如果我尝试将字节“11010100”转换为 BigInteger,它会转换为负数“-101100” 为什么 BigInteger 将其转换为负数?第一位是符号位,如何禁用符号位?

【问题讨论】:

  • hmac变量的确切值是多少?
  • ca3871e40207fc0cd66558e4e4fa2817d283da605c15e4c941ff7945ad4be29f
  • 我无法重现您的问题,因为您发布的代码抛出了 NumberFormatException...
  • 显示您正在使用的代码。我也无法重现您在编辑中写的内容...
  • 不,我已经完全运行了您评论中的代码,结果是肯定的。发布一个重现您的问题的最小示例。 main 方法中的几行代码就可以完成这项工作。

标签: java hex biginteger hmac


【解决方案1】:

有一个 BigInteger 构造函数用于分别设置符号和大小,而不是默认的期望 2s 补码位串。见here

例子:

Bmsg[i] = new BigInteger(1, msg[i]);

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2019-03-08
    • 1970-01-01
    • 2015-06-12
    相关资源
    最近更新 更多