web_modules/forge.bundle.js
function(key, cipher) {
      // Part 1: Symmetric encryption
      if(msg.encryptedContent.content === undefined) {
        cipher = cipher || msg.encryptedContent.algorithm;
        key = key || msg.encryptedContent.key;

        var keyLen, ivLen, ciphFn;
        switch(cipher) {
          case forge.pki.oids['aes128-CBC']:
            keyLen = 16;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['aes192-CBC']:
            keyLen = 24;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['aes256-CBC']:
            keyLen = 32;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['des-EDE3-CBC']:
            keyLen = 24;
            ivLen = 8;
            ciphFn = forge.des.createEncryptionCipher;
            break;

          default:
            throw new Error('Unsupported symmetric cipher, OID ' + cipher);
        }

        if(key === undefined) {
          key = forge.util.createBuffer(forge.random.getBytes(keyLen));
        } else if(key.length() != keyLen) {
          throw new Error('Symmetric key has wrong length; ' +
            'got ' + key.length() + ' bytes, expected ' + keyLen + '.');
        }

        // Keep a copy of the key & IV in the object, so the caller can
        // use it for whatever reason.
        msg.encryptedContent.algorithm = cipher;
        msg.encryptedContent.key = key;
        msg.encryptedContent.parameter = forge.util.createBuffer(
          forge.random.getBytes(ivLen));

        var ciph = ciphFn(key);
        ciph.start(msg.encryptedContent.parameter.copy());
        ciph.update(msg.content);

        // The finish function does PKCS#7 padding by default, therefore
        // no action required by us.
        if(!ciph.finish()) {
          throw new Error('Symmetric encryption failed.');
        }

        msg.encryptedContent.content = ciph.output;
      }

      // Part 2: asymmetric encryption for each recipient
      for(var i = 0; i < msg.recipients.length; ++i) {
        var recipient = msg.recipients[i];

        // Nothing to do, encryption already done.
        if(recipient.encryptedContent.content !== undefined) {
          continue;
        }

        switch(recipient.encryptedContent.algorithm) {
          case forge.pki.oids.rsaEncryption:
            recipient.encryptedContent.content =
              recipient.encryptedContent.key.encrypt(
                msg.encryptedContent.key.data);
            break;

          default:
            throw new Error('Unsupported asymmetric cipher, OID ' +
              recipient.encryptedContent.algorithm);
        }
      }
    }
function(key, cipher) {
      // Part 1: Symmetric encryption
      if(msg.encryptedContent.content === undefined) {
        cipher = cipher || msg.encryptedContent.algorithm;
        key = key || msg.encryptedContent.key;

        var keyLen, ivLen, ciphFn;
        switch(cipher) {
          case forge.pki.oids['aes128-CBC']:
            keyLen = 16;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['aes192-CBC']:
            keyLen = 24;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['aes256-CBC']:
            keyLen = 32;
            ivLen = 16;
            ciphFn = forge.aes.createEncryptionCipher;
            break;

          case forge.pki.oids['des-EDE3-CBC']:
            keyLen = 24;
            ivLen = 8;
            ciphFn = forge.des.createEncryptionCipher;
            break;

          default:
            throw new Error('Unsupported symmetric cipher, OID ' + cipher);
        }

        if(key === undefined) {
          key = forge.util.createBuffer(forge.random.getBytes(keyLen));
        } else if(key.length() != keyLen) {
          throw new Error('Symmetric key has wrong length; ' +
            'got ' + key.length() + ' bytes, expected ' + keyLen + '.');
        }

        // Keep a copy of the key & IV in the object, so the caller can
        // use it for whatever reason.
        msg.encryptedContent.algorithm = cipher;
        msg.encryptedContent.key = key;
        msg.encryptedContent.parameter = forge.util.createBuffer(
          forge.random.getBytes(ivLen));

        var ciph = ciphFn(key);
        ciph.start(msg.encryptedContent.parameter.copy());
        ciph.update(msg.content);

        // The finish function does PKCS#7 padding by default, therefore
        // no action required by us.
        if(!ciph.finish()) {
          throw new Error('Symmetric encryption failed.');
        }

        msg.encryptedContent.content = ciph.output;
      }

      // Part 2: asymmetric encryption for each recipient
      for(var i = 0; i < msg.recipients.length; ++i) {
        var recipient = msg.recipients[i];

        // Nothing to do, encryption already done.
        if(recipient.encryptedContent.content !== undefined) {
          continue;
        }

        switch(recipient.encryptedContent.algorithm) {
          case forge.pki.oids.rsaEncryption:
            recipient.encryptedContent.content =
              recipient.encryptedContent.key.encrypt(
                msg.encryptedContent.key.data);
            break;

          default:
            throw new Error('Unsupported asymmetric cipher, OID ' +
              recipient.encryptedContent.algorithm);
        }
      }
    }
function ( key , cipher ) { // Part 1: Symmetric encryption
 if ( msg . encryptedContent . content === undefined ) { cipher = cipher || msg . encryptedContent . algorithm ; key = key || msg . encryptedContent . key ; var keyLen , ivLen , ciphFn ; switch ( cipher ) { case forge . pki . oids [ 'aes128-CBC' ] : keyLen = 16 ; ivLen = 16 ; ciphFn = forge . aes . createEncryptionCipher ; break ; case forge . pki . oids [ 'aes192-CBC' ] : keyLen = 24 ; ivLen = 16 ; ciphFn = forge . aes . createEncryptionCipher ; break ; case forge . pki . oids [ 'aes256-CBC' ] : keyLen = 32 ; ivLen = 16 ; ciphFn = forge . aes . createEncryptionCipher ; break ; case forge . pki . oids [ 'des-EDE3-CBC' ] : keyLen = 24 ; ivLen = 8 ; ciphFn = forge . des . createEncryptionCipher ; break ; default : throw new Error ( 'Unsupported symmetric cipher, OID ' + cipher ) ; } if ( key === undefined ) { key = forge . util . createBuffer ( forge . random . getBytes ( keyLen ) ) ; } else if ( key . length ( ) != keyLen ) { throw new Error ( 'Symmetric key has wrong length; ' + 'got ' + key . length ( ) + ' bytes, expected ' + keyLen + '.' ) ; } // Keep a copy of the key & IV in the object, so the caller can
 // use it for whatever reason.
 msg . encryptedContent . algorithm = cipher ; msg . encryptedContent . key = key ; msg . encryptedContent . parameter = forge . util . createBuffer ( forge . random . getBytes ( ivLen ) ) ; var ciph = ciphFn ( key ) ; ciph . start ( msg . encryptedContent . parameter . copy ( ) ) ; ciph . update ( msg . content ) ; // The finish function does PKCS#7 padding by default, therefore
 // no action required by us.
 if ( ! ciph . finish ( ) ) { throw new Error ( 'Symmetric encryption failed.' ) ; } msg . encryptedContent . content = ciph . output ; } // Part 2: asymmetric encryption for each recipient
 for ( var i = 0 ; i < msg . recipients . length ; ++ i ) { var recipient = msg . recipients [ i ] ; // Nothing to do, encryption already done.
 if ( recipient . encryptedContent . content !== undefined ) { continue ; } switch ( recipient . encryptedContent . algorithm ) { case forge . pki . oids . rsaEncryption : recipient . encryptedContent . content = recipient . encryptedContent . key . encrypt ( msg . encryptedContent . key . data ) ; break ; default : throw new Error ( 'Unsupported asymmetric cipher, OID ' + recipient . encryptedContent . algorithm ) ; } } }
Encrypt enveloped content.

This function supports two optional arguments, cipher and key, which
can be used to influence symmetric encryption.  Unless cipher is
provided, the cipher specified in encryptedContent.algorithm is used
(defaults to AES-256-CBC).  If no key is provided, encryptedContent.key
is (re-)used.  If that one's not set, a random key will be generated
automatically.

@param [key] The key to be used for symmetric encryption.
@param [cipher] The OID of the symmetric cipher to use.
Encrypt enveloped content .