簡體   English   中英

使用itextsharp進行數字簽名

[英]Digital signature with itextsharp

有人可以解釋以下代碼。

return語句將做什么。

 public byte[] sign(string text)
 {
    string password = "1234";
    X509Certificate2 cert = new X509Certificate2("c:\\certificate.pfx", password);
    RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
    SHA1Managed sha1 = new SHA1Managed();
    UnicodeEncoding encoding = new UnicodeEncoding();
    byte[] data = encoding.GetBytes(text);
    byte[] hash = sha1.ComputeHash(data);
    return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
}
    public byte[] sign(string text)
         {
//Password for the PFX certificate
            string password = "1234";
//Importing the PFX certificate that contains the private key which will be used for creating the digital signature
            X509Certificate2 cert = new X509Certificate2("c:\\certificate.pfx", password);
//declaring RSA cryptographic service provider
            RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)cert.PrivateKey;
//cryptographic hash of type SHA1
            SHA1Managed sha1 = new SHA1Managed();
//encoding the data to be signed
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] data = encoding.GetBytes(text);
//generate Hash
            byte[] hash = sha1.ComputeHash(data);
//sign Hash
            return crypt.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
        }

SignHash(byte [],string)方法將基於從證書中讀取的私鑰計算您作為第一個參數傳遞的哈希值的簽名。 看這里:

RSACryptoServiceProvider.SignHash方法

其結果(隨后返回)將是一個byte [],其中包含可以隨數據一起發送的簽名,以便其他人可以使用您的公共密鑰來驗證簽名。

暫無
暫無

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

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