php - 如何在数据元素中创建 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);
解决方案
推荐阅读
- visual-studio-code - 如何在 VS 代码中获取搜索结果中的文件列表?
- c++ - 为什么重载分辨率不选择我的模板函数的 std::vector 重载?
- android - 构建和运行时出现 Ionic Cordova 错误
- sql - 保留 hive 表中的最新记录
- javascript - React 调用其他文件的 fetch 方法,成功后执行操作
- powershell - Powershell 中的“$”符号有什么作用?
- mysql - 当数据库已使用新条目更新时,如何在 django 中获取推送通知?
- mysql - 如何使用 mysql 客户端连接到远程 mysql kubernetes pod
- javascript - 如何修复无法在组件上调用 setState,但它可能会在使用 React Js 的应用程序中出现错误
- r - R data.table 保留组的第一个非NA值直到组结束