How to sign string with private key

I guess what you say is you know the key pair before hand and want to sign/verify with that. Please see the following code. import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.Signature; import sun.misc.BASE64Encoder; public class MainClass { public static void main(String[] args) throws Exception { KeyPair keyPair = getKeyPair(); byte[] data = “test”.getBytes(“UTF8”); Signature … Read more

Sign PDF with iTextSharp 5.3.3 and USB token

This approach works fine for us (iTextSharp 5.3.3). We use smart-card and USB-token (vendor – www.author.kiev.ua): X509Store store = new X509Store(StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection); X509Certificate2 cert = sel[0]; Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData)}; IExternalSignature externalSignature = new X509Certificate2Signature(cert, “SHA-1”); PdfReader pdfReader = new … Read more

How to check if a file is signed in C#? [closed]

Assuming you want to check if a file is Authenticode signed and that the certificate is trusted you can pinvoke to WinVerifyTrust in Wintrust.dll. Below is a wrapper (more or less reproduced from here) that can be called as follows: AuthenticodeTools.IsTrusted(@”path\to\some\signed\file.exe”) Where AuthenticodeTools is defined as follows: internal static class AuthenticodeTools { [DllImport(“Wintrust.dll”, PreserveSig = … Read more