Beispiel #1
0
        static void TestSignAndVerifyPDF()
        {
            bool TestSign   = true;
            bool TestVerify = true;

            string input  = @"test1.pdf";
            string output = @"test1.signed.pdf";

            if (TestSign)
            {
                X509Certificate2Collection keyStore = new X509Certificate2Collection();
                X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);
                keyStore.AddRange(store.Certificates);
                store.Close();

                //Chung thu so nguoi ky
                X509Certificate2 cert = null;

                //[1] Chon chung thu so
                Console.WriteLine("Chon chung thu so ky");
                try
                {
                    cert = X509Certificate2UI.SelectFromCollection(keyStore, "Chứng thư số ký", "Chọn chứng thư số ký", X509SelectionFlag.SingleSelection)[0];
                    Console.WriteLine(cert.Subject);
                }
                catch { }

                if (cert == null)
                {
                    Console.WriteLine("Chua chon chung thu so ky");
                    return;
                }

                //[3] Ky so

                PdfSigner pdf = new PdfSigner(input, output, cert);
                pdf.Location = "Hà Nội";
                //Hiển thị chữ ký trên tài liệu dạng thông tin miêu tả
                pdf.SignatureAppearance = PdfSignatureAppearance.RenderingMode.DESCRIPTION;

                //Hiển thị chữ ký trên tài liệu dạng thông tin miêu tả và hình ảnh
                //pdf.SignatureAppearance = PdfSignatureAppearance.RenderingMode.GraphicAndDescription;
                //pdf.SignatureImage = (Bitmap)image;

                pdf.TsaUrl = "http://ca.gov.vn/tsa";

                try
                {
                    pdf.Sign(1, 0, 0, 250, 50, 0); //iPage: trang; llx: toa do X, lly: Toa do y; iWidth: rong; iHeight: cao
                    Console.WriteLine("Ky so thanh cong!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Loi: " + ex.Message);
                    return;
                }
            }

            if (TestVerify)
            {
                //[4] Xac thuc
                PdfVerifier verifier = new PdfVerifier(output);
                verifier.AllowedOnlineChecking = true;
                List <SignatureInfo> list = verifier.Verify();
                if (list.Count <= 0)
                {
                    Console.WriteLine("Van ban chua duoc ky so!");
                }
                else
                {
                    foreach (SignatureInfo info in list)
                    {
                        string   str   = string.Empty;
                        string   str2  = string.Empty;
                        CertInfo info2 = null;
                        bool     flag  = false;
                        bool     flag2 = false;
                        try
                        {
                            info2 = new CertInfo(info.SigningCertificate);
                            str   = info2.ToString();
                        }
                        catch
                        {
                            str = "N/A";
                        }
                        string str3 = string.Format("{0}: Ky boi {1}", info.SignatureName, str);
                        Console.WriteLine(str3);
                        Console.WriteLine("- Tinh trang xac thuc:");
                        if (!info.ValidityErrors.ContainsKey(SignatureValidity.DocumentModified))
                        {
                            Console.WriteLine("-- Tai lieu chua bi thay doi.");
                        }
                        else
                        {
                            Console.WriteLine("-- Tai lieu da bi thay doi.");
                            flag = true;
                        }
                        if (info.ValidityErrors.ContainsKey(SignatureValidity.InvalidSigningCertificate))
                        {
                            Console.WriteLine("-- Chung thu so khong hop le: " + info.ValidityErrors[SignatureValidity.InvalidSigningCertificate]);
                            flag = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.ErrorCheckingSigningCertificate))
                        {
                            Console.WriteLine("-- Loi kiem tra chung thu so: " + info.ValidityErrors[SignatureValidity.ErrorCheckingSigningCertificate]);
                            flag2 = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.NonCheckingRevokedSigningCert))
                        {
                            Console.WriteLine("-- Chung thu so khong duoc kiem tra trang thai truc tuyen.");
                            flag2 = true;
                        }
                        else
                        {
                            Console.WriteLine("-- Chung thu so hop le.");
                        }
                        if (info.ValidityErrors.ContainsKey(SignatureValidity.NotTimestamped))
                        {
                            Console.WriteLine("-- Chu ky khong duoc gan dau thoi gian tin cay.");
                            flag2 = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.InvalidTimestampImprint))
                        {
                            Console.WriteLine("-- Dau thoi gian khong hop le.");
                            flag2 = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.InvalidTSACertificate))
                        {
                            Console.WriteLine("-- Chung thu so TSA khong hop le:" + info.ValidityErrors[SignatureValidity.InvalidTSACertificate]);
                            flag2 = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.ErrorCheckingTSACertificate))
                        {
                            Console.WriteLine("-- Loi kiem tra chung thu so TSA: " + info.ValidityErrors[SignatureValidity.ErrorCheckingTSACertificate]);
                            flag2 = true;
                        }
                        else if (info.ValidityErrors.ContainsKey(SignatureValidity.NonCheckingRevokedTSACert))
                        {
                            Console.WriteLine("--Chung thu so TSA khong duoc kiem tra trang thai truc tuyen.");
                            flag2 = true;
                        }
                        else
                        {
                            Console.WriteLine("-- Dau thoi gian hop le.");
                        }
                        if (!flag && !flag2)
                        {
                            Console.WriteLine("==> Chu ky hop le!");
                        }
                        else if (flag)
                        {
                            Console.WriteLine("==> Chu ky khong hop le!");
                        }
                        else
                        {
                            Console.WriteLine("==> Khong du thong tin xac thuc!");
                        }
                        Console.WriteLine("--Thong tin chu ky");
                        if (!info.IsTsp)
                        {
                            try
                            {
                                str2 = info.SigningTime.ToString("dd/MM/yyyy HH:mm:ss zzz");
                            }
                            catch
                            {
                                str2 = "N/A";
                            }
                            Console.WriteLine(string.Format("--- Thoi gian ky: {0}", str2));
                        }
                        else
                        {
                            Console.WriteLine("--- Chu ky la dau thoi gian");
                        }
                        if (!info.ValidityErrors.ContainsKey(SignatureValidity.NotTimestamped))
                        {
                            Console.WriteLine("--Thong tin dau thoi gian");
                            try
                            {
                                str2 = info.TimeStampDate.ToString("dd/MM/yyyy HH:mm:ss zzz");
                            }
                            catch
                            {
                                str2 = "N/A";
                            }
                            Console.WriteLine(string.Format("--- Gia tri dau thoi gian: {0}", str2));
                            if (!info.IsTsp)
                            {
                                try
                                {
                                    str = new CertInfo(info.TimeStampCertificate).ToString();
                                }
                                catch
                                {
                                    str = "N/A";
                                }
                                Console.WriteLine(string.Format("--- Chung thu so TSA: {0}", str));
                            }
                        }
                        try
                        {
                            Console.WriteLine(string.Format("--- Chu ky tren trang {0}", info.PageIndex.ToString()));
                            continue;
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
            }

            Console.ReadLine();
        }