/// <include file='doc\PasswordDeriveBytes.uex' path='docs/doc[@for="PasswordDeriveBytes.CryptDeriveKey"]/*' /> public byte[] CryptDeriveKey(String algname, String alghashname, int keySize, byte[] rgbIV) { int hr; int algid; int algidhash; int dwFlags = 0; if (keySize < 0) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } if (_hCSP == IntPtr.Zero) { hr = _AcquireCSP(_cspParams, ref _hCSP); if (_hCSP == IntPtr.Zero) { throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_CouldNotAcquire")); } } // Convert password to UTF8 string byte[] rgbPwd = (new UTF8Encoding(false)).GetBytes(_strPassword); // Form the correct dwFlags dwFlags |= (keySize << 16); algidhash = CryptoConfig.MapNameToCalg(alghashname); if (algidhash == 0) { throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidAlgorithm")); } algid = CryptoConfig.MapNameToCalg(algname); if (algid == 0) { throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidAlgorithm")); } // Validate the rgbIV array SymmetricAlgorithm symAlg = (SymmetricAlgorithm)CryptoConfig.CreateFromName(algname); if (symAlg == null) { throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidAlgorithm")); } if (rgbIV == null || rgbIV.Length != symAlg.BlockSize / 8) { throw new CryptographicException(Environment.GetResourceString("Cryptography_PasswordDerivedBytes_InvalidIV")); } byte[] rgbKey = _CryptDeriveKey(_hCSP, algid, algidhash, rgbPwd, dwFlags, rgbIV); GC.KeepAlive(this); return(rgbKey); }