-
Notifications
You must be signed in to change notification settings - Fork 2
BlockCipher
The block cipher is one of the two major categories of symmetric ciphers. It operates on fixed-length groups of bits, called blocks. It means that if you want to encrypt 64 bytes of data, and the block size is 128 bits (16 bytes), data will be splitted in 4 blocks of 16 bytes and each block will be encrypted separately. A block cipher is usually used with a cipher mode of operation.
A cipher is said "symmetric" when it uses only one key : the same key is used to encrypt and decrypt data.
Block cipher classes implements the Cryptopp\BlockCipherInterface interface.
$o = new Cryptopp\BlockCipherAes();
// returns the name of the block cipher
$o->getName();
// returns the block size
$o->getBlockSize();
// indicates if a given key length is valid
$o->isValidKeyLength(10);A block cipher should not be used as is. Instead, use a cipher mode of operation.
To encrypt/decrypt data, use the encrypt() and decrypt() methods. Data length have to be a multiple of the cipher block size.
$o = new Cryptopp\BlockCipherAes();
$o->setKey("your secret key");
$ciphertext = $o->encrypt("this string length has to be a multiple of the cipher block size");
$plaintext = $o->decrypt($ciphertext);To encrypt/decrypt a single block, use the encryptBlock() and decryptBlock() methods :
$o = new Cryptopp\BlockCipherAes();
$o->setKey("your secret key");
$ciphertext = $o->encrypt("this string length has to match exactly the cipher block size");
$plaintext = $o->decrypt($ciphertext);Extending a block cipher class is possible to add some methods, but existing methods are not overwritable. Also, you are required to call the parent constructor.
class MyAes extends Cryptopp\BlockCipherAes
{
public function __construct()
{
parent::__construct();
}
public function myMethod()
{
// ...
}
}
$o = new MyAes();You can create your own block cipher class by implementing the Cryptopp\BlockCipherInterface interface. The following methods are required :
| Method signature | Descritption |
|---|---|
getName() |
Returns the cipher name |
getBlockSize() |
Returns the block size (in bytes) |
encrypt($data) |
Encrypts data |
decrypt($data) |
Decrypts data |
encryptBlock($block) |
Encrypts a single data block |
decryptBlock($block) |
Decrypts a single data block |
setKey($key) |
Sets the key |
getKey() |
Returns the key |
isValidKeyLength($length) |
Indicates if a given key length is valid for this algorithm |
The Advanced Encryption Standard (AES) is based on the Rijndael cipher developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen. This is the recommended block cipher.
-
Class :
Cryptopp\BlockCipherAes - Block size : 128 bits (16 bytes)
- Key size : 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes)