internal static byte[] ExtractSpkiBlob(X509Certificate2 certificate)
        {
            NativeMethods.CERT_PUBLIC_KEY_INFO subjectPublicKeyInfo = ((NativeMethods.CERT_INFO)Marshal.PtrToStructure(((NativeMethods.CERT_CONTEXT)Marshal.PtrToStructure(certificate.Handle, typeof(NativeMethods.CERT_CONTEXT))).pCertInfo, typeof(NativeMethods.CERT_INFO))).SubjectPublicKeyInfo;
            uint   pcbEncoded     = 0;
            IntPtr lpszStructType = new IntPtr(8);

            if (!NativeMethods.CryptEncodeObjectEx(1U, lpszStructType, ref subjectPublicKeyInfo, 0U, (IntPtr)null, (byte[])null, ref pcbEncoded))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            byte[] pbEncoded = new byte[(int)pcbEncoded];
            if (!NativeMethods.CryptEncodeObjectEx(1U, lpszStructType, ref subjectPublicKeyInfo, 0U, (IntPtr)null, pbEncoded, ref pcbEncoded))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(pbEncoded);
        }
 internal static extern bool CryptEncodeObjectEx(uint dwCertEncodingType, IntPtr lpszStructType, ref NativeMethods.CERT_PUBLIC_KEY_INFO pvStructInfo, uint dwFlags, IntPtr pEncodePara, byte[] pbEncoded, ref uint pcbEncoded);