code: public CMSEnvelopedData encode(final byte[] messageData)
              throws MessageEncodingException {
          LOGGER.debug("Encoding pkcsPkiEnvelope");
          CMSEnvelopedDataGenerator edGenerator = new CMSEnvelopedDataGenerator();
          CMSTypedData envelopable = new CMSProcessableByteArray(messageData);
          RecipientInfoGenerator recipientGenerator;
          try {
              recipientGenerator = new JceKeyTransRecipientInfoGenerator(
                      recipient);
          } catch (CertificateEncodingException e) {
              throw new MessageEncodingException(e);
          }
          edGenerator.addRecipientInfoGenerator(recipientGenerator);
          LOGGER.debug(
                  "Encrypting pkcsPkiEnvelope using key belonging to [dn={}; serial={}]",
                  recipient.getSubjectDN(), recipient.getSerialNumber());
  
          OutputEncryptor encryptor;
          try {
              encryptor = new JceCMSContentEncryptorBuilder(encAlgId).build();
          } catch (CMSException e) {
              throw new MessageEncodingException(e);
          }
          try {
              CMSEnvelopedData pkcsPkiEnvelope = edGenerator.generate(
                      envelopable, encryptor);
  
              LOGGER.debug("Finished encoding pkcsPkiEnvelope");
              return pkcsPkiEnvelope;
          } catch (CMSException e) {
              throw new MessageEncodingException(e);
          }
      }
docs: Encrypts and envelops the provided messageData.
  
  @param messageData
  the message data to encrypt and envelop.
  @return the enveloped data.
  @throws MessageEncodingException
  if there are any problems encoding the message.