ho una classe che dovrebbe criptare un file..
problema è che quando la eseguo mi da un errore..
cercando in rete ho scoperto che è dovuto al file di policy contenuto nel jar..
ho un modo per aggirarlo ?
contando che il programma dovrà essere intallato sui clienti, non posso interagire sulla jre di ciascuno..
non esiste un modo per specificare qualcosa da progetto ?
codice:
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.KeyTransRecipientInformation;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
String pathFile = "prove/x509NOPPR8002_20090401.zip.p7m";
String pathCert = "prove/concentratore.cer";
//Plug the Provider into the JCA/JCE
Security.addProvider(new BouncyCastleProvider());
FileInputStream freader = null;
File f = null;
X509Certificate cert = null;
//------ Get the content data from file -------------
f = new File(pathFile) ;
int sizecontent = ((int) f.length());
byte[] contentbytes = new byte[sizecontent];
try {
freader = new FileInputStream(f);
System.out.println("\nContent Bytes: " + freader.read(contentbytes, 0, sizecontent));
freader.close();
}
catch(IOException ioe) {
System.out.println(ioe.toString());
return;
}
//------ Get recipient certificate from file -------------
try{
InputStream inStream = new FileInputStream(pathCert);
CertificateFactory cf = CertificateFactory.getInstance("X509");
cert = (X509Certificate)cf.generateCertificate(inStream);
inStream.close();
String[] infos_emetteur = cert.getIssuerDN().getName().split("(=|, )", -1);
String[] infos_titulaire = cert.getSubjectDN().getName().split("(=|, )", -1);
System.out.println("CommonName : " + infos_titulaire[3]);
System.out.println("EmailAdresse : " + infos_titulaire[1] + "\n");
for (int i = 0; i < infos_emetteur.length; i += 2)
{
if (infos_emetteur[i].equals("C"))
System.out.println("CountryName : " + infos_emetteur[i + 1]);
if (infos_emetteur[i].equals("O"))
System.out.println("OrganizationName : " + infos_emetteur[i + 1]);
if (infos_emetteur[i].equals("CN"))
System.out.println("CommonName : " + infos_emetteur[i + 1]);
}
System.out.println("valido");
System.out.println("dal : " + cert.getNotBefore());
System.out.println("al : " + cert.getNotAfter());
}
catch(Exception exc){
System.out.println("Couldn't instantiate X.509 certificate");
return;
}
// --- Use Bouncy Castle provider to create enveloped message ---
String algorithm = CMSEnvelopedDataGenerator.DES_EDE3_CBC;
int keysize = 192; // bits
CMSEnvelopedDataGenerator fact = new CMSEnvelopedDataGenerator();
fact.addKeyTransRecipient((X509Certificate)cert);
CMSProcessableByteArray content = new CMSProcessableByteArray(contentbytes);
try{
CMSEnvelopedData envdata = fact.generate(content, algorithm, keysize, "BC");
byte[] enveloped = envdata.getEncoded() ;
System.out.println("Got encoded pkcs7 bytes " + enveloped.length + " bytes") ;
FileOutputStream envfos = new FileOutputStream("BCenveloped.p7");
envfos.write(enveloped);
envfos.close();
}
catch(CMSException ex){
System.out.println(ex.getUnderlyingException());
ex.printStackTrace();
} catch (IOException e) {
System.out.println("COuldn't generate enveloped signature") ;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}