我正在尝试获得与 C#MACTripleDES
类等效的 MAC TripleDES。
我试过关注mcrypt()
,但这只是在 TripleDES 中编码。我需要获得一个等效的 MACTripleDES 字符串作为在 C# 中生成的字符串来验证消息。
我还查看了 PHP 的hash_hmac()
函数,但它没有提供使用 TripleDES 生成 MAC 的选项
我正在尝试获得与 C#MACTripleDES
类等效的 MAC TripleDES。
我试过关注mcrypt()
,但这只是在 TripleDES 中编码。我需要获得一个等效的 MACTripleDES 字符串作为在 C# 中生成的字符串来验证消息。
我还查看了 PHP 的hash_hmac()
函数,但它没有提供使用 TripleDES 生成 MAC 的选项
我不确定,因为微软没有费心说他们的类符合什么标准,但我怀疑这个 NIST 文档是微软类正在计算的,只使用三重 DES 代替 DES。
我想您将不得不使用 mcrypt 中的原语编写自己的方法。
编辑1:
受赏金的启发,我有这两个示例显示了 PHP 和 C# 中的等效结果。
首先,C#:
using System;
using System.Text;
using System.Security.Cryptography;
namespace TDESMacExample
{
class MainClass
{
public static void Main (string[] args)
{
var keyString = "012345678901234567890123";
var keyBytes = Encoding.ASCII.GetBytes(keyString);
var mac = new MACTripleDES(keyBytes);
var data = "please authenticate me example number one oh one point seven niner";
Console.WriteLine(data.Length);
var macResult = mac.ComputeHash(Encoding.ASCII.GetBytes(data));
Console.WriteLine(BitConverter.ToString(macResult));
// B1-29-14-74-EA-E2-74-2D
}
}
}
接下来,PHP:
<?php
$data = 'please authenticate me example number one oh one point seven niner';
$key = '012345678901234567890123'; // Key must be 24 bytes long
$iv = '\x00\x00\x00\x00\x00\x00\x00\x00'; // All zero IV is required
$cipher = mcrypt_cbc(MCRYPT_3DES, $key, $data, MCRYPT_ENCRYPT, $iv);
$mac_result = substr($cipher, -8); // Last 8 bytes of the cipher are the MAC
echo "mac result : " . bin2hex($mac_result);
echo "<br>";
?>
MAC 只是 CBC 加密数据的最后八个字节。如果键、IV 和填充方法匹配,您应该能够只使用这些字节。
有关 MAC 定义的更多详细信息,请参阅FIPS-81 的附录 F,DES 操作模式。