我的PDF文件存在数字签名问题,过程如下: -我在服务器上有一个PDF文件,然后找到签名并将其转换为SHA1的字节格式[]。 -客户端将从USB令牌中检索签名,以分配给从服务器传输的字节[]。 -然后我返回了服务器以分配签名。
但是当我按以下方式编写代码时,程序仍然可以正常运行而没有错误,但是USB签名内容未签名到pdf文件中。请帮助我修复它。
private void Form1_Load(object sender, EventArgs e)
{
string UnsingnedPdf = @"E:\PrintContractAdviceNote.pdf";
string TempPdf = @"E:\PrintContractAdviceNote_TMP.pdf";
string singnedPdf = @"E:\PrintContractAdviceNote_Sign.pdf";
PDFSigning cl = new PDFSigning();
cl.GetBytesToSign(UnsingnedPdf, TempPdf, singnedPdf, "CHUKY1", 3, "BIG BEAR");
}
public static X509Certificate2 selectCert()
{
X509Certificate2 certificate = null;
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
if (store.Certificates.Count == 1)
{
//Return the certificate present.
certificate = store.Certificates[0];
}
else if (store.Certificates.Count > 0)
{
// Request the user to select a certificate
try
{
var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates,
"Digital Certificates", "Select a certificate from the following list:",
X509SelectionFlag.SingleSelection);
// Check if one has been returned
if (certificates.Count == 1)
{
certificate = certificates[0];
var newCert = new X509Certificate2(certificate.RawData, "mypassword");
}
else
{
throw new ArgumentException("Please select a certificate to publish PnL to Flash");
}
}
catch (Exception ex)
{
}
}
else
{
throw new ArgumentException("There is no certificate available to publish PnL to flash, please contact support.");
}
}
finally
{
store.Close();
}
return certificate;
}
public class IpInfo
{
//country
public string Country { get; set; }
}
public byte[] GetBytesToSign(string unsignedPdf, string tempPdf, string signedPdf, string signatureFieldName, Int16 iType, string SignName)
{
if (File.Exists(tempPdf))
File.Delete(tempPdf);
if (File.Exists(signedPdf))
File.Delete(signedPdf);
byte[] array = null;
bool Islock = true;
FileStream os = File.OpenWrite(tempPdf);
PdfReader reader = new PdfReader(unsignedPdf);
DateTime dt = DateTime.Now;
using (PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0'))
{
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Contact = "www.marico.com";
appearance.SignDate = dt;
//BaseFont bf = BaseFont.CreateFont(HttpContent.Current.Server.MapPath("~/Resources/Font/arial.ttf"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//Font font = new Font(bf, 8);
//appearance.Layer2Font = font;
appearance.CertificationLevel = PdfSignatureAppearance.CERTIFIED_FORM_FILLING_AND_ANNOTATIONS;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
appearance.Acro6Layers = true;
appearance.Layer2Text = "Signed by:" + Environment.NewLine + " " + SignName + Environment.NewLine + "Date :" + Environment.NewLine + " " + dt.ToShortDateString() + " " + dt.ToShortTimeString();
Image img;
if (iType == 3)
{
img = Image.GetInstance(@"E:\Tài liệu\Marico\HINH ANH\talent.png");
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(15, 50, 235, 95), reader.NumberOfPages, signatureFieldName);
}
else
{
img = Image.GetInstance(@"E:\Tài liệu\Marico\HINH ANH\talent.png");
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(350, 50, 570, 95), reader.NumberOfPages, signatureFieldName);
}
appearance.SignatureGraphic = img;
IExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
MakeSignature.SignExternalContainer(appearance, external, 8192);
array = SHA1.Create().ComputeHash(appearance.GetRangeStream());
stamper.Dispose();
stamper.Close();
}
if (iType != 3)
{
while (Islock)
{
reader.Dispose();
reader.Close();
os.Dispose();
os.Close();
Islock = IsFileLocked(unsignedPdf);
}
}
else
{
reader.Dispose();
reader.Close();
os.Dispose();
os.Close();
}
X509Certificate2 cert = selectCert();
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(cert.PrivateKey);
formatter.SetHashAlgorithm("SHA1");
byte[] signature = formatter.CreateSignature(array);
EmbedSignature(tempPdf, signedPdf, signatureFieldName, signature);
return (array);
}
public static bool IsFileLocked(string pathfile)
{
try
{
using (FileStream fs = new FileStream(pathfile, FileMode.Open))
{
fs.Dispose();
fs.Close();
}
return false;
}
catch (IOException ex)
{
return true;
}
}
private static void SetSigPosition(PdfSignatureAppearance sigAppearance, int oldSigCount, string signatureFieldName, int numpage = 1)
{
float llx = (200 + 20) * (oldSigCount % 5) + 20,
lly = (25 + 20) * (oldSigCount / 5) + 5,
urx = llx + 200,
ury = lly + 45;
}
public void EmbedSignature(string tempPdf, string signedPdf, string signatureFieldName, byte[] signedBytes)
{
IExternalSignatureContainer external = new MyExternalSignatureContainer(signedBytes);
using (FileStream os = File.OpenWrite(signedPdf))
{
using (PdfReader reader = new PdfReader(tempPdf))
{
MakeSignature.SignDeferred(reader, signatureFieldName, os, external);
reader.Dispose();
reader.Close();
os.Dispose();
os.Close();
}
}
}
private class MyExternalSignatureContainer : IExternalSignatureContainer
{
private readonly byte[] signedBytes;
public MyExternalSignatureContainer(byte[] signedBytes)
{
this.signedBytes = signedBytes;
}
public byte[] Sign(Stream data)
{
return signedBytes;
}
public void ModifySigningDictionary(PdfDictionary signDic)
{
}
}