2012-09-18 77 views
0

有人能解釋一下下面的代碼..數字簽名與iTextSharp的

什麼會是回報語句來完成。

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

回答

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

的SignHash(字節[],String)方法將計算爲您傳遞基於從證書讀取私鑰的第一個參數的散列值的簽名。在這裏看到:

RSACryptoServiceProvider.SignHash Method

這樣的結果(這是隨後返回)將是一個byte []包含你可以隨着您的數據發送,這樣的簽名可以被別人利用來驗證簽名你的公鑰。