@@ -181,16 +181,63 @@ public JceCMSContentEncryptorBuilder setAlgorithmParameters(AlgorithmParameters
181181 return this ;
182182 }
183183
184+ /**
185+ * Build the OutputEncryptor with an internally generated key.
186+ *
187+ * @return an OutputEncryptor configured to use an internal key.
188+ * @throws CMSException
189+ */
184190 public OutputEncryptor build ()
185191 throws CMSException
192+ {
193+ KeyGenerator keyGen = helper .createKeyGenerator (encryptionOID );
194+
195+ random = CryptoServicesRegistrar .getSecureRandom (random );
196+
197+ if (keySize < 0 )
198+ {
199+ keyGen .init (random );
200+ }
201+ else
202+ {
203+ keyGen .init (keySize , random );
204+ }
205+
206+ return build (keyGen .generateKey ());
207+ }
208+
209+ /**
210+ * Build the OutputEncryptor using a pre-generated key given as a raw encoding.
211+ *
212+ * @param rawEncKey a raw byte encoding of the key to be used for encryption.
213+ * @return an OutputEncryptor configured to use rawEncKey.
214+ * @throws CMSException
215+ */
216+ public OutputEncryptor build (byte [] rawEncKey )
217+ throws CMSException
218+ {
219+ SecretKey encKey = new SecretKeySpec (rawEncKey , helper .getBaseCipherName (encryptionOID ));
220+
221+ return build (encKey );
222+ }
223+
224+ /**
225+ * Build the OutputEncryptor using a pre-generated key.
226+ *
227+ * @param encKey a pre-generated key to be used for encryption.
228+ * @return an OutputEncryptor configured to use encKey.
229+ * @throws CMSException
230+ */
231+ public OutputEncryptor build (SecretKey encKey )
232+ throws CMSException
186233 {
187234 if (algorithmParameters != null )
188235 {
189236 if (helper .isAuthEnveloped (encryptionOID ))
190237 {
191- return new CMSAuthOutputEncryptor (kdfAlgorithm , encryptionOID , keySize , algorithmParameters , random );
238+ return new CMSAuthOutputEncryptor (kdfAlgorithm , encryptionOID , encKey , algorithmParameters , random );
192239 }
193- return new CMSOutputEncryptor (kdfAlgorithm , encryptionOID , keySize , algorithmParameters , random );
240+ return new CMSOutputEncryptor (kdfAlgorithm , encryptionOID , encKey , algorithmParameters , random );
194241 }
195242 if (algorithmIdentifier != null )
196243 {
@@ -212,9 +259,9 @@ public OutputEncryptor build()
212259
213260 if (helper .isAuthEnveloped (encryptionOID ))
214261 {
215- return new CMSAuthOutputEncryptor (kdfAlgorithm , encryptionOID , keySize , algorithmParameters , random );
262+ return new CMSAuthOutputEncryptor (kdfAlgorithm , encryptionOID , encKey , algorithmParameters , random );
216263 }
217- return new CMSOutputEncryptor (kdfAlgorithm , encryptionOID , keySize , algorithmParameters , random );
264+ return new CMSOutputEncryptor (kdfAlgorithm , encryptionOID , encKey , algorithmParameters , random );
218265 }
219266
220267 private class CMSOutEncryptor
@@ -252,24 +299,14 @@ private void applyKdf(ASN1ObjectIdentifier kdfAlgorithm, AlgorithmParameters par
252299 algorithmIdentifier = new AlgorithmIdentifier (kdfAlgorithm , algorithmIdentifier );
253300 }
254301
255- protected void init (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , int keySize , AlgorithmParameters params , SecureRandom random )
302+ protected void init (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , SecretKey encKey , AlgorithmParameters params , SecureRandom random )
256303 throws CMSException
257304 {
258- KeyGenerator keyGen = helper . createKeyGenerator ( encryptionOID ) ;
305+ this . encKey = encKey ;
259306
260307 random = CryptoServicesRegistrar .getSecureRandom (random );
261308
262- if (keySize < 0 )
263- {
264- keyGen .init (random );
265- }
266- else
267- {
268- keyGen .init (keySize , random );
269- }
270-
271- cipher = helper .createCipher (encryptionOID );
272- encKey = keyGen .generateKey ();
309+ this .cipher = helper .createCipher (encryptionOID );
273310
274311 if (params == null )
275312 {
@@ -327,10 +364,10 @@ private class CMSOutputEncryptor
327364 extends CMSOutEncryptor
328365 implements OutputEncryptor
329366 {
330- CMSOutputEncryptor (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , int keySize , AlgorithmParameters params , SecureRandom random )
367+ CMSOutputEncryptor (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , SecretKey encKey , AlgorithmParameters params , SecureRandom random )
331368 throws CMSException
332369 {
333- init (kdfAlgorithm , encryptionOID , keySize , params , random );
370+ init (kdfAlgorithm , encryptionOID , encKey , params , random );
334371 }
335372
336373 public AlgorithmIdentifier getAlgorithmIdentifier ()
@@ -355,10 +392,10 @@ private class CMSAuthOutputEncryptor
355392 {
356393 private MacCaptureStream macOut ;
357394
358- CMSAuthOutputEncryptor (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , int keySize , AlgorithmParameters params , SecureRandom random )
395+ CMSAuthOutputEncryptor (ASN1ObjectIdentifier kdfAlgorithm , ASN1ObjectIdentifier encryptionOID , SecretKey encKey , AlgorithmParameters params , SecureRandom random )
359396 throws CMSException
360397 {
361- init (kdfAlgorithm , encryptionOID , keySize , params , random );
398+ init (kdfAlgorithm , encryptionOID , encKey , params , random );
362399 }
363400
364401 public AlgorithmIdentifier getAlgorithmIdentifier ()
0 commit comments