我正在尝试解密文件。前 2 个步骤:
Copy the first 16 bytes of the file to a buffer. This is the HMAC-SHA1 hash of the file which is made using one of the keys above.
Use HMAC-SHA1 on that buffer with a key from above to create the RC4 key, which is 0x10 bytes.
我的代码是:
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
BinaryReader binread = new BinaryReader(File.Open(ofd.FileName, FileMode.Open));
byte[] RetailKey = { 0xE1, 0xBC, 0x15, 0x9C, 0x73, 0xB1, 0xEA, 0xE9, 0xAB, 0x31, 0x70, 0xF3, 0xAD, 0x47, 0xEB, 0xF3 };
HMACSHA1 SHA = new HMACSHA1(RetailKey); //Initalize HMAC w/ retail or development key
byte[] buffer = binread.ReadBytes(16);
buffer = SHA.ComputeHash(buffer);
MessageBox.Show(buffer.Length.ToString());
正如你所看到的,它说缓冲区必须是 10 个字节,但消息框说它是 20 个字节。我的错误在哪里?
最佳答案
SHA-1 和 HMAC-SHA-1 输出 20 个字节。
您只需要 16 (0x10) 个,因此需要截断。例如,使用 byte[] key = hmacSha1.ComputeHash(input).Take(16).ToArray()
。
0x10
中的 0x
是表示 c(及其派生语言)中的十六进制数字的前缀。所以 0x10
表示 16 而不是 10。
关于cryptography - C# HMACSHA1 哈希不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14684772/