HELP ME!! I can't envelopped data !

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

HELP ME!! I can't envelopped data !

Lopez Thomas
Hello,

i wrote a simple code that "Envelloped data":

public static byte[] encode(X509Certificate cert,
            String filename) throws IOException, CertificateException,
            CMSException, NoSuchProviderException,
NoSuchAlgorithmException {
        File f = new File(filename);
        FileInputStream fin = new FileInputStream(f);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        for (int c = 0; (c = fin.read(buf)) > 0;)
            bos.write(buf, 0, c);

        fin.close();
        bos.close();
       
        CMSProcessableByteArray data = new CMSProcessableByteArray(bos
                .toByteArray());
        Security.addProvider(new BouncyCastleProvider());
        Provider[] listp = Security.getProviders();
        int taille = listp.length ;
        Provider MyPro = listp[taille-1];
        CMSEnvelopedDataGenerator  fact = new CMSEnvelopedDataGenerator();
        fact.addKeyTransRecipient(cert);
        String algName = CMSEnvelopedDataGenerator.AES192_CBC;
        CMSEnvelopedData  encrypted  = fact.generate(data, algName,128,
"BC");
        buf = encrypted.getEncoded();
        return buf;
    }

but i always return a strange exeption :
org.bouncycastle.cms.CMSException: key inappropriate for algorithm.
    at
org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(CMSEnvelopedDataGenerator.java:411)
    at
org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(CMSEnvelopedDataGenerator.java:472)
    at interbat.crypto.x509.info_p12.encode(info_p12.java:554)

thanks in advance.

Thomas.