× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hello Group,

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;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Iterator;

import javax.swing.JOptionPane;

/**
* 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.");
}



/**
* encryptFile
*
* @param out - OutputStream toFile
* @param fileName - String file to Encrypt
* @param encKey - PGPPublicKey read from keyfile
*/
private void encryptFile(OutputStream out,String fileName,PGPPublicKey encKey)
throws IOException, NoSuchProviderException{

try{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();

PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY, new File(fileName));
comData.close();

PGPEncryptedDataGenerator cPk =
new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, false, new SecureRandom(), "BC");

cPk.addMethod(encKey);

byte[] bytes = bOut.toByteArray();
OutputStream cOut = cPk.open(out, bytes.length);
cOut.write(bytes);
cOut.close();

out.close();
}
catch (PGPException e){
System.out.println(e);
JOptionPane.showMessageDialog(null, formatErrorStrings("Error Encrypting File :"+e.getMessage()),
"Encryption Error", JOptionPane.ERROR_MESSAGE);
if (e.getUnderlyingException() != null){
e.getUnderlyingException().printStackTrace();
}
}
}




As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.