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
- Suppose you are using jdk15. Go to http://java.sun.com/javase/downloads/index_jdk5.jsp
- Go to the Other Downloads section and click on download link next to "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 5.0"
- Download jce_policy-1_5_0.zip and extract it in to a directory.
- 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)
No hay comentarios:
Publicar un comentario