|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_InfoCard |
|
17 * @subpackage Zend_InfoCard_Cipher |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Aes256cbc.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * Zend_InfoCard_Cipher_Symmetric_Adapter_Abstract |
|
25 */ |
|
26 require_once 'Zend/InfoCard/Cipher/Symmetric/Adapter/Abstract.php'; |
|
27 |
|
28 /** |
|
29 * Zend_InfoCard_Cipher_Symmetric_Aes256cbc_Interface |
|
30 */ |
|
31 require_once 'Zend/InfoCard/Cipher/Symmetric/Aes256cbc/Interface.php'; |
|
32 |
|
33 /** |
|
34 * Implements AES256 with CBC encryption implemented using the mCrypt extension |
|
35 * |
|
36 * @category Zend |
|
37 * @package Zend_InfoCard |
|
38 * @subpackage Zend_InfoCard_Cipher |
|
39 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
40 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
41 */ |
|
42 class Zend_InfoCard_Cipher_Symmetric_Adapter_Aes256cbc |
|
43 extends Zend_InfoCard_Cipher_Symmetric_Adapter_Abstract |
|
44 implements Zend_InfoCard_Cipher_Symmetric_Aes256cbc_Interface |
|
45 { |
|
46 /** |
|
47 * The MCRYPT Cipher constant for this encryption |
|
48 */ |
|
49 const MCRYPT_CIPHER = MCRYPT_RIJNDAEL_128; |
|
50 |
|
51 /** |
|
52 * The MCRYPT Mode constant for this encryption |
|
53 */ |
|
54 const MCRYPT_MODE = MCRYPT_MODE_CBC; |
|
55 |
|
56 /** |
|
57 * The default length of the IV to use |
|
58 */ |
|
59 const IV_LENGTH = 16; |
|
60 |
|
61 /** |
|
62 * The object constructor |
|
63 * |
|
64 * @throws Zend_InfoCard_Cipher_Exception |
|
65 */ |
|
66 public function __construct() |
|
67 { |
|
68 // Can't test for this |
|
69 // @codeCoverageIgnoreStart |
|
70 if(!extension_loaded('mcrypt')) { |
|
71 require_once 'Zend/InfoCard/Cipher/Exception.php'; |
|
72 throw new Zend_InfoCard_Cipher_Exception("Use of the AES256CBC Cipher requires the mcrypt extension"); |
|
73 } |
|
74 // @codeCoveregIgnoreEnd |
|
75 } |
|
76 |
|
77 /** |
|
78 * Decrypts data using the AES Algorithm using the mCrypt extension |
|
79 * |
|
80 * @throws Zend_InfoCard_Cipher_Exception |
|
81 * @param string $encryptedData The encrypted data in binary format |
|
82 * @param string $decryptionKey The decryption key |
|
83 * @param integer $iv_length The IV length to use |
|
84 * @return string the decrypted data with any terminating nulls removed |
|
85 */ |
|
86 public function decrypt($encryptedData, $decryptionKey, $iv_length = null) |
|
87 { |
|
88 |
|
89 $iv_length = ($iv_length === null) ? self::IV_LENGTH : $iv_length; |
|
90 |
|
91 $mcrypt_iv = null; |
|
92 |
|
93 if($iv_length > 0) { |
|
94 $mcrypt_iv = substr($encryptedData, 0, $iv_length); |
|
95 $encryptedData = substr($encryptedData, $iv_length); |
|
96 } |
|
97 |
|
98 $decrypted = mcrypt_decrypt(self::MCRYPT_CIPHER, $decryptionKey, $encryptedData, self::MCRYPT_MODE, $mcrypt_iv); |
|
99 |
|
100 if(!$decrypted) { |
|
101 require_once 'Zend/InfoCard/Cipher/Exception.php'; |
|
102 throw new Zend_InfoCard_Cipher_Exception("Failed to decrypt data using AES256CBC Algorithm"); |
|
103 } |
|
104 |
|
105 $decryptedLength = strlen($decrypted); |
|
106 $paddingLength = substr($decrypted, $decryptedLength -1, 1); |
|
107 $decrypted = substr($decrypted, 0, $decryptedLength - ord($paddingLength)); |
|
108 |
|
109 return rtrim($decrypted, "\0"); |
|
110 } |
|
111 } |