Beispiel #1
0
        internal static IntPtr ImportRsaPublicKey(byte[] rawData)
        {
            CryptoProvider provider            = new CryptoProvider();
            IntPtr         nativeStructure     = IntPtr.Zero;
            int            nativeStructureSize = 0;

            try {
                if (!Advapi32.CryptDecodeObjectEx(CertificateEncodingType.X509Asn,
                                                  InteropHelpers.GetWellKnownOIDPointer(InteropHelpers.WellKnownOIDs.X509PublicKeyInfo),
                                                  rawData, rawData.Length, Advapi32.EncodingFlags.AllocateMemory, IntPtr.Zero,
                                                  ref nativeStructure, ref nativeStructureSize))
                {
                    throw new CryptographyException((WinErrors)(uint)Marshal.GetLastWin32Error());
                }
                IntPtr result = IntPtr.Zero;
                if (!Crypt32.CryptImportPublicKeyInfo(provider.Handle, CertificateEncodingType.X509Asn,
                                                      nativeStructure, out result))
                {
                    throw new CryptographyException((WinErrors)(uint)Marshal.GetLastWin32Error());
                }
                return(result);
            }
            finally {
                if (IntPtr.Zero != nativeStructure)
                {
                    if (IntPtr.Zero != Kernel32.LocalFree(nativeStructure))
                    {
                        throw new InteropException(Marshal.GetLastWin32Error());
                    }
                }
                provider.Dispose();
            }
        }