c# - 在 Metro (WinRT) 应用程序中使用 PBKDF2 加密

标签 c# c++ encryption windows-runtime pbkdf2

我需要在 C# 和 C++ Metro (WinRT) 应用程序中使用 PBKDF2 加密从加盐密码派生 key 。我应该使用什么在 Metro 上使用 PBKDF2(如 OpenSSL 的 PKCS5_PBKDF2_HMAC_SHA1 调用)派生 key ?是否有基于 WinRT 构建的 OpenSSL 版本? (我读过它只在桌面平台的 Windows 上构建。)或者我应该使用其他一些解决方案吗?

顺便说一句,我可以从 C# 或 C++ 调用该函数,所以两者都可以。任何建议将不胜感激!

编辑: 我刚找到一个名为“Rfc2898DeriveBytes”的 .NET 函数 -- 详细信息 here .如果我没看错的话,它会做与 OpenSSL 的 PKCS5_PBKDF2_HMAC_SHA1 调用相同的事情——对吗?

编辑#2: 不幸的是,我似乎无法在我的 Windows 8.1 Metro 应用程序中使用 Rfc2898DeriveBytes,因为尽管 Microsoft documentation for Rfc2898DeriveBytes表示,在构建 Windows 8.1 应用程序时,该 API 方法不存在于“Windows.Security.Cryptography”命名空间中。还有什么我可以使用的吗?

最佳答案

经过大量挖掘,我终于找到了 this link .这是我最终在 Metro 应用程序中执行的操作:

private static bool GetPBKDFDerivedKey(string password, 
    byte[] salt,                    // length = 32 bytes (256 bits)
    out byte[] encryptionKeyOut)    // length = 32 bytes (256 bits)
{            
    IBuffer saltBuffer = CryptographicBuffer.CreateFromByteArray(salt);
    KeyDerivationParameters kdfParameters = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 10000);  // 10000 iterations

    // Get a KDF provider for PBKDF2 and hash the source password to a Cryptographic Key using the SHA256 algorithm.
    // The generated key for the SHA256 algorithm is 256 bits (32 bytes) in length.
    KeyDerivationAlgorithmProvider kdf = KeyDerivationAlgorithmProvider.OpenAlgorithm(KeyDerivationAlgorithmNames.Pbkdf2Sha256);
    IBuffer passwordBuffer = CryptographicBuffer.ConvertStringToBinary(password, BinaryStringEncoding.Utf8);
    CryptographicKey passwordSourceKey = kdf.CreateKey(passwordBuffer);

    // Generate key material from the source password, salt, and iteration count
    const int keySize = 256 / 8;  // 256 bits = 32 bytes  
    IBuffer key = CryptographicEngine.DeriveKeyMaterial(passwordSourceKey, kdfParameters, keySize);

    // send the generated key back to the caller
    CryptographicBuffer.CopyToByteArray(key, out encryptionKeyOut);

    return true;  // success
}

关于c# - 在 Metro (WinRT) 应用程序中使用 PBKDF2 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23023058/

相关文章:

c++ - 为什么 GDB 没有在断点处停止?

sql-server - 如何加密数据库的所有现有存储过程

c# - 获取独占进程句柄

c# - 在 ListView 的下拉列表中获取 selectedIndexChanged 上的 ListView 项的值

c# - 如何通过 C# 为 sproc 传递参数

c++ - OpenCV像素化算法

c# - 使用 DapperExtensions 比较两个字段的谓词

c++ - 某些阵列大小的性能下降

简单加密代码的 C 段错误

c++ - 如何在 C++ 中安全地存储 AES 加密 key ?