2016-06-30 22 views
1

この記事では、https://blogs.windows.com/buildingapps/2015/10/13/create-more-secure-apps-with-less-effort-10-by-10/で、サーバーに安全に接続する方法について説明します。彼らは証明書が合法であることを確認するために拇印をチェックします。しかし、証明書は時間とともに変化し、チェックしたハードコーディングされた文字列はもはや有効ではなくなります。C#ユニバーサルアプリケーションプラットフォームの証明書の公開鍵

だから私は公開鍵を抽出したいのです。なぜなら、ある証明書から別の証明書に変更するつもりはないからです。このコードで

 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の証明書から公開鍵を簡単に抽出する方法があると教えてもらえますか?

よろしくお願いいたします。

答えて

1

トーマスはGetPublicKeyと呼ばれる方法があった言ったように。 APIには含まれていません。ちょうどこのメソッドが利用可能だった "System.Security.Cryptography.X509Certificates"と呼ばれるナゲットパッケージがあったことに気づいた。

ありがとうございます!

1

X509Certificate.GetPublicKeyメソッドは、ユニバーサルWindowsプラットフォームで使用できます。

次の例のように使用することができます

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

または

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

GetPublicKeyはWindowsユニバーサルアプリケーションでは使用できませんが、わからない名前空間に存在しますか? – toroveneno

+1

[MSDNの例](https://msdn.microsoft.com/cs-cz/library/system.security.cryptography.x509certificates.x509certificate.getpublickey(v = vs.110).aspx) –

関連する問題