c++ - 不使用 CLR 的 C++ 中 Rfc2898DeriveBytes 的等价物

标签 c++ cryptography pbkdf2

在不使用 CLR 的情况下,C++ 中的 Rfc2898DeriveBytes 是什么替代品。下面分享了 C# 示例。

string clearText="text to sign";
string EncryptionKey = "secret";
byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x48, 0x71, 0x21, 0x6d, 0x21, 0x4c, 0x61, 0x62, 0x72, 0x62, 0x61, 0x62, 0x72 });
    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);
    using (MemoryStream ms = new MemoryStream())
    {
        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(clearBytes, 0, clearBytes.Length);
            cs.Close();
        }
        clearText = Convert.ToBase64String(ms.ToArray());
    }
}

最佳答案

您可以在 OpenSSL 中使用 PKCS5_PBKDF2_HMAC

这两个函数都是PBKDF2功能和可以互换使用。

更新:

这是一个示例代码,用于在 C#OpenSSL 中生成类似的 key 。

C# 端:

public static void Main()
{
    string EncryptionKey = "secret";
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x48, 0x71, 0x21, 0x6d, 0x21, 0x4c, 0x61, 0x62, 0x72, 0x62, 0x61, 0x62, 0x72 }, 1000);
    Console.WriteLine("[{0}]", string.Join(", ", pdb.GetBytes(32)));
    Console.WriteLine("[{0}]", string.Join(", ", pdb.GetBytes(16)));
}

OpenSSL 端:

#include <openssl/evp.h>
#include <string.h>
#include <stdlib.h>

int main(){
        char secret[] = "secret";
        unsigned char buf[48] = {0,};
        int size = 48;
        unsigned char salt[] = { 0x48, 0x71, 0x21, 0x6d, 0x21, 0x4c, 0x61, 0x62, 0x72, 0x62, 0x61, 0x62, 0x72 };
        PKCS5_PBKDF2_HMAC(secret, strlen(secret), salt, sizeof(salt), 1000, EVP_sha1(), size, buf);
        for (int i = 0; i < size; i++)
                printf("%d ", buf[i]);
        return 0;
}

请记住,在这些代码中,迭代次数仅为 1,000,请至少使用 100,000 甚至 1,000,000。

关于c++ - 不使用 CLR 的 C++ 中 Rfc2898DeriveBytes 的等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015935/

相关文章:

c++ - 如何构建一个元组 vector 并像对一样对它们进行排序?

C# 接口(interface) C++ DLL?

c++ - 将类映射到 mySql 表,而不在 cpp 中使用 ODB 或其他框架

java - 在 Java 中使用对称加密保护磁盘上的私钥

java - 如何在Java中分别打印证书的公钥长度和模数

java - 如何生成要作为参数发送给 KeyStore.PrivateKeyEntry 的证书链?

couchdb - Ironclad 和 couch pbkdf2 哈希之间不匹配

c++ - 在 vector 的 vector 上使用算法

javascript - javascript 和 iOS 中的 PBKDF2 哈希生成不同的 key

security - 使用 PKBDF2、AES、IV 和 salt 进行更好的实践