Friday, November 26, 2010

Working with JTables

With JTables you can display tables of data, allowing the user to edit the data. Fallow this example if you want to learn how to fill a specific position in a JTable.
  • Create a new JDialog Form.
  • Drag and drop the JTable swing component into the JDialog Form. You should have something like this,

Tuesday, November 16, 2010

Image inside a Scroll Pane

Today I'm going to show you how you can place an image inside Scroll Pane.

  • Create new JDialog Form,

Thursday, November 11, 2010

How to sign JAR files

Since JAR files need to be downloaded as part of a Java applet you should digitally sign them. Before we can start make sure you have Java SDK keytool and jarsigner in your path. You can find these tools in Java SDK bin directory.

Create a new key,
  • keytool -genkey -keystore myKeystore -alias myself

When asked, fill the information regarding the new key name, password, etc. This procedure creates myKeystore on you disk.

Create a self signed certificate,
  • keytool -selfcert -alias myself -keystore myKeystore

List the contents of the keystore (not mandatory),
  • keytool -list -keystore myKeystore

        Keystore type: jks
        Keystore provider: SUN

        Your keystore contains 1 entry:
        myself, Fri Nov 12 19:29:32 PST 2010, keyEntry,
        Certificate fingerprint (MD5):
        C2:E9:BF:F9:D3:DF:4C:8F:3C:5F:22:9E:AF:0B:42:9D

Repeat these last step to sign all your JAR files,
  • jarsigner -keystore myKeystore test.jar myself

Read certificates from windows store


If you are on Java 6, you can use the MSCAPI keystore to read it. Just open your keystore like this,

KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
ks.load(null, null);
java.util.Enumeration en = ks.aliases();

while (en.hasMoreElements()) {
  String aliasKey = (String) en.nextElement();
  Certificate[] chain = ks.getCertificateChain(aliasKey);
  X509Certificate x509 = ((X509Certificate) chain[0]);

  boolean keyUsage[] = x509.getKeyUsage();

  //keyUsage[0] -> digitalSignature
  //keyUsage[1] -> nonRepudiation
  //keyUsage[2] -> keyEncipherment
  //keyUsage[3] -> dataEncipherment
  //keyUsage[4] -> keyAgreement
  //keyUsage[5] -> keyCertSign
  //keyUsage[6] -> cRLSign
  //keyUsage[7] -> encipherOnly
  //keyUsage[8] -> decipherOnly
}

To load windows root certificates simple replace the first line (highlighted) with this,

KeyStore ks = KeyStore.getInstance("Windows-MY", "Windows-ROOT");


To know more about the subject please read this

Tuesday, November 9, 2010

How to sign a PDF using iText and iTextSharp

iText supports visible and invisible signing using the following modes:
  • Self signed (Adobe.PPKLite)
  • VeriSign plug-in (VeriSign.PPKVS)
  • Windows Certificate Security (Adobe.PPKMS)
Signing and verifying with iText is easy, and it's always done the same way, the difficult part comes with the key and certificate generation.

Signing is done with this simple code:

KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("my_private_key.pfx"), "my_password".toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, "my_password".toCharArray());

Certificate[] chain = ks.getCertificateChain(alias);

PdfReader reader = new PdfReader("original.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");

PdfStamper stp = PdfStamper.createSignature(reader,fout,'\0');
PdfSignatureAppearance sap = stp.getSignatureAppearance();

sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
sap.setReason("I'm the author");

sap.setLocation("Lisbon");
sap.setVisibleSignature(new Rectangle(50,50,200,200),1,null); 
stp.close();