【问题标题】:Adding an integer octet to a byte array将整数八位字节添加到字节数组
【发布时间】:2011-06-27 21:51:16
【问题描述】:

我正在尝试实施 Salted Challenge Response Authentication Mechanism (RFC 5802),但遇到了一些问题。

Hi(str, salt, i):

U1   := HMAC(str, salt + INT(1))
U2   := HMAC(str, U1)
...
Ui-1 := HMAC(str, Ui-2)
Ui   := HMAC(str, Ui-1)

Hi := U1 XOR U2 XOR ... XOR Ui

where "i" is the iteration count, "+" is the string concatenation
operator, and INT(g) is a 4-octet encoding of the integer g, most
significant octet first.

我不确定如何添加 INT(1)。我有一个用于盐的字节数组。我需要做的只是位移 1 并将其添加到数组的末尾吗?

【问题讨论】:

  • 谢谢。我应该考虑建立一个链接。
  • 在我看来它只是二进制序列00,00,00,01。这是整数 1 的 4 字节编码,最重要的字节在前。

标签: c# arrays integer byte rfc


【解决方案1】:

您不能向数组中添加任何内容。由于数组是固定大小的,您需要为结果创建一个新数组。使用BitConverter类获取整数的二进制表示:

// create new array
byte[] key = new byte[salt.Length + 4];
// copy salt
Array.Copy(salt, key, salt.Length);
// create array from integer
byte[] g = BitConverter.GetBytes(1);
if (BitConverter.IsLittleEndian) {
  Array.Reverse(g);
}
// copy integer array
Array.Copy(g, 0, key, salt.Length, 4);

【讨论】:

  • 谢谢。看看它有一种更简单的方法,但这很有效。
  • @Coder2000:是的,还有其他方法可以做到这一点,例如使用扩展方法合并数组中的数据并将其转换为新数组。
  • 我想更多的是 Array.Copy(new byte[] { 0,0,0,1 }, 0, salt.Length, 4);
  • @Coder2000:您甚至可以利用数组在创建时填充为零的事实,因此您不必复制零字节。您只需 key[salt.Length + 3]++; 就可以逃脱 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多