【问题标题】:Equivalent of Rfc2898DeriveBytes in C++ without using CLR不使用 CLR 的 C++ 中的 Rfc2898DeriveBytes 等价物
【发布时间】:2019-07-27 16:41:16
【问题描述】:

在不使用 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());
    }
}

【问题讨论】:

  • RFC2898 "C++" site:github.com。另请参阅pbkdf2 标签。
  • 感谢分享链接...大多数可用的代码实际上并不是 C++ 中的。
  • 那么您是说没有足够的替代 C++ 实现可供选择吗?也许社区应该为您找到更多?

标签: c++ cryptography pbkdf2


【解决方案1】:

您可以在 OpenSSL 中使用PKCS5_PBKDF2_HMAC

这两个函数都是PBKDF2函数,可以互换使用。

更新:

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

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++ 的加密值(密码文本)未在 C# 代码中解码。我们使用 C++ 代码加密文本,使用 C# 解密文本。
  • @raj 最好自己尝试解决问题。写这篇文章只花了几分钟。但我会为你更新。
猜你喜欢
  • 2014-01-08
  • 2011-04-02
  • 2021-10-15
  • 2013-10-20
  • 2011-04-10
  • 2021-12-02
  • 2010-09-14
  • 2016-12-24
相关资源
最近更新 更多