CryptDeriveKey算法名称

时间:2013-01-16 15:03:26

标签: c# encryption cryptography aes password-encryption

public byte[] CryptDeriveKey(
    string algname,
    string alghashname,
    int keySize,
    byte[] rgbIV
)

有人可以告诉我,在algname中有哪些选项?如果我想为AES-128和AES-256指定加密算法,我应该在algname中添加什么?

3 个答案:

答案 0 :(得分:2)

我不是100%确定,但algname是您的算法名称。 keySize是密钥的大小。

您应该像这样使用AES-128AES-256;

CryptDeriveKey("AES", "SHA1", 128, aes.IV)

CryptDeriveKey("AES", "SHA1", 256, aes.IV)

查看 MSDN 的更多详情。

以下是PasswordDeriveBytes.CryptDeriveKey方法的反编译代码。

[SecuritySafeCritical]
public byte[] CryptDeriveKey(string algname, string alghashname, int keySize, byte[] rgbIV)
{
    if (keySize < 0)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
    }
    int algidHash = X509Utils.NameOrOidToAlgId(alghashname, OidGroup.HashAlgorithm);
    if (algidHash == 0)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidAlgorithm"));
    }
    int algid = X509Utils.NameOrOidToAlgId(algname, OidGroup.AllGroups);
    if (algid == 0)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidAlgorithm"));
    }
    if (rgbIV == null)
    {
        throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidIV"));
    }
    byte[] o = null;
    DeriveKey(this.ProvHandle, algid, algidHash, this._password, this._password.Length, keySize << 0x10, rgbIV, rgbIV.Length, JitHelpers.GetObjectHandleOnStack<byte[]>(ref o));
    return o;
}

以下是NameOrOidToAlgId方法的反编译代码。

internal static int NameOrOidToAlgId(string oid, OidGroup oidGroup)
{
    if (oid == null)
    {
        return 0x8004;
    }
    string str = CryptoConfig.MapNameToOID(oid, oidGroup);
    if (str == null)
    {
        str = oid;
    }
    int algIdFromOid = GetAlgIdFromOid(str, oidGroup);
    switch (algIdFromOid)
    {
        case 0:
        case -1:
            throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidOID"));
    }
    return algIdFromOid;
}

答案 1 :(得分:2)

我这里没有Reflector这样的解析器来找到解决方案。如果您有一个转到mscorlib.dll并反编译PasswordDerivedBytes.CryptDeriveKey方法。你会在那里找到支持的字符串。

顺便说一句,不支持AES:here

答案 2 :(得分:0)

一个人可以将TripleDES用作algname参数。

例如:

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes("password", 24, 1000);
byte[] key = pdb.CryptDeriveKey("TripleDES", "SHA256", 192, tdes.IV);

点击此处查看文档Pessimistic Concurrency (Locking)