1

我需要将 MACTripleDes 功能移植到另一种编程语言,并想知道它的真正作用。所以我有数据和一个关键词。

对 IV=0 和关键短语的数据进行 TripleDes 处理,然后将最后 8 个字节作为 MAC 是否正确?

乔以法莲

4

2 回答 2

2

DO NOT WRITE YOUR OWN CRYPTOGRAPHIC FUNCTIONS! Not even a direct port of an existing function.

If your language has any maturity, it will have an open source implementation that you can use. If it does not, DO NOT PORT ONE! Find one in another language and write a wrapper for it that you can call (like the C# implementation that you want to port).

Any existing open source implementation will be tested and verified for correctness by people who know far more about cryptography than me, you and over 99% of the global developer population.

If you really cannot find an open source implementation, write a simple .exe using C# that calls the C# implementation and returns the result. Not including setup and boilerplate, that's basically one line of code.

Again, if you write your own implementation, you will inevitably fail to account for some edge case or not understand the algorithm enough, resulting in a dangerous piece of code with exploitable flaws. Unless you are one of the original developers of 3DES or have many years of experience developing currently used encryption standards, you simply do not have the technical skill to port an implementation.

Again, if you write your own implementation, something goes wrong and your data gets leaked, you can and will be held personally accountable. Linkedin was recently sued for $ 5 million because they didn't implement proper security. This is something that can break your career.

DO NOT MAKE THE SAME MISTAKE AS LINKEDIN!

于 2014-09-19T11:35:42.980 回答
0

显然MACTripleDES类使用Triple DES作为底层分组密码来实现CBC-MAC 。如果您可以访问实现 CBC-MAC 和 Triple DES 的加密库(并允许您将后者用作前者中的分组密码,就像任何通用 CBC-MAC 实现一样),您应该能够将它们组合起来以获得等效的 MAC。

或者,如果您的加密库不直接实现 CBC-MAC,但确实实现了CBC 模式加密,您确实可以通过在 CBC 模式下加密消息、使用全零 IV 并获取最后一个块来自己实现 CBC-MAC得到的密文作为 MAC 值。(这只是 CBC-MAC 的定义。)自己实现 CBC 模式(或 CBC-MAC)也不是特别困难或棘手,只使用原始分组密码。但实际上,任何体面的加密库都应该已经内置了 CBC-MAC,或者至少是 CBC 模式加密。

您不应该尝试自己实现三重 DES 或任何其他分组密码!安全地实现像 Triple DES 这样的低级加密算法是非常困难的,因为您必须注意诸如侧信道攻击之类的事情,并且任何此类实现都应该在用于任何严重的事情之前由专业的密码学家进行彻底的测试和审查。

(这里的一个具体例外是,如果您只能访问普通DES分组密码,那么您可以在此基础上合理安全地实现三重 DES。这是因为三重 DES 实际上并不是一个单独的低级密码,而只是一个扩展 DES 密钥空间的方法,以牺牲性能为代价,通过使用独立密钥对每个块进行多次加密。但同样,过去 20 年编写的任何体面的加密库都应该支持三重 DES,如果它完全支持 DES。)

当然,即使只实现 CBC-MAC 这样的高级加密算法,仍然有可能出错,这样的错误会产生安全后果。但是在这个级别上,难度和风险并不比首先简单地使用加密(因此,隐含地使用加密密钥等安全关键数据)所固有的那些更大。即便如此,在部署代码之前让具有加密经验的人(或者最好是几个人)审查您的代码始终是一个好主意。即使您自己是加密货币专家,这一点也成立;任何人都可能犯错误,而且你对代码的关注越多,就越有可能在有人利用它们之前发现任何错误。


最后,请注意,如果您不特别需要与 Microsoft 的 MACTripleDES 类兼容,则 MAC 算法有比 CBC-MAC 更好的选择(例如CMAC),密码选择也比 Triple DES 更好(例如AES)。AES 和 CMAC 的具体组合甚至在RFC 4493中进行了标准化,它提供了详细的实现说明和测试向量来验证正确性。

特别是,如果消息长度未经过身份验证,纯 CBC-MAC对于可变长度消息是不安全的。虽然有一些方法可以修复此漏洞,例如通过在计算 MAC 之前将长度添加到消息中,但通常最好使用“开箱即用”的安全算法,如 CMAC。

(此外,由于您在问题中提到了“密钥短语”,我希望您在将其用作三重 DES(或任何其他分组密码)的密钥之前,实际上是通过适当的密钥派生函数来提供它。特别是,如果keyphrase 是用户提供的,因此可能具有低熵,您确实应该使用像PBKDF2scryptArgon2这样的密钥拉伸KDF 。)

于 2016-01-07T17:35:47.493 回答