cat.debug("Succesfully extracted PKCS10.");
log.debug("Successfully extracted PKCS10.");
    private void decrypt() throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, CMSException,    NoSuchProviderException, BadPaddingException, InvalidAlgorithmParameterException, GeneralSecurityException, IOException {        cat.debug(">decrypt");        // Now we are getting somewhere (pheew),        // Now we just have to get the damn key...to decrypt the PKCS10        if (envEncData == null) {            cat.error("No enveloped data to decrypt!");            return;        }            CMSEnvelopedData ed = new CMSEnvelopedData(envEncData);            RecipientInformationStore  recipients = ed.getRecipientInfos();            Collection  c = recipients.getRecipients();            Iterator    it = c.iterator();            byte[] pkcs10Bytes = null;            while (it.hasNext())            {                RecipientInformation   recipient = (RecipientInformation)it.next();                pkcs10Bytes = recipient.getContent(privateKey, "BC");                break;            }/*        if (envData == null) {            cat.error("No enveloped data to decrypt!");            return;        }        Enumeration ris = envData.getRecipientInfos().getObjects();        if (ris.hasMoreElements()) {            RecipientInfo ri = RecipientInfo.getInstance(ris.nextElement());            DEREncodable info = ri.getInfo();            if (info instanceof KeyTransRecipientInfo) {                KeyTransRecipientInfo kti = (KeyTransRecipientInfo)info;                AlgorithmIdentifier keyEncAlg = AlgorithmIdentifier.getInstance(kti.getKeyEncryptionAlgorithm());                String id = keyEncAlg.getObjectId().getId();                if(id.equals(PKCSObjectIdentifiers.rsaEncryption.getId())) {                    cat.debug("Found key encrypted with RSA inside message.");                    //RecipientIdentifier rid = kti.getRecipientIdentifier();                    DEREncodable rid = kti.getRecipientIdentifier().getId();                    if (rid instanceof IssuerAndSerialNumber) {                        cat.debug("Issuer and serialnumer of recipient:");                        cat.debug("Issuer: "+((IssuerAndSerialNumber)rid).getName().toString());                        cat.debug("SerialNo: "+((IssuerAndSerialNumber)rid).getSerialNumber().getValue().toString());                        cat.debug("My key Issuer: "+cert.getIssuerDN().toString());                        cat.debug("My serialNo: "+cert.getSerialNumber().toString());                    }                    //boolean ok = checkKeys(cert.getPublicKey(), privateKey);                    //if (!ok) {                    //    cat.error("Public and private keys do not match!");                    //    return;                    //}                    // At least OpenSCEP uses nopadding, go figure...//                    Cipher cipher = Cipher.getInstance("RSA/NONE/NOPADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/ECB/NOPADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/ECB/ISO9796-1PADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/NONE/NOPADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPPADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/NONE/ISO9796-1PADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/1/PCKS1PADDING", "BC");//                    Cipher cipher = Cipher.getInstance("RSA/2/PCKS1PADDING", "BC");                    Cipher cipher = Cipher.getInstance(id, "BC");                    cipher.init(Cipher.DECRYPT_MODE, privateKey);                    cat.info("blocksize="+cipher.getBlockSize());                    cat.info("keysize="+((RSAPrivateKey)privateKey).getPrivateExponent().bitLength());                    byte[] encKey = kti.getEncryptedKey().getOctets();                    cat.info("Encrypted keybytes: "+encKey.length);                    byte[] dekBytes = cipher.doFinal(encKey);                    cat.info("Decrypted keybytes: "+dekBytes.length);                    byte[] cekBytes = unpad(dekBytes);                    cat.info("Unpadded keybytes: "+cekBytes.length);                    AlgorithmIdentifier aid = envData.getEncryptedContentInfo().getContentEncryptionAlgorithm();                    String alg = aid.getObjectId().getId();                    cat.info("Symm alg="+alg);                    AlgorithmParameterSpec iv = getIv(alg, aid.getParameters());                    SecretKey cek = getContentEncryptionKey(cekBytes, alg);                    cat.debug("Extracted secret key.");                    byte[] enc = envData.getEncryptedContentInfo().getEncryptedContent().getOctets();                    FileOutputStream fos1 = new FileOutputStream("C:\\pkcs10.enc");                    fos1.write(enc);                    fos1.close();                    cipher = getCipher(alg);                    if(iv == null) {                        cat.debug("IV is null.");                        cipher.init(Cipher.DECRYPT_MODE, cek);                    } else {                        cat.debug("IV is NOT null.");                    cat.info("blocksize="+cipher.getBlockSize());                    cat.info("key alg="+cek.getAlgorithm());                    cat.info("enc key size="+cekBytes.length);                    cipher.init(Cipher.DECRYPT_MODE, cek, iv);                    }                    byte[] pkcs10Bytes = unpad(cipher.doFinal(enc));                    FileOutputStream fos = new FileOutputStream("C:\\pkcs10.txt");                    fos.write(Base64.encode(pkcs10Bytes));                    fos.close();                    DERObject derobj = new DERInputStream(new ByteArrayInputStream(pkcs10Bytes)).readObject();                    DERConstructedSequence seq = (DERConstructedSequence)derobj;                    pkcs10 = new PKCS10CertificationRequest(seq);                    cat.debug("Succesfully extracted PKCS10.");                } else {                    cat.error("Key not encrypted with RSA!");                    error = 4;                }            } else {                cat.error("RecipientInfo is not KeyTransRecipientInfo!");                error = 5;            }        }        */        cat.debug("<decrypt");    } // decrypt