簡體   English   中英

從證書獲取Sha256公鑰

[英]Get Sha256 public key from certificate

當前,正在為客戶端服務器應用程序(Chat)工作,正在為服務器和客戶端實現安全性,很少有客戶端是用Java SMACK庫編寫的,他們使用的是TLS Pining for JAVA,它需要sha2哈希[ https://github.com/ Flowdalic / java-pinning] [1]

服務器是使用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

我已經找到問題的解決方案,請允許我分享。

如果要獲取證書的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("-", ":");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM