Thank you very much. Best regards Barbara Zussner
corba.pdf_signed.pdf
Description: Adobe PDF document
using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; using iTextSharp.text; using iTextSharp.text.pdf; using System.Runtime.InteropServices; using Org.BouncyCastle.X509; using Org.BouncyCastle.Asn1.X509; using mctcomm; using System.Collections; using Interop.CAPICOM;
namespace PDFSignature
{
class PDFSigner
{
private byte[] pinArray;
private ManagedSignature msig;
private X509Certificate[] certs;
public PDFSigner()
{
msig = new ManagedSignature();
}
public bool initTerminal()
{
return msig.initTerminal();
}
public bool closeTerminal()
{
return msig.closeTerminal();
}
public void setPin(char[] pin)
{
// pad pin array with 0 to length 8
pinArray = new byte[8];
for (int i = 0; i < 8; i++)
{
if (i < pin.Length)
{
pinArray[i] = charToHex(pin[i]);
}
else
{
pinArray[i] = charToHex(' ');
}
}
}
public bool initCertificate()
{
byte[] crt;
byte[] certificate;
certs = new X509Certificate[1];
System.Security.Cryptography.X509Certificates.X509Certificate crl =
new System.Security.Cryptography.X509Certificates.X509Certificate("test.cer");
X509CertificateParser cp = new X509CertificateParser();
System.Security.Cryptography.X509Certificates.X509Certificate2 cert
= getCert();//new
System.Security.Cryptography.X509Certificates.X509Certificate2(certificate);
certificate = crl.GetRawCertData();
Console.WriteLine("Certificate from File:");
for (int i = 0; i < certificate.Length; i++)
{
Console.Write(certificate[i]);
Console.Write(" ");
}
Console.WriteLine();
certs[0] = cp.ReadCertificate(crl.GetRawCertData());
if (certs[0] == null)
{
return false;
}
return true;
}
public void signPDF(FileInfo file)
{
byte[] signature;
byte[] content;
byte[] hash;
FileStream outFile;
PdfReader reader;
PdfStamper stmp;
PdfSignatureAppearance vis_sign;
HashAlgorithm hashFunct = new SHA1CryptoServiceProvider();
PdfDictionary dict;
Hashtable excl = new Hashtable();
excl.Add(PdfName.CONTENTS, 48 * 2 + 2);
reader = new PdfReader(file.FullName);
outFile = File.Create(file.DirectoryName + "/" + file.Name +
"_signed.pdf"); //new FileStream(file.DirectoryName + file.Name + "signed.pdf",
FileAccess.Write);
stmp = PdfStamper.CreateSignature(reader, outFile, '\0');
vis_sign = stmp.SignatureAppearance;
vis_sign.SetCrypto(null, certs, null,
PdfSignatureAppearance.WINCER_SIGNED);
vis_sign.Reason = "fact4web.com PDF Signatur";
vis_sign.Location = "Austria";
vis_sign.SignDate = DateTime.Now;
vis_sign.SetExternalDigest(new byte[48], null, "RSA");
vis_sign.Render =
PdfSignatureAppearance.SignatureRender.NameAndDescription;
vis_sign.Acro6Layers = true;
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS,
PdfName.ADBE_PKCS7_SHA1);
dic.Date = new PdfDate(vis_sign.SignDate);
dic.Name = PdfPKCS7.GetSubjectFields(certs[0]).GetField("CN");
if (vis_sign.Reason != null)
dic.Reason = vis_sign.Reason;
if (vis_sign.Location != null)
dic.Location = vis_sign.Location;
vis_sign.CryptoDictionary = dic;
int csize = 4000;
Hashtable exc = new Hashtable();
exc[PdfName.CONTENTS] = csize * 2 + 2;
vis_sign.PreClose(excl);
content = streamToByteArray(vis_sign.RangeStream);
hash = hashFunct.ComputeHash(content);
unsafe
{
fixed (byte* pinPtr = pinArray)
{
fixed (byte* hashPtr = hash)
{
//IntPtr ptrPin = (IntPtr)pinPtr;
//IntPtr ptrHash = (IntPtr)hashPtr;
byte* sigPtr = msig.signDocument(pinPtr, hashPtr);
int i = 0;
signature = new byte[50];
while (sigPtr[i] != '\0')
{
signature[i] = sigPtr[i];
i++;
}
//signature = sigPtr;
}
}
}
byte[] sig = new byte[signature.Length-2];
for (int i = 0; i < hash.Length; i++)
{
Console.Write(hash[i].ToString("X2"));
Console.Write(" ");
}
dict = new PdfDictionary();
Array.Copy(signature, 0, sig, 0, signature.Length-2);
dict.Put(PdfName.CONTENTS, new PdfString(sig).SetHexWriting(true));
vis_sign.Close(dict);
}
private byte[] streamToByteArray(Stream stream)
{
MemoryStream byteArray = new MemoryStream();
if (stream != null)
{
byte[] buffer = new byte[1024];
int c = stream.Read(buffer, 0, buffer.Length);
while (c > 0)
{
byteArray.Write(buffer, 0, c);
c = stream.Read(buffer, 0, 1024);
}
byteArray.Flush();
}
return byteArray.ToArray();
}
private byte charToHex(char c)
{
byte hex;
switch (c)
{
case '0':
hex = 0x30;
break;
case '1':
hex = 0x31;
break;
case '2':
hex = 0x32;
break;
case '3':
hex = 0x33;
break;
case '4':
hex = 0x34;
break;
case '5':
hex = 0x35;
break;
case '6':
hex = 0x36;
break;
case '7':
hex = 0x37;
break;
case '8':
hex = 0x38;
break;
case '9':
hex = 0x39;
break;
default:
hex = 0x00;
break;
}
return hex;
}
private System.Security.Cryptography.X509Certificates.X509Certificate2
getCert()
{
System.Security.Cryptography.X509Certificates.X509Store st = new
System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My,
System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser);
st.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly);
System.Security.Cryptography.X509Certificates.X509Certificate2Collection col =
st.Certificates;
System.Security.Cryptography.X509Certificates.X509Certificate2 card
= null;
System.Security.Cryptography.X509Certificates.X509Certificate2Collection sel =
System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(col,
"Certificates", "Select one to sign",
System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
if (sel.Count > 0)
{
System.Security.Cryptography.X509Certificates.X509Certificate2Enumerator en =
sel.GetEnumerator();
en.MoveNext();
card = en.Current;
}
st.Close();
return card;
}
}
}
------------------------------------------------------------------------- SF.Net email is sponsored by: Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://itext.ugent.be/itext-in-action/
