3

I'm using BountyCastle to encrypt a file using the algorithm AES128_CBC with the following code :

static {
    Provider provider = Security.getProvider (BouncyCastleProvider.PROVIDER_NAME);
    if (provider == null) {
        Security.addProvider (new BouncyCastleProvider ());
    }
}

public static void main (String[] args) throws  IOException, CertificateException, UnrecoverableKeyException, KeyStoreException,
                                                NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException, 
                                                CertStoreException, CMSException, NoSuchPaddingException, InvalidKeyException, 
                                                ShortBufferException, IllegalBlockSizeException, BadPaddingException {

    File f             = new File ("ToBeEncrypted.txt");
    byte[] buffer      = new byte [(int)f.length ()];
    DataInputStream in = new DataInputStream (new FileInputStream (f));
    in.readFully (buffer);
    in.close ();

    X509Certificate cert = ReadX509.read (new FileInputStream ("test.cer"));

    CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator ();
    RecipientInfoGenerator recipientGenerator = new JceKeyTransRecipientInfoGenerator (cert).setProvider ("BC");
    gen.addRecipientInfoGenerator (recipientGenerator);

    OutputEncryptor outputEncryptor = new JceCMSContentEncryptorBuilder (CMSAlgorithm.AES128_CBC).build ();
    CMSEnvelopedData envData        = gen.generate (new CMSProcessableByteArray (buffer), outputEncryptor);

    byte[] pkcs7envelopedData = envData.getEncoded ();

    FileOutputStream envfos   = new FileOutputStream ("ToBeDecrypted.pk7");
    envfos.write (pkcs7envelopedData);
    envfos.close ();
}

However, I keep having the following exception on the line

CMSEnvelopedData envData = gen.generate (new CMSProcessableByteArray (buffer), outputEncryptor); :

Exception in thread "main" org.bouncycastle.cms.CMSException: exception wrapping content key: cannot create cipher: No such algorithm: 1.2.840.10040.4.1
    at org.bouncycastle.cms.KeyTransRecipientInfoGenerator.generate(Unknown Source)
    at org.bouncycastle.cms.CMSEnvelopedDataGenerator.doGenerate(Unknown Source)
    at org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(Unknown Source)
    at com.crypto.tests.EncryptDocument.main(EncryptDocument.java:74)

Caused by: org.bouncycastle.operator.OperatorCreationException: cannot create cipher: No such algorithm: 1.2.840.10040.4.1
    at org.bouncycastle.operator.jcajce.OperatorHelper.createAsymmetricWrapper(Unknown Source)
    at org.bouncycastle.operator.jcajce.JceAsymmetricKeyWrapper.generateWrappedKey(Unknown Source)
... 4 more

Caused by: java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.10040.4.1
    at javax.crypto.Cipher.getInstance(Cipher.java:688)
    at javax.crypto.Cipher.getInstance(Cipher.java:596)
    at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createCipher(Unknown Source)
    ... 6 more

Any idea ?

1 Answer 1

2

You cannot encrypt with the DSA algorithm or DSA key (1.2.840.10040.4.1 is DSA). DSA stands for Digital Signature Algorithm. Try and use RSA instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. So basically what you're saying is that the certificate I'm using is generated by DSA ?
@Copernic That could very well be the case yes. RSA would be a better idea.
Oh.. Makes sense ! Thank you @Maarten !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.