Esempio n. 1
0
        private static SafeCapiKeyHandle SetupKey(SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, int feedbackSize)
        {
            Contract.Requires(key != null);
            Contract.Requires(cipherMode == CipherMode.ECB || iv != null);
            Contract.Requires(0 <= feedbackSize);
            Contract.Ensures(Contract.Result <SafeCapiKeyHandle>() != null &&
                             !Contract.Result <SafeCapiKeyHandle>().IsInvalid&&
                             !Contract.Result <SafeCapiKeyHandle>().IsClosed);

            // Make a copy of the key so that we don't modify the properties of the caller's copy
            SafeCapiKeyHandle encryptionKey = key.Duplicate();

            // Setup the cipher mode first
            CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.Mode, (int)cipherMode);

            // If we're not in ECB mode then setup the IV
            if (cipherMode != CipherMode.ECB)
            {
                CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.IV, iv);
            }

            // OFB and CFB require a feedback loop size
            if (cipherMode == CipherMode.CFB || cipherMode == CipherMode.OFB)
            {
                CapiNative.SetKeyParameter(encryptionKey, CapiNative.KeyParameter.ModeBits, feedbackSize);
            }

            return(encryptionKey);
        }
        private static SafeCapiKeyHandle SetupKey(SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, int feedbackSize)
        {
            SafeCapiKeyHandle handle = key.Duplicate();

            CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.Mode, (int)cipherMode);
            if (cipherMode != CipherMode.ECB)
            {
                CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.IV, iv);
            }
            if ((cipherMode == CipherMode.CFB) || (cipherMode == CipherMode.OFB))
            {
                CapiNative.SetKeyParameter(handle, CapiNative.KeyParameter.ModeBits, feedbackSize);
            }
            return(handle);
        }