Код: Выделить всё
IExternalSignature externalSignature = new X509Certificate2Signature(certificate,
"SHA-1");
Код: Выделить всё
System.ArgumentException: Unknown encryption algorithm
System.Security.Cryptography.RSACng
Код: Выделить всё
using iTextSharp.text.pdf.security;
using iTextSharp.text.pdf;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System.Security.Cryptography.X509Certificates;
using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Org.BouncyCastle.Tls;
using System.Text.Json;
using Serilog;
using iTextSharp.text;
private static IList chain = new List();
private static X509Certificate2 certificate = null;
X509CertificateParser cp = new X509CertificateParser();
//Get Sertifiacte
X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
st.Open(OpenFlags.MaxAllowed);
X509Certificate2Collection collection =
X509Certificate2UI.SelectFromCollection(st.Certificates,
"Please select certificate:", "", X509SelectionFlag.SingleSelection);
if (collection.Count > 0)
{
certificate = collection[0];
}
if (certificate == null)
{
MessageBox.Show("No certificate selected!");
button3.Enabled = true;
return;
}
st.Close();
//Get Cert Chain
X509Chain x509Chain = new X509Chain();
x509Chain.Build(certificate);
foreach (X509ChainElement x509ChainElement in x509Chain.ChainElements)
{
chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
}
Код: Выделить всё
private void signPdf(
string inputFile,
string outputPath,
string imagePath,
int pageNum,
int position,
float imageWidth,
float imageHeight)
{
try
{
string fileName = Path.GetFileName(inputFile);
ouputFile = ouputFile + "\\" + fileName;
PdfReader inputPdf = new PdfReader(inputFile);
FileStream signedPdf = new FileStream(ouputFile, FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(inputPdf, signedPdf,
'\0');
IExternalSignature externalSignature =
new X509Certificate2Signature(certificate, "SHA-1");
PdfSignatureAppearance signatureAppearance =
pdfStamper.SignatureAppearance;
int NumberOfPages = inputPdf.NumberOfPages;
if (imagePath != "" && imagePath != null)
{
signatureAppearance.SignatureGraphic =
iTextSharp.text.Image.GetInstance(imagePath);
}
iTextSharp.text.Rectangle pageSize = inputPdf.GetPageSize(pageNum);
float x0 = 0;
float y0 = 0;
float x1 = 0;
float y1 = 0;
switch (position)
{
case 0:
x0 = 0;
y0 = pageSize.Height - imageHeight;
break;
case 1:
x0 = pageSize.Width - imageWidth;
y0 = pageSize.Height - imageHeight;
break;
case 2:
x0 = 0;
y0 = 0;
break;
case 3:
x0 = pageSize.Width - imageWidth;
y0 = 0;
break;
default:
break;
}
x1 = x0 + imageWidth;
y1 = y0 + imageHeight;
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(
x0,
y0,
x1,
y1),
pageNum,
GetOrganizationName(certificate));
if (renderingMode == 0)
{
signatureAppearance.SignatureRenderingMode =
PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
}
else if (renderingMode == 1)
{
signatureAppearance.SignatureRenderingMode =
PdfSignatureAppearance.RenderingMode.GRAPHIC;
}
else if (renderingMode == 2)
{
signatureAppearance.SignatureRenderingMode =
PdfSignatureAppearance.RenderingMode.DESCRIPTION;
}
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain,
null, null, null, 0,CryptoStandard.CMS);
inputPdf.Close();
pdfStamper.Close();
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... itextsharp