【问题标题】:AES de/encryption in C and PHPC 和 PHP 中的 AES 解密/加密
【发布时间】:2019-05-30 23:35:23
【问题描述】:

这是我用作示例的代码

Aes128KeyLength = 128/8;

    //
    // Allocate Key buffer
    //

    Aes128Key = (PBYTE) HeapAlloc( GetProcessHeap(), 0, Aes128KeyLength);
    if( NULL == Aes128Key )
    {
        Status = STATUS_NO_MEMORY;
        ReportError(Status);
        goto cleanup;
    }

    //
    // Derive the AES 128 key from the password 
    // Using PBKDF2
    //

    //
    // Open an algorithm handle
    //

    Status = BCryptOpenAlgorithmProvider(
                                        &KdfAlgHandle,              // Alg Handle pointer
                                        BCRYPT_PBKDF2_ALGORITHM,    // Cryptographic Algorithm name (null terminated unicode string)
                                        NULL,                       // Provider name; if null, the default provider is loaded
                                        0);                         // Flags
    if( !NT_SUCCESS(Status) )
    {
        ReportError(Status);
        goto cleanup;
    }


    //
    // Create a key handle to the password
    //

    Status = BCryptGenerateSymmetricKey(
                                        KdfAlgHandle,               // Algorithm Handle 
                                        &Aes128PasswordKeyHandle,   // A pointer to a key handle
                                        NULL,                       // Buffer that recieves the key object;NULL implies memory is allocated and freed by the function
                                        0,                          // Size of the buffer in bytes
                                        (PBYTE)Aes128Password,      // Buffer that contains the key material
                                        sizeof (Aes128Password),    // Size of the buffer in bytes
                                        0);                         // Flags
    if( !NT_SUCCESS(Status) )
    {
        ReportError(Status);
        goto cleanup;
    }


    //
    // Derive AES key from the password
    //

    Status = BCryptKeyDerivation(
                                        Aes128PasswordKeyHandle,    // Handle to the password key
                                        &PBKDF2Parameters,          // Parameters to the KDF algorithm
                                        Aes128Key,                  // Address of the buffer which recieves the derived bytes
                                        Aes128KeyLength,            // Size of the buffer in bytes
                                        &ResultLength,              // Variable that recieves number of bytes copied to above buffer  
                                        0);                         // Flags
    if( !NT_SUCCESS(Status) )
    {
        ReportError(Status);
        goto cleanup;
    }

我在 PHP 端使用hash_pbkdf2 函数来做同样的事情。 在 PHP 中我添加了echo hash_pbkdf2("sha256","PASSWORD", $salt,1000, 16, TRUE);

这是什么原因?我已经尝试了我在网上找到的各种标准测试,但输出仍然不一样。我看不出我可能在哪里搞砸了。对于 C 代码中的迭代次数是 1000 以及 PHP 端的相同值。我传递给函数的所有值在 PHP 和 C 端都是相同的。但是 C 和 PHP 端的输出派生键不一样?我做错了什么还是我应该注意一些能力问题?

【问题讨论】:

  • 阅读minimal reproducible example然后编辑您的问题以包含代码可能会有所帮助。
  • bcrypt 和 PBKDF2 是不同的算法。
  • @Peter 我没有使用 bcrypt....这是docs.microsoft.com/en-us/windows/desktop/api/bcrypt
  • 如果没有完全复制这两个代码,有点难以激发对此进行调查。
  • @DavidHeffernan 代码在 PHP 端是完全一样的,这让我发疯。我根本看不到。我在做什么是不同的。我通过 hex2bin(“此处来自 C 代码的盐”)传递给 php 函数的盐。一切都是一样的,我不知道这里发生了什么。

标签: php c winapi cryptography cryptoapi


【解决方案1】:

除了您提到的迭代之外,盐还会影响生成的密钥。

以下我在 php 和 C++ (sample) 中使用相同的盐 'a' / 0x61

<?php
$password = "PASSWORD";
$iterations = 1000;
$salt = 'a'; //0x61

$hash = hash_pbkdf2("sha256", $password, $salt, $iterations, 16);
echo $hash;
?>

因此,如果您使用与 CipherEncryptionDecryption.cpp 中相同的盐值,您将得到相同的结果。

static const
BYTE Salt [] =
{
    0x61
};

两者都给我结果:

32f8b5d8c4d1aa1fbb39a0a33338ccb1

【讨论】:

  • 我尝试过使用相同的盐值和相同的密码。它仍然没有给出相同的结果。 20也应该是16吧?我已经尝试将它们设置为常量值。我尝试生成盐并将其复制到 C 代码中,但仍然没有运气。我在这里做错了什么? KDF 哈希算法之间是否存在内部差异?
  • 有人知道吗?我还是有这个问题
  • @NTDLL 请检查我的更新答案并试一试。
猜你喜欢
  • 2023-03-23
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
相关资源
最近更新 更多