简体   繁体   中英

Digital signature with itextsharp

Can someone explain the following code..

What will that return statement do.

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

The SignHash(byte[], string) method will compute the signature for the hash value you pass as the first argument based on the private key read from your certificate. See here:

RSACryptoServiceProvider.SignHash Method

The result of this (which is subsequently returned) will be a byte[] containing the signature which you can send along with your data so that the signature can be verified by someone else using your public key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM