簡體   English   中英

從X509證書獲取簽名值

[英]Get the Signature Value from X509 Certificate

我想制作一個小程序,將其作為輸入(1)X509證書(2)作為對該證書進行簽名的相應CA。 它應通過驗證簽名來驗證此證書是否完整。 為此,我相信首先我需要提取兩件事:(1)簽名值(2)其余證書字段。 以下代碼可以很好地獲取公鑰, 出於我的目的, 我需要簽名值

URL httpslink = new URL("https://mail.yahoo.com");
HttpsURLConnection con = (HttpsURLConnection) httpslink.openConnection();
con.connect();
Certificate ct[] = con.getServerCertificates();

X509Certificate c = ((X509Certificate) ct[0]);
System.out.println(c.getPublicKey().toString());

我嘗試了多種方法來獲取簽名值,但失敗了。 你們能給我至少一個成功的機會嗎? 謝謝

正如注釋已表明的那樣,使用getSignature方法可以獲取簽名。 但是,它是一個byte[] 因此,您不應期望其toString值有任何可用的東西。

不過,關於您的原始目標:

通過驗證簽名來驗證此證書是否完整。

您無需手動完成所有這些工作。 相反,您應該使用Certificate方法getPublicKeyverify

boolean check (Certificate testCert, Certificate caCert)
{
    try
    {
        testCert.verify(caCert.getPublicKey());
        return true;
    }
    catch (GeneralSecurityException e)
    {
        return false;
    }
]

根據所使用的算法,您可能需要使用其他驗證重載來提供顯式提供程序。

對於那些有疑問的人,相應的Certificate方法評論:

/**
 * Verifies that this certificate was signed using the
 * private key that corresponds to the specified public key.
 *
 * @param key the PublicKey used to carry out the verification.
 *
 * ...
 */
public abstract void verify(PublicKey key)

/**
 * Gets the public key from this certificate.
 *
 * @return the public key.
 */
public abstract PublicKey getPublicKey()

暫無
暫無

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

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