Commit | Line | Data |
---|---|---|
14d4f286 S |
1 | <?php |
2 | /** | |
3 | * An implementation of the TripleDES cipher | |
4 | * | |
5 | * This was forked from phpseclib and modified to use CryptLib conventions | |
6 | * | |
7 | * PHP version 5.3 | |
8 | * | |
9 | * @category PHPCryptLib | |
10 | * @package Cipher | |
11 | * @subpackage Block | |
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@@ | |
17 | */ | |
18 | ||
19 | namespace CryptLib\Cipher\Block\Cipher; | |
20 | ||
21 | /** | |
22 | * An implementation of the TripleDES Cipher | |
23 | * | |
24 | * @category PHPCryptLib | |
25 | * @package Cipher | |
26 | * @subpackage Block | |
27 | * @author Anthony Ferrara <ircmaxell@ircmaxell.com> | |
28 | */ | |
29 | class TripleDES extends DES { | |
30 | ||
31 | /** | |
32 | * @var int The key size for the cipher | |
33 | */ | |
34 | protected $keySize = 24; | |
35 | ||
36 | /** | |
37 | * Get a list of supported ciphers for this class implementation | |
38 | * | |
39 | * @return array A list of supported ciphers | |
40 | */ | |
41 | public static function getSupportedCiphers() { | |
42 | return array('tripledes'); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Set the key to use for the cipher | |
47 | * | |
48 | * @param string $key The key to use | |
49 | * | |
50 | * @throws InvalidArgumentException If the key is not the correct size | |
51 | * @return void | |
52 | */ | |
53 | public function setKey($key) { | |
54 | $len = strlen($key); | |
55 | if ($len == 16) { | |
56 | $key .= substr($key, 0, 8); | |
57 | } elseif ($len != 24) { | |
58 | throw new \InvalidArgumentException( | |
59 | 'The supplied key block is not the correct size' | |
60 | ); | |
61 | } | |
62 | $this->key = $key; | |
63 | $this->initialized = true; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Decrypt a block of data using the supplied string key | |
68 | * | |
69 | * Note that the supplied data should be the same size as the block size of | |
70 | * the cipher being used. | |
71 | * | |
72 | * @param string $data The data to decrypt | |
73 | * | |
74 | * @return string The result decrypted data | |
75 | */ | |
76 | protected function decryptBlockData($data) { | |
77 | $key = $this->key; | |
78 | $this->key = substr($key, 16, 8); | |
79 | $this->initialize(); | |
80 | $data = parent::decryptBlockData($data); | |
81 | $this->key = substr($key, 8, 8); | |
82 | $this->initialize(); | |
83 | $data = parent::encryptBlockData($data); | |
84 | $this->key = substr($key, 0, 8); | |
85 | $this->initialize(); | |
86 | $data = parent::decryptBlockData($data); | |
87 | $this->key = $key; | |
88 | return $data; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Encrypt a block of data using the supplied string key | |
93 | * | |
94 | * Note that the supplied data should be the same size as the block size of | |
95 | * the cipher being used. | |
96 | * | |
97 | * @param string $data The data to encrypt | |
98 | * | |
99 | * @return string The result encrypted data | |
100 | */ | |
101 | protected function encryptBlockData($data) { | |
102 | $key = $this->key; | |
103 | $this->key = substr($key, 0, 8); | |
104 | $this->initialize(); | |
105 | $data = parent::encryptBlockData($data); | |
106 | $this->key = substr($key, 8, 8); | |
107 | $this->initialize(); | |
108 | $data = parent::decryptBlockData($data); | |
109 | $this->key = substr($key, 16, 8); | |
110 | $this->initialize(); | |
111 | $data = parent::encryptBlockData($data); | |
112 | $this->key = $key; | |
113 | return $data; | |
114 | } | |
115 | ||
116 | } |