【问题标题】:Digital signature with itextsharp使用 itextsharp 进行数字签名
【发布时间】:2012-09-18 10:09:51
【问题描述】:

谁能解释一下下面的代码..

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"));
}

【问题讨论】:

    标签: c# asp.net digital-signature pfx


    【解决方案1】:
        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"));
            }
    

    【讨论】:

      【解决方案2】:

      SignHash(byte[], string) 方法将根据从您的证书中读取的私钥计算您作为第一个参数传递的哈希值的签名。见这里:

      RSACryptoServiceProvider.SignHash Method

      此结果(随后返回)将是一个包含签名的字节[],您可以将其与您的数据一起发送,以便其他人可以使用您的公钥验证该签名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-18
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        相关资源
        最近更新 更多