c++ - 不使用 CLR 的 C++ 中的 Rfc2898DeriveBytes 等价物
问题描述
在不使用 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());
}
}
解决方案
您可以PKCS5_PBKDF2_HMAC
在 OpenSSL 中使用。
这两个函数都是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 次。
推荐阅读
- reactjs - Flow(cannot-resolve-module)错误将webp文件导入反应
- django - Django 开发服务器非常慢
- sql-server - SQL Server Count with case when
- r - 如何在R中重塑表格?
- c# - 如何在 Caliburn.Micro ViewModel 中处理 WPF MenuItem 命令?
- c++ - 将指针转换为范围
- c# - 在 .NET Core Oracle.EntityframeworkCore 上运行“更新数据库”命令时出错
- c# - 自定义验证属性未在模型上验证 - WebAPI C# 和 JSON
- python - 在 Tensorflow 2.0 中加载模型时无法在类中保存方法
- python - 通过在python中删除换行符并删除第一个和最后一个字符,将列表中的字符串转换为列表