【问题标题】:Openssl Command Line for Triple DES HMAC like C# MACTripleDES用于 Triple DES HMAC 的 Openssl 命令行,例如 C# MACTripleDES
【发布时间】:2020-07-30 04:29:20
【问题描述】:

谁能解释如何在 OpenSSL 命令行中制作 TDES MAC?

我正在尝试在 C 中为 OpenSSL API 复制工作 C# 程序的某些功能,但在 openssl 中复制 .Net MACTripleDES.ComputeHash 函数时遇到问题。这是一个带有伪造数据和密钥的示例:

        using (MACTripleDES hmac = new MACTripleDES(Utilities.HexStringToByteArray("112233445566778899aabbccddeeff00")))
        {
            // Compute the hash of the input file.
            byte[] hashValue = hmac.ComputeHash(Utilities.HexStringToByteArray("001000000000000000000000000000008000000000000000"));
            string signature = Utilities.ByteArrayToHexString(hashValue);
            PrintToFeedback("Bogus Signature = " + signature);
        }

结果是“Bogus Signature = A056D11063084B3E” 我的新 C 程序必须提供该数据的相同散列,以便与其更广泛的环境进行互操作。但是在 openSSL 中执行此操作的方法让我难以理解。这表明 openssl 数据开始时与 C# 数据相同:

cmd>od -tx1 bsigin
0000000 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0000020 80 00 00 00 00 00 00 00

字符串化,001000000000000000000000000000008000000000000000 匹配 c# 字符串。

cmd>openssl dgst -md5 -mac hmac -macopt hexkey:112233445566778899aabbccddeeff00 bsigin
HMAC-MD5(bsigin)= 7071d693451da3f2608531ee43c1bb8a

该数据太长,我预期的数据不是子字符串。 -sha1 等也一样。我尝试分别加密和制作摘要,不好。 MS 没有说它做了什么样的哈希,我也找不到关于如何在 openssl 中使用 TDES 设置 MAC 的文档。

所以我希望这里有人对这两个平台有足够的了解,能给我一个体面的提示。

【问题讨论】:

标签: c# openssl tripledes system.security message-authentication-code


【解决方案1】:

命令行回答:

cmd>openssl enc -des-ede-cbc -K 112233445566778899aabbccddeeff00 -iv 0000000000000000 -in bsigin -out bsigout
cmd>od -tx1 bsigout
0000000 7c de 93 c6 5f b4 03 21 aa c0 89 b8 ae f3 da 5d
0000020 a0 56 d1 10 63 08 4b 3e 4c 03 41 d6 dd 9e e4 32
        ^^^^^^^^^^^^^^^^^^^^^^^

即命令行形式返回32字节,字节16..23包含hmac。

API 答案:

    DES_key_schedule SchKey1,SchKey2;
    DES_cblock iv = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    DES_set_key((C_Block *)Key1, &SchKey1);
    DES_set_key((C_Block *)Key2, &SchKey2);
    DES_ede3_cbc_encrypt( (unsigned char*)input_data, (unsigned char*)cipher, inputLength, &SchKey1, &SchKey2, &SchKey1, &iv, DES_ENCRYPT); 

其中 Key1 是 Lkey 或 16 字节 TDES 密钥的左 8 字节,Key2 是 Rkey 或 16 字节 TDES 密钥的右 8 字节。此调用仅填充 24 字节的密码,而不是命令行版本的 32 字节返回。您仍然需要字节 16..23。希望支持声明是直观的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-27
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多