aspose file tools
The moose likes Security and the fly likes PKCS7 BouncyCastle encrypt / decrypt  with CMSDataEnveloppe dont work ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Meteor in Action this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "PKCS7 BouncyCastle encrypt / decrypt  with CMSDataEnveloppe dont work ? " Watch "PKCS7 BouncyCastle encrypt / decrypt  with CMSDataEnveloppe dont work ? " New topic

PKCS7 BouncyCastle encrypt / decrypt with CMSDataEnveloppe dont work ?

christian leclerc
Ranch Hand

Joined: Apr 08, 2008
Posts: 38
Hello Guy's
Im trying to encrypt something in pkcs7 with the bouncy castle api.
Well I have an X509 certificate and I have able to the public key of this certificate.
I want to encrypt something for the user that have the private key of this certificate.

For the test, im able to have the X509 public certificate and the private key of the user.

This is my test case. :

Here is an example of the public certificate :

Version: V3
Subject: CN=user2
Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

Key: Sun RSA public key, 1024 bits
modulus: 104965928970371301381642596325632286151364435665409117333116382140937162604251594686222841801447769104519105677674112917419196133678894833484801421082713999709673203500688746820522804755779458343915142839059916649881971973054828663985071994033667903640037845847065659682640478177952406704508301059842880634919
public exponent: 3
Validity: [From: Wed Nov 30 11:07:16 EST 2011,
To: Fri Dec 30 11:07:16 EST 2011]
Issuer: CN=Chris, OU=Isac, O=CA, L=Mtl, ST=Qc, C=CA
SerialNumber: [ 0133f53a e205]

Algorithm: [SHA1withRSA]
0000: 85 76 FD 1F 00 FE E1 7E 8C D9 FA 88 C6 0D 7B A6 .v..............
0010: 77 CF 0F 57 20 B6 04 86 66 B5 FE 3C 82 22 0F 53 w..W ...f..<.".S
0020: B8 2F A1 8E 06 98 BC F7 A2 66 17 C5 97 F1 34 74 ./.......f....4t
0030: DA 9A 85 44 2E 3C D2 23 0F 69 13 72 0A ED 34 29 ...D.<.#.i.r..4)
0040: 5C E6 F1 F3 F8 E6 EF D5 B5 FE D4 17 04 5A 75 58 \............ZuX
0050: F8 1A 8D 4F 25 E7 9C 88 E8 CA 68 40 2A 36 93 15 ...O%.....h@*6..
0060: 1C FA 2F 03 51 09 F4 10 11 2D 1E 00 F5 E1 02 88 ../.Q....-......
0070: E0 C8 60 C6 42 9D 77 AE 76 09 A3 95 CB 9A 99 B0 ..`.B.w.v.......


Has you can see the public key is an RSA 1024 bits.
Well ... when im trying to run the test case .. i've got this exception :

org.bouncycastle.cms.CMSException: key inappropriate for algorithm.
at org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(Unknown Source)
at org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(Unknown Source)
at ca.s3s.demos.crypto.tests.TestCryptoManager.testBouncyCastleApiLightMethod(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(
at org.junit.internal.runners.MethodRoadie.runTestMethod(
at org.junit.internal.runners.MethodRoadie$
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(
at org.junit.internal.runners.MethodRoadie.runTest(
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(
at org.junit.internal.runners.JUnit4ClassRunner$
at org.junit.internal.runners.ClassRoadie.runUnprotected(
at org.junit.internal.runners.ClassRoadie.runProtected(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
Caused by: Illegal key size or default parameters
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at org.bouncycastle.cms.CMSEnvelopedGenerator$RecipientInf.toRecipientInfo(Unknown Source)
... 25 more

Its like my RSA public key can't fit with the AES128 algorythm.
Im a little screwed up here and I really want to make this work so if someone can explain me why I can't to this little encrypt / decrypt demo It will be very nice !


Arshad Noor
Ranch Hand

Joined: Oct 06, 2011
Posts: 34
Have you installed the "JCE Unlimited Strength" jar file for your JVM? See last two items on

Arshad Noor
StrongAuth, Inc.
christian leclerc
Ranch Hand

Joined: Apr 08, 2008
Posts: 38
Thank you Arshad Noor for your response !
Finally I opted for this

Its work with my key and because for the moment performance is not a concern, I will continue my demo with this way.
When the product will be approve, I will try the other method !


I agree. Here's the link:
subject: PKCS7 BouncyCastle encrypt / decrypt with CMSDataEnveloppe dont work ?