首页 > 解决方案 > 如何在数据元素中创建 ValueMac

问题描述

我正在尝试使用 AES-CBC-128 加密创建一个 RFC6030 PSKC XML 文件以导入 totp 令牌(https://www.rfc-editor.org/rfc/rfc6030#section-6)。

我能够生成 mackey 和秘密的密码值。

$encryptionkey =  hex2bin("12345678901234567890123456789012");

######GENERATE ENCRYPTED MAC KEY
$ivhmac = hex2bin("11223344556677889900112233445566");
$mackey = hex2bin("1122334455667788990011223344556677889900");

$ciphertext_raw_mac = openssl_encrypt($mackey, $cipher="AES-128-CBC", $encryptionkey, $options=OPENSSL_RAW_DATA, $ivhmac);
$ciphertext_mac = $ivhmac.$ciphertext_raw_mac;
echo "ciphertext_raw = ".base64_encode ($ciphertext_raw_mac)."<br>";
echo "ciphertext = ".base64_encode($ciphertext_mac)."<br>";
echo "<br><br><br>";

//ciphertext_raw = AqfpaMtpMLWxt4ExK8iFF6M328lrYE6VJoytHsjnBJc=
//ciphertext = ESIzRFVmd4iZABEiM0RVZgKn6WjLaTC1sbeBMSvIhRejN9vJa2BOlSaMrR7I5wSX


######GENERATE ENCRYPTED Secret
$ivsecret = hex2bin("000102030405060708090a0b0c0d0e0f");
$secret =  hex2bin("3132333435363738393031323334353637383930");


$ciphertext_raw_secret = openssl_encrypt($secret, $cipher="AES-128-CBC", $encryptionkey, $options=OPENSSL_RAW_DATA, $ivsecret);
$ciphertext_secret = $ivsecret.$ciphertext_raw_secret;
echo "ciphertext_raw = ".base64_encode ($ciphertext_raw_secret)."<br>";
echo "ciphertext = ".base64_encode($ciphertext_secret)."<br>";
echo "<br><br><br>";

//ciphertext_raw = 5wgci2UHdatrUNSnFW87HaV5/VWY08IyXyPBmohSoa8=
//ciphertext = AAECAwQFBgcICQoLDA0OD+cIHItlB3Wra1DUpxVvOx2lef1VmNPCMl8jwZqIUqGv

但我不明白它<ValueMac>是如何创建的。有人说我必须从 mac-key 和秘密创建密码的 hmac (sha1) 并将其转换为 base64。

######GENERATE HMAC 
$hmac = hash_hmac('sha1', $ciphertext_raw_mac.$ciphertext_raw_secret, $encryptionkey, $as_binary=true);
echo "hmac = ".base64_encode ($hmac)."<br>";
echo "<br><br><br>";
//hmac = Sk+T1Kky0ix4+MFKYz9yiSk/qA8=

但我<ValueMac>和你在示例https://www.rfc-editor.org/rfc/rfc6030#section-6.1中看到的不一样(第 24 页,见下文)

<ValueMAC>Su+NvtQfmvfJzF6bmQiJqoLRExc=</ValueMAC>

我尝试的一切都无法获得这个值。

希望有人可以帮助我并解释它。

我知道了!生成 HMAC 的解决方案如下:

$hmac = hash_hmac('sha1', $ivsecret.$ciphertext_raw_secret, $mackey, $as_binary=true);

标签: phpxmlencryption

解决方案


推荐阅读