2016-12-27 394 views
1

目前,我正在爲客戶端服務器應用程序(聊天)實現服務器和客戶端的安全性,很少的客戶端是用java SMACK庫編寫的,他們使用TLS PING for JAVA它需要sha2 hash [https://github.com/Flowdalic/java-pinning][1]從證書獲取Sha256公鑰

服務器使用C#實現,我在服務器端有證書如何從證書獲取具有以下格式的sha2公鑰,下面是我的代碼。

cer =new X509Certificate2(ConfigurationManager.AppSettings["CertificateName"],"123456"); 

string hellow= cer.GetCertHashString(); //it will return sha1 hash 

我需要的是下面的格式和sha2-256鍵從證書 SHA2-256關鍵

83:F9:17:1E:06:A3:13:11:88 :89:F7:D7:93:02:BD:1B:7A:20:42 EE:0C:FD:02:9A:BF:8D:D0:6F:FA:6C:D9:D3

回答

2

我發現了我的問題的解決方案,讓我分享。

如果你想獲得證書的SHA256指紋,你必須做一些手動工作。內置的Thumbprint屬性僅限SHA1。

呦必須使用SHA256類和計算散列以上證書的內容:

using System; 
using System.Linq; 
using System.Security.Cryptography; 
using System.Security.Cryptography.X509Certificates; 

namespace MyNamespace { 
    class MyClass { 
     public static String GetSha2Thumbprint(X509Certificate2 cert) { 
      Byte[] hashBytes; 
      using (var hasher = new SHA256Managed()) { 
       hashBytes = hasher.ComputeHash(cert.RawData); 
      } 
      return BitConverter.ToString(hashBytes).Replace("-", ":"); 
     } 
    } 
} 
相關問題