cryptography - C# HMACSHA1 哈希不正确

标签 cryptography

我正在尝试解密文件。前 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/

相关文章:

c++ - HMACSHA1 减少 2 个字节

java - 雅可比坐标下的椭圆曲线点加法

php - 使用 PHP 生成公钥/私钥对并将公钥导出为 .der 编码的字符串

python - 从python客户端发送加密消息到nodejs服务器

python - ECC - 无法生成整个循环组

java - 尝试比较我的两个哈希值。两者都是大整数形式

objective-c - 如何在 Cocoa 应用程序中存储敏感数据(例如密码、API key )?

python - 大消息RSA加密和解密不正确

c++ - 在 C++ 中生成 ECDSA key 对

javascript - 错误: Unable to get property 'createEncryptor' of undefined or null reference