KeyDerivationParameters.BuildForPbkdf2(IBuffer, UInt32) 方法

定义

创建 KeyDerivationParameters 对象,用于基于密码的密钥派生函数 2 (PBKDF2) 。

public:
 static KeyDerivationParameters ^ BuildForPbkdf2(IBuffer ^ pbkdf2Salt, unsigned int iterationCount);
 static KeyDerivationParameters BuildForPbkdf2(IBuffer const& pbkdf2Salt, uint32_t const& iterationCount);
public static KeyDerivationParameters BuildForPbkdf2(IBuffer pbkdf2Salt, uint iterationCount);
function buildForPbkdf2(pbkdf2Salt, iterationCount)
Public Shared Function BuildForPbkdf2 (pbkdf2Salt As IBuffer, iterationCount As UInteger) As KeyDerivationParameters

参数

pbkdf2Salt
IBuffer

盐,一个随机或伪随机值,要在多次迭代中与密码组合。 盐用于增加超过单独使用密码可以获得的熵。

iterationCount
UInt32

unsigned int

uint32_t

用于派生密钥的迭代次数。

返回

引用在密钥派生期间使用的参数。

示例

public String SampleDeriveFromPbkdf(
    String strAlgName,
    UInt32 targetSize)
{
    // Open the specified algorithm.
    KeyDerivationAlgorithmProvider objKdfProv = KeyDerivationAlgorithmProvider.OpenAlgorithm(strAlgName);

    // Create a buffer that contains the secret used during derivation.
    String strSecret = "MyPassword";
    IBuffer buffSecret = CryptographicBuffer.ConvertStringToBinary(strSecret, BinaryStringEncoding.Utf8);

    // Create a random salt value.
    IBuffer buffSalt = CryptographicBuffer.GenerateRandom(32);

    // Specify the number of iterations to be used during derivation.
    UInt32 iterationCount = 10000;

    // Create the derivation parameters.
    KeyDerivationParameters pbkdf2Params = KeyDerivationParameters.BuildForPbkdf2(buffSalt, iterationCount);

    // Create a key from the secret value.
    CryptographicKey keyOriginal = objKdfProv.CreateKey(buffSecret);

    // Derive a key based on the original key and the derivation parameters.
    IBuffer keyDerived = CryptographicEngine.DeriveKeyMaterial(
        keyOriginal,
        pbkdf2Params,
        targetSize);

    // Encode the key to a hexadecimal value (for display)
    String strKeyHex = CryptographicBuffer.EncodeToHexString(keyDerived);

    // Return the encoded string
    return strKeyHex;
}

注解

可以将以下算法名称与 OpenAlgorithm 函数结合使用,以打开 PBKDF2 算法提供程序:

适用于