# Modules.Crypto.Cryptor

The Cryptor object provides access to a number of symmetric encryption algorithms. Symmetric encryption algorithms come in two "flavors" - block ciphers, and stream ciphers. Block ciphers process data (while both encrypting and decrypting) in discrete chunks of data called blocks; stream ciphers operate on arbitrary sized data.

Availability
4.0.0
4.0.0

# Overview

The Cryptor object provides access to both block ciphers and stream ciphers with the same API; however some options are available for block ciphers that do not apply to stream ciphers. The Android version of this module only exposes block ciphers.

The general operation of a Cryptor is:

  1. Initialize it with raw key data and other optional fields with crypto.createCryptor()
  2. Process input data via one or more calls to cryptor.update()
  3. Obtain possible remaining output data with cryptor.final()
  4. The cryptor object is disposed of by setting the cryptor variable to null. The cryptor object can be reused (with the same key data as provided to crypto.createCryptor()) by calling cryptor.reset() or cryptor.release().

Alternatively, cryptor.encrypt() and cryptor.decrypt() methods are provided for a stateless, one-shot encrypt or decrypt operation.

# Properties

# algorithm

Availability
4.0.0
4.0.0
algorithm :Number

Used in the encrypt, decrypt, update and finish methods.

Default: Modules.Crypto.ALGORITHM_AES128


# initializationVector

Availability
4.0.0
4.0.0
initializationVector :Titanium.Buffer

Used in the encrypt, decrypt, update and finish methods.

Default: null


# key

Availability
4.0.0
4.0.0

Used in the encrypt, decrypt, update and finish methods.

Default: null


# operation

Availability
4.0.0
4.0.0
operation :Number

Used in the capture and parse methods and the success event to specify accepted formats.

Default: Modules.Crypto.ENCRYPT


# options

Availability
4.0.0
4.0.0
options :Number

Used in the encrypt, decrypt, update and finish methods.

Default: 0


# resizeBuffer

Availability
4.0.0
4.0.0
resizeBuffer :Boolean

Used in the encrypt, decrypt, update and finish methods. Indicates if the dataOut buffer should be resized to the size needed to hold the result of the operation.

Default: true

# Methods

# decrypt

Availability
4.0.0
4.0.0
decrypt(dataIn[, dataInLength[, dataOut[, dataOutLength]]]) Number

Stateless, one-shot decryption operation.

Parameters

Name Type Description
dataIn Titanium.Buffer

The Titanium.Buffer object containing the data to decrypt

dataInLength Number

The number of bytes in the dataIn buffer to decrypt. If this argument is not provided or is < 0, then the length of the dataIn buffer will be used.

dataOut Titanium.Buffer

The Titanium.Buffer object to receive the decrypted data. If this argument is not provided, then the dataIn buffer will be used.

  • If the resizeBuffer property is set to true, then the dataOut buffer will be resized to the size of the decrypted data
  • If the resizeBuffer property is set to false, then ensure that the dataOut buffer is large enough to receive the decrypted data.
dataOutLength Number

The number of bytes available in the dataOut buffer. If this argument is not provided or is < 0, then the length of the dataOut buffer will be used.

Returns

Returns the number of bytes decrypted into the dataOut buffer. If an error occurred, then the return value will be less than zero and will be one of the following values:

Type
Number

# encrypt

Availability
4.0.0
4.0.0
encrypt(dataIn[, dataInLength[, dataOut[, dataOutLength]]]) Number

Stateless, one-shot encryption operation.

Parameters

Name Type Description
dataIn Titanium.Buffer

The Titanium.Buffer object containing the data to encrypt

dataInLength Number

The number of bytes in the dataIn buffer to encrypt. If this argument is not provided or is < 0, then the length of the dataIn buffer will be used.

dataOut Titanium.Buffer

The Titanium.Buffer object to receive the encrypted data. If this argument is not provided, then the dataIn buffer will be used.

  • If the resizeBuffer property is set to true, then the dataOut buffer will be resized to the size of the encrypted data
  • If the resizeBuffer property is set to false, then ensure that the dataOut buffer is large enough to receive the encrypted data.
dataOutLength Number

The number of bytes available in the dataOut buffer. If this argument is not provided or is < 0, then the length of the dataOut buffer will be used.

Returns

Returns the number of bytes encrypted into the dataOut buffer. If an error occurred, then the return value will be less than zero and will be one of the following values:

Type
Number

# finish

Availability
4.0.0
4.0.0
finish(dataOut[, dataOutLength]) Number

Finishes encryption and decryption operations and obtains the final data output.

Parameters

Name Type Description
dataOut Titanium.Buffer

The Titanium.Buffer object to receive the output data. If this argument is not provided, then the dataIn buffer will be used.

  • If the resizeBuffer property is set to true, then the dataOut buffer will be resized to the size of the decrypted data
  • If the resizeBuffer property is set to false, then ensure that the dataOut buffer is large enough to receive the decrypted data.
dataOutLength Number

The number of bytes available in the dataOut buffer. If this argument is not provided or is < 0, then the length of the dataOut buffer will be used.

Returns

Returns the number of bytes moved into the dataOut buffer. If an error occurred, then the return value will be less than zero and will be one of the following values:

Type
Number

# getOutputLength

Availability
4.0.0
4.0.0
getOutputLength(dataInLength, final) Number

getOutputLength is used to determine the output buffer size required to process a given input size.

Parameters

Name Type Description
dataInLength Number

The number of bytes for the operation

final Boolean

Indicates if the calculation is for determining the output buffer size for a call to final or update. Set to true for final and false for update.

Returns

Returns the number of bytes required for the output buffer

Type
Number

# release

Availability
4.0.0
4.0.0
release() Number

release will dispose of the internal cryptor data

Returns

Returns one of the status constants:

Type
Number

# reset

Availability
4.0.0
4.0.0
reset([initializationVector]) Number

reset reinitializes an existing cryptor object with a (possibly) new initialization vector.

Parameters

Name Type Description
initializationVector Titanium.Buffer

The Titanium.Buffer object containing the initialization vector.

Returns

Returns one of the status constants:

Type
Number

# update

Availability
4.0.0
4.0.0
update(dataIn[, dataInLength[, dataOut[, dataOutLength]]]) Number

update is used to encrypt or decrypt data. This method can be called multiple times. The caller does not need to align input data lengths to block sizes; input is buffered as necessary for block ciphers.

Parameters

Name Type Description
dataIn Titanium.Buffer

The Titanium.Buffer object containing the data to decrypt

dataInLength Number

The number of bytes in the dataIn buffer to process. If this argument is not provided or is < 0, then the length of the dataIn buffer will be used.

dataOut Titanium.Buffer

The Titanium.Buffer object to receive the output data. If this argument is not provided, then the dataIn buffer will be used.

  • If the resizeBuffer property is set to true, then the dataOut buffer will be resized to the size of the decrypted data
  • If the resizeBuffer property is set to false, then ensure that the dataOut buffer is large enough to receive the decrypted data.
dataOutLength Number

The number of bytes available in the dataOut buffer. If this argument is not provided or is < 0, then the length of the dataOut buffer will be used.

Returns

Returns the number of bytes moved into the dataOut buffer. If an error occurred, then the return value will be less than zero and will be one of the following values:

Type
Number