jueves, 24 de marzo de 2011

miércoles, 16 de marzo de 2011

Encrypting using PGP (GnuPG) and Java

So I was required to encrypt some files in a bpm process using GNUPPGP. There are not too many libraries out there that makes this and are for free. So after some googling I found this site http://www.cryptix.org/ . Very cool !!!!

After I download this two links

Cryptix OpenPGP snapshot 2005/04/18 cryptix-openpgp-20050418-snap.zip
Cryptix JCE snapshot 2005/03/28 cryptix-jce-20050328-snap.zip

I create I simple Helper that will simplify the final user to use them.

My helper looks like this
  • First register the Crypto :

private static final String OPEN_PGP = "OpenPGP";
static {
//**********************************************************************
// Dynamically register both the Cryptix JCE and Cryptix OpenPGP
// providers.
//**********************************************************************
java.security.Security.addProvider(new CryptixCrypto());
java.security.Security.addProvider(new CryptixOpenPGP());
}


  • Second create a simple helper method that accepts a byte[] or Binary in BPM and returns the encrypted byte[].


public static byte[] encryptFile(File publicKey, byte[] contentToEncrypt) {
//**********************************************************************
// First read the key.
//**********************************************************************
KeyBundle publicBob = null;
FileInputStream in = null;

try {
MessageFactory mf = MessageFactory.getInstance(OPEN_PGP);
in = new FileInputStream(publicKey);
Collection msgs = mf.generateMessages(in);
KeyBundleMessage kbm = (KeyBundleMessage) msgs.iterator().next();
publicBob = kbm.getKeyBundle();
} catch (IOException ioe) {
throw new RuntimeException("IOException... You did remember to run the " + "GenerateAndWriteKey example first, right?", ioe);
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
throw new RuntimeException("Cannot find the OpenPGP MessageFactory. " + "This usually means that the Cryptix OpenPGP provider is not " + "installed correctly.", nsae);
} catch (MessageException me) {
me.printStackTrace();
throw new RuntimeException("Reading keybundle failed.", me);
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
//ignore this exception!!!
}
}


//**********************************************************************
// The actual stream encryption.
//**********************************************************************
BufferedInputStream bin = null;
LiteralMessageOutputStream literalMessageOutputStream = null;
try {
ByteArrayOutputStream encryptedFileOutputStream = new ByteArrayOutputStream();
literalMessageOutputStream = LiteralMessageOutputStream.getInstance(OPEN_PGP);
EncryptedMessageOutputStream encryptedMessage = EncryptedMessageOutputStream.getInstance(OPEN_PGP);
SecureRandom sr = new SecureRandom();
literalMessageOutputStream.init(encryptedMessage, sr); // Literal writes to Encrypted
encryptedMessage.init(encryptedFileOutputStream, sr); // Encrypted writes to file
encryptedMessage.addRecipient(publicBob);
//
literalMessageOutputStream.write(contentToEncrypt);
return encryptedFileOutputStream.toByteArray();

} catch (NoSuchAlgorithmException nsae) {
throw new RuntimeException("Cannot find OpenPGP implementation." +
" This usually means that the Cryptix OpenPGP provider is not " +
"installed correctly.", nsae);
} catch (MessageStreamException me) {
throw new RuntimeException("Streaming the message failed.", me);
} catch (IOException ioe) {
throw new RuntimeException("IO error.", ioe);
} finally {
if (bin != null) {
try {
bin.close();
} catch (IOException e) {
//ignore error
}
}
if (literalMessageOutputStream != null) {
try {
literalMessageOutputStream.close();
} catch (IOException e) {
//ignore error
}
}
}
}




Issues I found

java.security.InvalidKeyException: Illegal key size


Solution replace the security files
  1. Suppose you are using jdk15. Go to http://java.sun.com/javase/downloads/index_jdk5.jsp
  2. Go to the Other Downloads section and click on download link next to "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 5.0"
  3. Download jce_policy-1_5_0.zip and extract it in to a directory.
  4. You will find local_policy.jar and US_export_policy.jar files there in the extracted directory. Copy these two files to $JAVA_HOME/jre/lib/security directory. (These files will already be there. you may replace them)

lunes, 14 de marzo de 2011

Oracle ALBPM Performance Monitoring

Currently I'm working at credit suisse as a BPM Consultant and I will like to create a tool to monitor the performance of the engine. This must show :
  1. DB usage
  2. JMS usage
  3. Prepare statement pool
In order to implement this I will try to connect with JMX to the Weblogic server. Let see if I can...
Links :
First item: Connect to the JMX weblogic Server

  • My First approach was to use an external (Failed)
The external resource of bpm is too old and does not allow me to connect to Weblogic 10
  • Second approach Is to create my Java Library and catalog it as a Java Component (Succedded)

I can not connect to the server using JMX from outside , for security reasons, but I do can connect using the local connection. That is why I use

private MBeanServer getMBeanServer() throws NamingException {
MBeanServer server;
InitialContext ctx = new InitialContext();
server = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");
return server;
}


So this is how I will get my connection to the Weblogic MBeanServer access. Things I need to take care of :
  1. This method need to be runs on server since I like to monitor the engine I need to get the MBeanServer from the engine not PAPI
So in order to finish this I create my library with only one class and catalog it

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JMXMonitor {


public static void main(String[] args) throws Exception {


}

public Object invokeMBeanMethod(String mBeanName, String methodName, Object[] args, String[] signature) throws Exception {
MBeanServer mBeanServer = getMBeanServer();
ObjectName name = new ObjectName(mBeanName);
return mBeanServer.invoke(name, methodName, args, signature);
}

public Object getMBeanAttributeValue(String mBeanName, String attributeName) throws Exception {
MBeanServer server = getMBeanServer();
ObjectName name = new ObjectName(mBeanName);
return server.getAttribute(name, attributeName);
}

private MBeanServer getMBeanServer() throws NamingException {
MBeanServer server;
InitialContext ctx = new InitialContext();
server = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");
return server;
}
}


So once I can get the this done I have 50% of the problem resolved.

Start up

I'm starting this blog with the purpose of writing Ideas... what ever goes trough my mind... ;)