The Cipher
class handles all encryption and decryption including AEAD
as well as provides various information about selecte cipher algorithm.
The CBC (Cipher Block Chaining) mode XOR's the previous block with the currently en/decrypted one. It requires random IV to be set.
The CCM (Counter with CBC-MAC) is an authenticated mode. It requires a length pre-initialization which means that a plain resp. cipher text must be known before encryption resp. decryption. That makes it unsituable for streams or continuous cipher update.
Encryption is done using CTR (Counter) mode which means that a supplied nonce and counter is used. The nonce is passed as an IV. The default tag size is 16 bytes.
The CFB (Cipher FeedBack) mode makes a block ciphper into a self-synchronizing stream cipher.
The CTR (CounTeR) mode uses counter and a random nonce.
The ECB (Electronic Codebook) mode is an insecure mode susceptible on replay attack if a message length is greater than a block length.
The GCM (Golias Counter Mode) is an authenticated mode.
The OFB (Output FeedBack) mode makes a block cipher into a synchronous stream cipher
The XTS (XEX-based tweaked codebook mode with ciphertext stealing) mode is a mode designed for hard disk storage.
Description: Creates a cipher using a static call syntax.
The usage of __callStatic
magic method allows simplified syntax
for creating a Cipher
object
(e.g. Cipher::aes(Crypto\Cipher::MODE_CBC, 128)
). The $name
depicts the algorithm which is checked if it's found. If not
then CipherException
is thrown. Otherwise the new Cipher
instance is returned.
name : string
- the algorithm name (e.g. aes
)
arguments : array
- there should be an algorithm mode
and key size (if supported by algorithm)
Cipher
: New instances of the class.
It can throw CipherException
with code
CipherException::ALGORITHM_NOT_FOUND
- the algorithm (name) is not found
$cipher = \Crypto\Cipher::aes(\Crypto\Cipher::MODE_CBC, 128);
Description: Returns all cipher algorithms.
This static method returns all cipher algorithms. Their parameters
allow filtering of the result. Some algorithms have aliases that
can be returned if the $aliases
parameter is true
. The $prefix
allows filtering by the supplied prefix string.
aliases : bool
- whether to show aliases
prefix : string
- prefix that is used for filtering of the result
This method does not throw any exception.
array
: list of supported cipher alorithms
print_r(\Crypto\Cipher::getAlgorithms());
Description: Finds out wheter the supplied algorithm is supported
This static method checks if the supplied cipher algorithm is supported.
algorithm : string
- algorithm name
This method does not throw any exception.
bool
: if the algorithm is supperted, returns true
, otherwise false
if (\Crypto\Cipher::hasAlgorithm('aes-128-ccm')) {
// use AES wiht CCM mode
}
Description: Finds out wheter the supplied mode is supported
This static method checks if the supplied cipher mode is supported.
The $mode
parameter must be one of the mode cipher constant
defined in Cipher
class.
mode : int
- mode constant
This method does not throw any exception.
bool
: if the mode is supperted, returns true
, otherwise false
if (\Crypto\Cipher::hasMode(\Crypto\Cipher::MODE_CCM)) {
// use CCM mode
}
Description: Creates a new cipher object
The constructor allows creating an object using two ways. Either
algorithm name is a string containing all details (algorithm, mode,
key size) or it is just a name of the block algorithm (e.g. AES)
followed by mode Cipher
class constant and, if algorithm allows
that, then key size. Internally the name is concatened to the first
form so the result is the same. The final algorithm name is then
checked if it is supported. If not, then the CipherException
is thrown.
algorithm : string
- algorithm name
mode : int
- mode constant
key_size : int
- algorithm key size
It can throw CipherException
with code
CipherException::ALGORITHM_NOT_FOUND
- the algorithm (name) is not found
Cipher
: new cipher object
Creating cipher using just algorithm parameter
$cipher = new \Crypto\Cipher('AES-128-GCM');
Creating cipher using composed parameters
use Crypto\Cipher;
$cipher = new Cipher('AES', Cipher::MODE_GCM, 128);
Description: Decrypts encrypted data using key and IV
This method decrypts encrypted data (cipher text) on the Cipher
object. It uses a supplied key $key
and an initial vector $iv
for decryption. Internally it calls init, update and finish
operations on cipher context. If any of them fails, a CipherException
with an appropriate code is thrown.
The key resp. IV parameters has to contain an exact number of bytes
that is returned by Cipher::getKeyLength
resp. Cipher::getIVLength()
.
If it's not the case, then a CipherException
is thrown.
data : string
- cipher text
key : string
- key
iv : string
- initial vector
It can throw CipherException
with code
CipherException::INIT_ALG_FAILED
- initialization of cipher algorithm failedCipherException::INIT_CTX_FAILED
- initialization of cipher context failedCipherException::UPDATE_FAILED
- updating of decryption failedCipherException::FINISH_FAILED
- finalizing of decryption failedCipherException::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds C INT_MAXCipherException::KEY_LENGTH_INVALID
- the key length is invalidCipherException::IV_LENGTH_INVALID
- the IV length is invalidCipherException::TAG_VERIFY_FAILED
- tag verification failed (only for GCM or CCM mode)
string
: The decrypted plain text.
$cipher = new \Crypto\Cipher('AES-128-GCM');
echo $cipher->decrypt($msg, $key, $iv);
Description: Finalizes a decryption
This method decrypts an outstanding incomplete block if there is any such block in cipher context. It also closes the context so the update cannot be called again unless the object is again initialized. In addition it finishes the authentication for GCM mode.
If the operation fails (e.g. verification fails), then
CipherException
is thrown. The same exception with different code
is thrown if the context has not been initialized for decryption
before.
This method has no parameters.
It can throw CipherException
with code
CipherException::FINISH_FAILED
- finalizing of decryption failedCipherException::FINISH_DECRYPT_FORBIDDEN
- cipher has not been initialized for decryptionCipherException::TAG_VERIFY_FAILED
- tag verification failed (only for GCM or CCM mode)
string
: The decrypted data (plain text) from the last incomplets
block or empty string.
$cipher = new \Crypto\Cipher('AES-128-CTR');
$cipher->decryptInit($key, $iv);
$ct = $cipher->decryptUpdate($msg);
$ct .= $cipher->decryptFinish();
Description: Initializes cipher decryption
This method initializes decryption on the Cipher
object.
It uses a supplied key $key
and an initial vector $iv
. If
the initialization fails, a CipherException
with an appropriate
code is thrown.
The key resp. IV parameters has to contain an exact number of bytes
that is returned by Cipher::getKeyLength
resp. Cipher::getIVLength()
.
If it's not the case, then a CipherException
is thrown.
key : string
- key
iv : string
- initial vector
It can throw CipherException
with code
CipherException::INIT_ALG_FAILED
- initialization of cipher algorithm failedCipherException::INIT_CTX_FAILED
- initialization of cipher context failedCipherException::KEY_LENGTH_INVALID
- the key length is invalidCipherException::IV_LENGTH_INVALID
- the IV length is invalid
null
: Nothing is returned.
$cipher = new \Crypto\Cipher('AES-128-CBC');
try {
$cipher->decryptInit($key, $iv);
} catch (\Crypto\CipherException $ex) {
switch ($ex->getCode()) {
case \Crypto\CipherException::KEY_LENGTH_INVALID:
echo "You need to set a correct key length";
break;
case \Crypto\CipherException::IV_LENGTH_INVALID:
echo "You need to set a correct IV length";
break;
default:
echo $ex->getMessage();
break;
}
}
Description: Updates decryption context with data and returns encrypted blocks.
This method decrypts encrypted data (cipher text) on the Cipher
object.
It updates an initialized context and all encrypted blocks are returned.
If the context is not initialized using Cipher::decryptInit
, then
a CipherException
is thrown.
If the decryption fails, a CipherException
is thrown.
data : string
- cipher text
It can throw CipherException
with code
CipherException::UPDATE_FAILED
- updating of decryption failedCipherException::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds C INT_MAXCipherException::UPDATE_DECRYPT_FORBIDDEN
- cipher has not been initialized for decryptionCipherException::TAG_VERIFY_FAILED
- tag verification failed (only for GCM or CCM mode)
string
: The decrypted plain text.
$cipher = new \Crypto\Cipher('AES-128-CTR');
$cipher->decryptInit($key, $iv);
$plain_text = "";
while (($data = read_data_from_somewhere()) !== false) {
$plain_text .= $cipher->decryptUpdate($msg);
}
$plain_text .= $cipher->decryptFinish();
Description: Encrypts data using key and IV
This method encrypts data (plain text) on the Cipher
object. It uses a supplied key $key
and an initial vector $iv
for encryption. Internally it calls init, update and finish
operations on cipher context. If any of them fails, a CipherException
with an appropriate code is thrown.
The key resp. IV parameters has to contain an exact number of bytes
that is returned by Cipher::getKeyLength
resp. Cipher::getIVLength()
.
If it's not the case, then a CipherException
is thrown.
data : string
- plain text
key : string
- key
iv : string
- initial vector
It can throw CipherException
with code
CipherException::INIT_ALG_FAILED
- initialization of cipher algorithm failedCipherException::INIT_CTX_FAILED
- initialization of cipher context failedCipherException::UPDATE_FAILED
- updating of encryption failedCipherException::FINISH_FAILED
- finalizing of encryption failedCipherException::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds C INT_MAXCipherException::KEY_LENGTH_INVALID
- the key length is invalidCipherException::IV_LENGTH_INVALID
- the IV length is invalid
string
: The encrypted cipher text.
$cipher = new \Crypto\Cipher('AES-128-CTR');
echo $cipher->encrypt($cipher_text, $key, $iv);
Description: Finalizes encryption
This method encrypts an outstanding incomplete block including padding if there is any such block in cipher context and/or padding is required. It also closes the context so the update cannot be called again unless the object is again initialized.
If the operation fails (e.g. verification fails), then
CipherException
is thrown. The same exception with different code
is thrown if the context has not been initialized for decryption
before.
This method has no parameters.
It can throw CipherException
with code
CipherException::FINISH_FAILED
- finalizing of encryption failedCipherException::FINISH_ENCRYPT_FORBIDDEN
- cipher has not been initialized for encryption
string
: The encrypted data (cipher text) from the last incomplete block
with padding or empty string.
$cipher = new \Crypto\Cipher('AES-128-CTR');
$cipher->encryptInit($key, $iv);
$plain_text = $cipher->encryptUpdate($cipher_text);
$plain_text .= $cipher->encryptFinish();
Description: Initializes cipher encryption
This method initializes encryption on the Cipher
object.
It uses a supplied key $key
and an initial vector $iv
. If
the initialization fails, a CipherException
with an appropriate
code is thrown.
The key resp. IV parameters has to contain an exact number of bytes
that is returned by Cipher::getKeyLength
resp. Cipher::getIVLength()
.
If it's not the case, then a CipherException
is thrown.
key : string
- key
iv : string
- initial vector
It can throw CipherException
with code
CipherException::INIT_ALG_FAILED
- initialization of cipher algorithm failedCipherException::INIT_CTX_FAILED
- initialization of cipher context failedCipherException::KEY_LENGTH_INVALID
- the key length is invalidCipherException::IV_LENGTH_INVALID
- the IV length is invalid
null
: Nothing is returned.
$cipher = new \Crypto\Cipher('AES-128-CBC');
try {
$cipher->encryptInit($key, $iv);
} catch (\Crypto\CipherException $ex) {
switch ($ex->getCode()) {
case \Crypto\CipherException::KEY_LENGTH_INVALID:
echo "You need to set a correct key length";
break;
case \Crypto\CipherException::IV_LENGTH_INVALID:
echo "You need to set a correct IV length";
break;
default:
echo $ex->getMessage();
break;
}
}
Description: Updates encryption context with data and returns encrypted blocks.
This method encrypts data (plain text) on the Cipher
object. It updates
an initialized context and all encrypted blocks are returned (if any). If
the context is not initialized using Cipher::encryptInit
, then
a CipherException
is thrown.
If the decryption fails, a CipherException
is thrown.
data : string
- plain text
It can throw CipherException
with code
CipherException::UPDATE_FAILED
- updating of encryption failedCipherException::INPUT_DATA_LENGTH_HIGH
- if the data length exceeds C INT_MAXCipherException::UPDATE_DECRYPT_FORBIDDEN
- cipher has not been initialized for encryption
string
: The encrypted cipher text.
$cipher = new \Crypto\Cipher('AES-128-CTR');
$cipher->decryptInit($key, $iv);
$cipher_text = "";
while (($data = read_data_from_somewhere()) !== false) {
$cipher_text .= $cipher->encryptUpdate($msg);
}
$cipher_text .= $cipher->encryptFinish();
Description: Returns a cipher algorithm name.
It is a getter for internal Cipher::$algorithm
read only property
which is set during the object creation.
This method has no parameters.
This method does not throw any exception.
string
: The name of the cipher algorithm and additional info
like a mode (e.g. AES-128-CTR
)
$cipher = new \Crypto\Cipher('aes-128-ctr');
// this will output AES-128-CTR
echo $cipher->getAlgorithmName();
Description: Returns a cipher block size in bytes.
This method returns a block size of the cipher algorithm.
This method has no parameters.
This method does not throw any exception.
int
: The cipher block size in bytes.
$cipher = new \Crypto\Cipher('aes-128-ctr');
// this will output 16
echo $cipher->getBlockSize();
Description: Returns a cipher IV length in bytes.
This method returns an initial vector length of the cipher algorithm. The IV length depends on the selected mode. This is also applicable on modes that are based on CTR mode which requires a nonce. The nonce length is returned in this case.
This method has no parameters.
This method does not throw any exception.
int
: The cipher IV length in bytes.
$cipher = new \Crypto\Cipher('aes-128-ctr');
// this will output 16
echo $cipher->getIVLength();
Description: Returns a cipher key length in bytes.
This method returns a key length of the cipher algorithm. The key length depends on the cipher where some ciphers support more key lengths (e.g. AES) that can be specified in the algorithm name or as a parameter of the cipher constructor.
This method has no parameters.
This method does not throw any exception.
int
: The cipher key length in bytes.
$cipher = new \Crypto\Cipher('aes-192-ctr');
// this will output 24
echo $cipher->getKeyLength();
Description: Returns a cipher mode constant value.
This method returns a Cipher
class constant value for the used mode.
It's identified from the algorithm name passed to the constructor.
This method has no parameters.
This method does not throw any exception.
int
: The cipher mode constant value.
$cipher = new \Crypto\Cipher('aes-128-ctr');
$mode = $cipher->getMode();
// this will be true
if ($mode === \Crypto\Cipher::MODE_CTR) {
echo "Mode is CTR";
}
Description: Returns an authentication tag.
This method returns a message authentication tag. It can be used
only for modes that supports that (GCM and CCM) and only after
encryption is finished. In any other case, CipherException
is thrown.
The returned tag length can be set by Cipher::setTagLength
before
encryption. If it's not set, then the defualt length is 16.
This method has no parameters.
It can throw CipherException
with code
CipherException::AUTHENTICATION_NOT_SUPPORTED
- mode is not an authenticated modeCipherException::TAG_GETTER_FORBIDDEN
- method is not called after finishing encryptionCipherException::TAG_GETTER_FAILED
- getting tag failed
string
: The authenticated tag.
$cipher = new \Crypto\Cipher('aes-128-gcm');
$cipher_text = $cipher->encrypt($plain_text, $key, $iv);
$tag = $cipher->getTag();
Description: Sets an additional application data.
This method sets an additional application data (AAD). It can be used
only for authenticated modes (GCM and CCM) and only before encryption
or decryption is updated (any data are encrypted or decrypted). In any
other case, a CipherException
is thrown.
aad : string
- additional application data
It can throw CipherException
with code
CipherException::AUTHENTICATION_NOT_SUPPORTED
- mode is not an authenticated modeCipherException::AAD_SETTER_FORBIDDEN
- method is not called before encryption or decryptionCipherException::AAD_LENGTH_HIGH
- if the AAD length exceeds C INT_MAX
bool
: true if the AAD was set succesfully
// encrypt
$cipher = new \Crypto\Cipher('aes-128-gcm');
$cipher->setAAD($aad);
$cipher_text = $cipher->encrypt($plain_text, $key, $iv);
$tag = $cipher->getTag();
// later you have to decrypt with the same AAD
$cipher = new \Crypto\Cipher('aes-128-gcm');
$cipher->setAAD($aad);
$cipher->setTag($tag);
$plain_text = $cipher->decrypt($cipher_text, $key, $iv);
Description: Sets a message authentication tag.
This method sets a message authentication tag. It can be used
only for authenticated modes (GCM and CCM) and only before
decryption is updated (any data are decrypted). In any other
case, a CipherException
is thrown.
The tag length has to be between 4 and 16 bytes, otherwise
a CipherException
is thrown.
tag : string
- message authentication tag
It can throw CipherException
with code
CipherException::AUTHENTICATION_NOT_SUPPORTED
- mode is not an authenticated modeCipherException::TAG_SETTER_FORBIDDEN
- method is not called before decryptionCipherException::TAG_LENGTH_LOW
- if tag length is less than 4 bytesCipherException::TAG_LENGTH_HIGH
- if tag length is more than 16 bytes
bool
: true if the tag was set succesfully
$cipher = new \Crypto\Cipher('aes-128-gcm');
$cipher->setTag($tag);
$plain_text = $cipher->decrypt($cipher_text, $key, $iv);
Description: Sets a message authentication tag length.
This method sets a length for an authentication tag that is
later returned using Cipher::getTag
. It can be used
only for authenticated modes (GCM and CCM) and only before
encryption is updated (any data are encrypted). In any other
case, a CipherException
is thrown.
The tag length has to be between 4 and 16 bytes, otherwise
a CipherException
is thrown.
The method is useful only if there is a requirement of different tag length than the default which is 16 bytes. The tag is just trimmed for GCM mode. However it's a completely different tag if it's used with CCM mode.
length : int
- message authentication tag length
It can throw CipherException
with code
CipherException::AUTHENTICATION_NOT_SUPPORTED
- mode is not an authenticated modeCipherException::TAG_SETTER_FORBIDDEN
- method is not called before encryptionCipherException::TAG_LENGTH_LOW
- if tag length is less than 4 bytesCipherException::TAG_LENGTH_HIGH
- if tag length is more than 16 bytes
bool
: true if the tag length was set succesfully
$cipher = new \Crypto\Cipher('aes-128-ccm');
$cipher->setTagLength(12);
$cipher_text = $cipher->encrypt($plain_text, $key, $nonce);
// tag with lenth 12 bytes (characters)
$tag = $cipher->getTag();