2013-01-16 53 views
3
public byte[] CryptDeriveKey(
    string algname, 
    string alghashname, 
    int keySize, 
    byte[] rgbIV 
) 

有人请赐教我关于algname中有哪些选项?如果我想为AES-128和AES-256指定加密算法,我应该在algname中输入什么?CryptDeriveKey算法名称

+0

您应该使用['Rfc2898DeriveBytes'(http://msdn.microsoft.com/en-us/library/system.security.cryptography.rfc2898derivebytes。 ASPX)。这使用更安全,更新的密钥派生方法:PBKDF2而不是PBKDF1。 PasswordDeriveBytes的实现非常糟糕,特别是对于超过20个字节的输出,在这种情况下,它不仅被破坏,而且也不安全。 –

+0

实现加密非常困难,出于这个原因,我将Google的[Keyczar](http://keyczar.org)框架移植到了[.net](http://jbtule.github.com/keyczar-dotnet/)。使用AES进行加密的原始图不是为正确使用而设计的。 – jbtule

回答

2

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

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

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

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

MSDN检查出更多的细节。

这是​​方法的反编译代码。

[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; 
} 
+0

确定感谢,有没有什么办法可以找到方法NameOrOidtoAlgId - 它需要什么值? – michelle

+0

@michelle更新。 –

+2

好的,谢谢你的帮助。不幸的是,它似乎不支持AES。我不知道使用什么替代方法。 – michelle

2

我没有在这里像Reflector寻找解决方案。如果你有一个去mscorlib.dll和反编译PasswordDerivedBytes.CryptDeriveKey方法。你会在那里找到支持的字符串。

顺便提一下,AES不支持:here

+0

然后我可以使用什么来代替cryptderivekey?谢谢! – michelle

+0

不知道,对不起:(我不是密码学专家... –