private static Lazy <SafeAlgorithmHandle> Open3DesAlgorithm(string cipherMode, int feedback = 0)
        {
            return(new Lazy <SafeAlgorithmHandle>(() =>
            {
                SafeAlgorithmHandle hAlg = Cng.BCryptOpenAlgorithmProvider(Cng.BCRYPT_3DES_ALGORITHM, null,
                                                                           Cng.OpenAlgorithmProviderFlags.NONE);
                hAlg.SetCipherMode(cipherMode);

                // The default feedback size is 1 (CFB8) on Windows. Do not set the CNG property
                // if we would be setting it to the default. Windows 7 only supports CFB8 and
                // does not permit setting the feedback size, so we don't call the property
                // setter at all in that case.
                if (feedback > 0 && feedback != 1)
                {
                    try
                    {
                        hAlg.SetFeedbackSize(feedback);
                    }
                    catch (CryptographicException ex)
                    {
                        throw new CryptographicException(SR.Cryptography_FeedbackSizeNotSupported, ex);
                    }
                }

                return hAlg;
            }));
        }
示例#2
0
        private static SafeAlgorithmHandle GetCachedAlgorithmHandle(ref SafeAlgorithmHandle?handle, string algId, string?chainingMode = null)
        {
            // Do we already have a handle to this algorithm?
            SafeAlgorithmHandle?existingHandle = Volatile.Read(ref handle);

            if (existingHandle != null)
            {
                return(existingHandle);
            }

            // No cached handle exists; create a new handle. It's ok if multiple threads call
            // this concurrently. Only one handle will "win" and the rest will be destroyed.
            SafeAlgorithmHandle newHandle = Cng.BCryptOpenAlgorithmProvider(algId, null, Cng.OpenAlgorithmProviderFlags.NONE);

            if (chainingMode != null)
            {
                newHandle.SetCipherMode(chainingMode);
            }

            existingHandle = Interlocked.CompareExchange(ref handle, newHandle, null);
            if (existingHandle != null)
            {
                newHandle.Dispose();
                return(existingHandle);
            }
            else
            {
                return(newHandle);
            }
        }
示例#3
0
        private static SafeAlgorithmHandle Open3DesAlgorithm(string cipherMode)
        {
            SafeAlgorithmHandle hAlg = Cng.BCryptOpenAlgorithmProvider(Cng.BCRYPT_3DES_ALGORITHM, null, Cng.OpenAlgorithmProviderFlags.NONE);

            hAlg.SetCipherMode(cipherMode);

            return(hAlg);
        }
示例#4
0
        private static Lazy <SafeAlgorithmHandle> OpenDesAlgorithm(string cipherMode)
        {
            return(new Lazy <SafeAlgorithmHandle>(() =>
            {
                SafeAlgorithmHandle hAlg = Cng.BCryptOpenAlgorithmProvider(
                    Cng.BCRYPT_DES_ALGORITHM,
                    null,
                    Cng.OpenAlgorithmProviderFlags.NONE);
                hAlg.SetCipherMode(cipherMode);

                return hAlg;
            }));
        }
示例#5
0
        private static SafeAlgorithmHandle OpenRC2Algorithm(string cipherMode, int effectiveKeyLength)
        {
            SafeAlgorithmHandle hAlg = Cng.BCryptOpenAlgorithmProvider(Cng.BCRYPT_RC2_ALGORITHM, null, Cng.OpenAlgorithmProviderFlags.NONE);

            hAlg.SetCipherMode(cipherMode);

            if (effectiveKeyLength != 0)
            {
                Cng.SetEffectiveKeyLength(hAlg, effectiveKeyLength);
            }

            return(hAlg);
        }
示例#6
0
        private static Lazy <SafeAlgorithmHandle> Open3DesAlgorithm(string cipherMode, int feedback = 0)
        {
            return(new Lazy <SafeAlgorithmHandle>(() =>
            {
                SafeAlgorithmHandle hAlg = Cng.BCryptOpenAlgorithmProvider(Cng.BCRYPT_3DES_ALGORITHM, null,
                                                                           Cng.OpenAlgorithmProviderFlags.NONE);
                hAlg.SetCipherMode(cipherMode);

                if (feedback > 0)
                {
                    // feedback is in bytes!
                    hAlg.SetFeedbackSize(feedback);
                }

                return hAlg;
            }));
        }