c# - C# 通用应用平台中证书的公钥

标签 c# certificate win-universal-app ssl-certificate

在本文中https://blogs.windows.com/buildingapps/2015/10/13/create-more-secure-apps-with-less-effort-10-by-10/他们向您解释如何安全地连接到服务器。他们检查指纹以查看证书是否合法。但证书会随着时间的推移而发生变化,我检查的硬编码字符串将不再有效。

这就是我想提取公钥的原因。因为我确信它不会从一个证书更改为另一个证书。

在此代码中:

        private async Task DemoSSLRoot()
    {
        // Send a get request to Bing
        HttpClient client = new HttpClient();
        Uri bingUri = new Uri("https://www.bing.com");
        HttpResponseMessage response = await client.GetAsync(bingUri);

        // Get the list of certificates that were used to validate the server's identity
        IReadOnlyList<Certificate> serverCertificates = response.RequestMessage.TransportInformation.ServerIntermediateCertificates;

        // Perform validation
        if (!ValidCertificates(serverCertificates))
        {
            // Close connection as chain is not valid
            return;
        }

        PrintResults("Validation passed\n");
        // Validation passed, continue with connection to service
    }

    private bool ValidCertificates(IReadOnlyList<Certificate> certs)
    {
        // In this example, we iterate through the certificates and check that the chain contains
        // one specific certificate we are expecting
        for (int i = 0; i < certs.Count; i++)
        {
            PrintResults("Cert# " + i + ": " + certs[i].Subject + "\n");
            byte[] thumbprint = certs[i].GetHashValue();

            // Check if the thumbprint matches whatever you are expecting
            // ‎d4 de 20 d0 5e 66 fc 53 fe 1a 50 88 2c 78 db 28 52 ca e4 74
            byte[] expected = new byte[] { 212, 222, 32, 208, 94, 102, 252, 83, 254, 26, 80, 136, 44, 120, 219, 40, 82, 202, 228, 116 };

            if (ThumbprintMatches(thumbprint, expected))
            {
                return true;
            }
        }

        return false;
    }

了解更多信息 https://blogs.windows.com/buildingapps/2015/10/13/create-more-secure-apps-with-less-effort-10-by-10/#1tFDZeMtskOkOrvd.99

访问指纹非常容易。但我需要公钥。 我在互联网上搜索,发现了非常疯狂的代码来检查我是否无法使其工作。

有人可以告诉我是否有一种简单的方法可以从 Windows 10 中的证书中提取公钥?

问候。

最佳答案

X509Certificate.GetPublicKey 方法可用于通用 Windows 平台。

您可以使用例如:

var publicKey = certs[i].GetPublicKey();

byte[] publicKey = certs[i].GetPublicKey.EncodedKeyValue.RawData;

关于c# - C# 通用应用平台中证书的公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38124530/

相关文章:

c# - 在 UWP 中使用 PCL resx 字符串

ssl - 从普遍信任的证书创建我自己的中间证书颁发机构

service - Windows 10 - 后台代理/服务?

javascript - 如何从 HTML 表单向 Controller 提供数据

C#:半抽象自动属性?

visual-studio-2008 - 我们是否需要安装中间代码签名证书以获得完全信任的 XBAP/ClickOnce 应用程序?

ssl - 如何修复 Stunnel 服务器上的 "Service [XXX]: SSL server needs a certificate"?

c# - 如何访问/编辑捕获图像中的像素 C#

c# - 实例化参数化类型列表,更好地利用泛型和 Linq

c# - 如何在没有 ViewBox 参数的情况下编写可缩放的 SVG?