3 * An implementation of the TripleDES cipher
5 * This was forked from phpseclib and modified to use CryptLib conventions
9 * @category PHPCryptLib
12 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
13 * @author Jim Wigginton <terrafrost@php.net>
14 * @copyright 2011 The Authors
15 * @license http://www.opensource.org/licenses/mit-license.html MIT License
16 * @version Build @@version@@
19 namespace CryptLib\Cipher\Block\Cipher
;
22 * An implementation of the TripleDES Cipher
24 * @category PHPCryptLib
27 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
29 class TripleDES
extends DES
{
32 * @var int The key size for the cipher
34 protected $keySize = 24;
37 * Get a list of supported ciphers for this class implementation
39 * @return array A list of supported ciphers
41 public static function getSupportedCiphers() {
42 return array('tripledes');
46 * Set the key to use for the cipher
48 * @param string $key The key to use
50 * @throws InvalidArgumentException If the key is not the correct size
53 public function setKey($key) {
56 $key .= substr($key, 0, 8);
57 } elseif ($len != 24) {
58 throw new \
InvalidArgumentException(
59 'The supplied key block is not the correct size'
63 $this->initialized
= true;
67 * Decrypt a block of data using the supplied string key
69 * Note that the supplied data should be the same size as the block size of
70 * the cipher being used.
72 * @param string $data The data to decrypt
74 * @return string The result decrypted data
76 protected function decryptBlockData($data) {
78 $this->key
= substr($key, 16, 8);
80 $data = parent
::decryptBlockData($data);
81 $this->key
= substr($key, 8, 8);
83 $data = parent
::encryptBlockData($data);
84 $this->key
= substr($key, 0, 8);
86 $data = parent
::decryptBlockData($data);
92 * Encrypt a block of data using the supplied string key
94 * Note that the supplied data should be the same size as the block size of
95 * the cipher being used.
97 * @param string $data The data to encrypt
99 * @return string The result encrypted data
101 protected function encryptBlockData($data) {
103 $this->key
= substr($key, 0, 8);
105 $data = parent
::encryptBlockData($data);
106 $this->key
= substr($key, 8, 8);
108 $data = parent
::decryptBlockData($data);
109 $this->key
= substr($key, 16, 8);
111 $data = parent
::encryptBlockData($data);