Just wanted to follow up and help the next poor guy that hits
this stuff... This is what worked for me.
/**
* Created Oct 23, 2008 by Rick DuVall
*
* EncryptFile2.java com.daaokc.eftutils
*
*
* Encrypt a file using a public key given to me by one of our trading partners
*
* Thanks To Tamas Perlaky for his help
*
* The main problem I had with this was my lack of
* Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.
*
* Download jce_policy-x_y_z.zip (relevant to your JDK version)
* and extract the jar files local_policy.jar and
* US_export_policy.jar to $JAVA_HOME/jre/lib/security.
*
* The above jars are REQUIRED for this to work!
*
* remove the extends MyUtilClass
*/
package com.daaokc.eftutils;
/**
* Encrypt a file using a public key given to me by one of our trading partners
*
* @author Rick DuVall Oct 23, 2008
*
*/
public class EncryptFile2 extends MyUtilClass{
/**
* Constructor
*
* EncryptFile2
*
* @param inFile - String the file name & location to Encrypt
* @param outFile - String the requested Output file name & location
* @param keyFile - the file containing the public key for encryption
* @throws Exception
*/
EncryptFile2(String inFile,String outFile, String keyFile)throws Exception{
Security.addProvider(new BouncyCastleProvider());
FileInputStream keyIn = new FileInputStream(keyFile);
FileOutputStream out = new FileOutputStream(outFile.trim());
encryptFile(out, inFile, readPublicKey(keyIn));
}
/**
* A simple routine that opens a key ring file and loads the first available key suitable for
* encryption.
* @return k - PGPPublicKey
* @param in
* @throws IOException
* @throws PGPException
*/
private PGPPublicKey readPublicKey(InputStream in)throws IOException, PGPException{
in = PGPUtil.getDecoderStream(in);
PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);
//========================================================================================
// we just loop through the collection till we find a key suitable for encryption, if your
// key file has more than one key you would probably want to be a bit more careful...
//========================================================================================
// iterate through the key rings.
//========================================================================================
Iterator rIt = pgpPub.getKeyRings();
while (rIt.hasNext()){
PGPPublicKeyRing kRing = (PGPPublicKeyRing)rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (kIt.hasNext()){
PGPPublicKey k = (PGPPublicKey)kIt.next();
if (k.isEncryptionKey()){
return k;
}
}
}
throw new IllegalArgumentException("Can't find encryption key in key ring.");
}
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.