#36 Problem with Gnu.Classpath.Core.dll

closed
nobody
None
5
2006-05-29
2006-03-06
Anonymous
No

Thank you very much for porting the JAVA classes
to .NET. However I have problem with your
Gnu.Classpath.Core.dll and I would like see if you
can help me out. I am using iTextSharp to digitally
sign PDF. I found how to do it with the JAVA
implementation – iText. Everything went fine until I
tried to use the KeyStore, it throws exception when I
provide type see my code:

KeyStore ks = KeyStore.getInstance
(KeyStore.getDefaultType()); //here it throws an
expception
ks.load(new java.io.FileInputStream
(@"C:\temp\keystore.ks"), "123456".ToCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey
(alias, "123456".ToCharArray());
jp.ujihara.java.security.cert.Certificate[] chain =
ks.getCertificateChain(alias);
PdfReader reader = new PdfReader(@"C:\temp\test.pdf");
FileOutputStream fout = new FileOutputStream
(@"C:\temp\signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader,
fout, '\0');
PdfSignatureAppearance sap =
stp.getSignatureAppearance();
sap.setCrypto(key, chain, null,
PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("I'm the author");
sap.setLocation("Lisbon");
// comment next line to have an invisible signature
sap.setVisibleSignature(new Rectangle(100, 100, 200,
200), 1, null);
stp.close();

I tried several types which I found googling the
Internet with no luck. Can you help me?

Regards,

Discussion

  • Kazuya Ujihara

    Kazuya Ujihara - 2006-03-06

    Logged In: YES
    user_id=734754

    At first, you need to set default KeyStore type
    like 'Security.setProperty("keystore.type", "pkcs12")'.
    And loading another securuty provider is required because
    GNU classpath and J# doesn't support PKCS#12. So you need
    to load SecurityProvider supporting PKCS12 such as
    BouncyCastle.
    The following is the sample code.

    package jp.ujihara.itext.test.sign;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import jp.ujihara.java.security.KeyStore;
    import jp.ujihara.java.security.cert.Certificate;
    import jp.ujihara.java.security.PrivateKey;
    import com.lowagie.text.Rectangle;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    import com.lowagie.text.pdf.PdfSignatureAppearance;
    import jp.ujihara.java.security.Security;

    public class HowToUseKeyStore {
    static final int SIGNTYPE_INVISIBLE = 0;
    static final int SIGNTYPE_VISIBLE = 1;
    static final int SIGNTYPE_FIELD = 2;

    public static void main(String[] args) throws
    Exception {
    jp.ujihara.java.security.Security.addProvider(new
    org.bouncycastle.jce.provider.BouncyCastleProvider());

    Security.setProperty
    ("keystore.type", "pkcs12");

    stampSignature
    ("HelloWorld.pdf", "VisibleSigned.pdf", "self-sign-
    test.pfx", "password", SIGNTYPE_VISIBLE, null, false, new
    Rectangle(100, 100, 200, 200), 1, "Signed by visible
    signature.", "Hyogo");
    stampSignature
    ("HelloWorld.pdf", "InvisibleSigned.pdf", "self-sign-
    test.pfx", "password", SIGNTYPE_INVISIBLE, null, false,
    null, 0, "Signed by invisible signature.", "Takarazuka");
    stampSignature
    ("SignatureField.pdf", "SignedOnField.pdf", "self-sign-
    test.pfx", "password", SIGNTYPE_FIELD, "Signature", false,
    null, 0, "Signed on a field.", "Japan");
    stampSignature
    ("SignedOnField.pdf", "MultiSigned.pdf", "self-sign-
    test2.pfx", "password2", SIGNTYPE_VISIBLE, null, true, new
    Rectangle(300, 100, 400, 200), 1, "Multi-Signed by visible
    signature.", "Tokyo");
    }

    static void stampSignature(String filename, String
    signedFilename,
    String keyFilename, String password,
    int signType, String fieldName, boolean
    append,
    Rectangle rect, int page,
    String reason, String location) throws
    Exception
    {
    KeyStore ks = KeyStore.getInstance
    (KeyStore.getDefaultType());
    ks.load(new java.io.FileInputStream
    (keyFilename), password.toCharArray());
    String alias = (String)ks.aliases
    ().nextElement();
    PrivateKey key = (PrivateKey)ks.getKey
    (alias, password.toCharArray());
    jp.ujihara.java.security.cert.Certificate
    [] chain = ks.getCertificateChain(alias);
    PdfReader reader = new PdfReader(filename);
    FileOutputStream fout = new
    FileOutputStream(signedFilename);
    PdfStamper stp = PdfStamper.createSignature
    (reader, fout, '\0');
    PdfSignatureAppearance sap =
    stp.getSignatureAppearance();
    sap.setCrypto(key, chain, null,
    PdfSignatureAppearance.SELF_SIGNED);
    sap.setReason("I'm the author");
    sap.setLocation("Lisbon");
    // comment next line to have an invisible
    signature
    sap.setVisibleSignature(new Rectangle(100,
    100, 200, 200), 1, null);
    stp.close();
    }
    }

     
  • Kazuya Ujihara

    Kazuya Ujihara - 2006-05-29
    • status: open --> closed
     

Log in to post a comment.