3 * An abstract class for simplifing creation of ciphers
7 * @category PHPCryptLib
10 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
11 * @copyright 2011 The Authors
12 * @license http://www.opensource.org/licenses/mit-license.html MIT License
13 * @version Build @@version@@
16 namespace CryptLib\Cipher\Block
;
19 * An abstract class for simplifing creation of ciphers
21 * @category PHPCryptLib
25 abstract class AbstractCipher
implements \CryptLib\Cipher\Block\Cipher
{
28 * @var int The block size for the cipher
30 protected $blockSize = 0;
33 * @var string The cipher name for the current instance
35 protected $cipher = '';
38 * @var boolean Is the cipher ready to encrypt/decrypt
40 protected $initialized = false;
43 * @var string The key to use for encryption/decryption
48 * @var int The size of the key to use
50 protected $keySize = 0;
53 * Decrypt a block of data
55 * @param string $data The ciphertext to decrypt
57 * @return string The decrypted data
59 abstract protected function decryptBlockData($data);
62 * Encrypt a block of data
64 * @param string $data The plaintext to encrypt
66 * @return string The encrypted cipher text
68 abstract protected function encryptBlockData($data);
71 * Construct the instance for the supplied cipher name
73 * @param string $cipher The cipher to implement
76 * @throws InvalidArgumentException if the cipher is not supported
78 public function __construct($cipher) {
79 $ciphers = static::getSupportedCiphers();
80 if (in_array($cipher, $ciphers)) {
81 $this->cipher
= $cipher;
83 throw new \
InvalidArgumentException('Unsupported Cipher Supplied');
88 * Decrypt a block of data using the supplied string key.
90 * Note that the supplied data should be the same size as the block size of
91 * the cipher being used.
93 * @param string $data The data to decrypt
95 * @return string The result decrypted data
96 * @throws InvalidArgumentException If the data size is not the block size
97 * @throws RuntimeException If the cipher is not initialized
99 public function decryptBlock($data) {
100 $this->enforceInitializedCipher();
101 $this->enforceProperBlockSize($data);
102 return $this->decryptBlockData($data);
106 * Encrypt a block of data using the supplied string key.
108 * Note that the supplied data should be the same size as the block size of
109 * the cipher being used.
111 * @param string $data The data to encrypt
113 * @return string The result encrypted data
114 * @throws InvalidArgumentException If the data size is not the block size
115 * @throws RuntimeException If the cipher is not initialized
117 public function encryptBlock($data) {
118 $this->enforceInitializedCipher();
119 $this->enforceProperBlockSize($data);
120 return $this->encryptBlockData($data);
124 * Get the block size for the current initialized cipher
126 * @param string $key The key the data will be encrypted with
128 * @return int The block size for the current cipher
130 public function getBlockSize() {
131 return $this->blockSize
;
135 * Get the string name of the current cipher instance
137 * @return string The current instantiated cipher
139 public function getCipher() {
140 return $this->cipher
;
144 * Get the key size for the current initialized cipher
146 * @return int The key size for the current cipher
148 public function getKeySize() {
149 return $this->keySize
;
153 * Set the key to use for the cipher
155 * @param string $key The key to use
157 * @throws InvalidArgumentException If the key is not the correct size
160 public function setKey($key) {
161 if (strlen($key) != $this->getKeySize()) {
162 throw new \
InvalidArgumentException(
164 'The supplied key block is not the correct size [%d:%d]',
171 $this->initialized
= $this->initialize();
175 * Check to see if the cipher is initialized
178 * @throws RuntimeException If the cipher is not initialized
180 protected function enforceInitializedCipher() {
181 if (!$this->initialized
) {
182 throw new \
RuntimeException(
183 'The cipher has not been properly initialized'
189 * Check to see if the data is of the correct block size
191 * @param string $data The data block to check
194 * @throws InvalidArgumentException if the data is not the correct size
196 protected function enforceProperBlockSize($data) {
197 if (strlen($data) != $this->getBlockSize()) {
198 throw new \
InvalidArgumentException(
200 'The supplied data block is not the correct size [%d:%d]',
202 $this->getBlockSize()
209 * Initialize the function after the key is set
211 * @return boolean The status of the initialization
213 protected function initialize() {