コード例 #1
0
        static CspCNGCollection m_enumcngprovs()
        {
            UInt32           pImplCount = 0;
            IntPtr           ppImplList = IntPtr.Zero;
            IntPtr           phProvider = IntPtr.Zero;
            StringBuilder    SB         = new StringBuilder();
            Hashtable        interfaces = get_interfaces();
            CspCNGCollection csps       = new CspCNGCollection();

            UInt32 retn = nCrypt.NCryptEnumStorageProviders(ref pImplCount, ref ppImplList, 0);

            if (retn != 0)
            {
                throw new Win32Exception(unchecked ((Int32)retn));
            }
            IntPtr pvInput = ppImplList;

            for (Int32 index = 0; index < pImplCount; index++)
            {
                nCrypt2.NCryptProviderName Name =
                    (nCrypt2.NCryptProviderName)Marshal.PtrToStructure(ppImplList, typeof(nCrypt2.NCryptProviderName));
                ppImplList = (IntPtr)((UInt64)ppImplList + (UInt32)Marshal.SizeOf(typeof(nCrypt2.NCryptProviderName)));
                SB.Append(Name.pszName + ",");
            }
            String[] names = SB.ToString().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            nCrypt.NCryptFreeBuffer(pvInput);
            foreach (String pszProviderName in names)
            {
                ALG_ID_CNGCollection algs = new ALG_ID_CNGCollection();
                retn = nCrypt.NCryptOpenStorageProvider(ref phProvider, pszProviderName, 0);
                if (retn == 0)
                {
                    Int32  pdwAlgCount = 0;
                    IntPtr ppAlgList   = IntPtr.Zero;
                    retn = nCrypt.NCryptEnumAlgorithms(phProvider, 0, ref pdwAlgCount, ref ppAlgList, 0);
                    if (retn != 0)
                    {
                        throw new Win32Exception(unchecked ((Int32)retn));
                    }
                    pvInput = ppAlgList;
                    for (Int32 index = 0; index < pdwAlgCount; index++)
                    {
                        nCrypt2.NCryptAlgorithmName AlgId =
                            (nCrypt2.NCryptAlgorithmName)Marshal.PtrToStructure(ppAlgList, typeof(nCrypt2.NCryptAlgorithmName));
                        algs.Add(get_cngalgparams(AlgId, interfaces));
                        ppAlgList = (IntPtr)((UInt64)ppAlgList + (UInt32)Marshal.SizeOf(typeof(nCrypt2.NCryptAlgorithmName)));
                    }
                    nCrypt.NCryptFreeBuffer(pvInput);
                }
                csps.Add(new CspCNG(pszProviderName, String.Empty, algs));
            }
            return(csps);
        }
コード例 #2
0
ファイル: Csp.cs プロジェクト: nhtha/pkix.net
        static ALG_ID_CNG get_cngalgparams(nCrypt2.NCryptAlgorithmName algId, IDictionary interfaces)
        {
            Int32[]  options      = { 4, 8, 16 };
            Int32[]  validoptions = new Int32[0];
            String[] operations   = new String[0];
            Int32    index        = 0;

            foreach (Int32 dwAlgOperations in options.Where(dwAlgOperations => (algId.dwAlgOperations & dwAlgOperations) != 0))
            {
                Array.Resize(ref validoptions, index + 1);
                validoptions.SetValue(dwAlgOperations, index);
                index++;
            }
            index = 0;
            foreach (Int32 opt in validoptions)
            {
                switch (opt)
                {
                case 4:
                    Array.Resize(ref operations, index + 1);
                    operations.SetValue("Asymmetric encryption", index);
                    break;

                case 8:
                    Array.Resize(ref operations, index + 1);
                    operations.SetValue("Secret agreement", index);
                    break;

                case 16:
                    Array.Resize(ref operations, index + 1);
                    operations.SetValue("Signature", index);
                    break;
                }
            }
            return(new ALG_ID_CNG(algId.pszName, (String)interfaces[algId.dwClass], operations));
        }