PageRenderTime 140ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/InfoCard/Cipher/Symmetric/Adapter/Aes256cbc.php

https://bitbucket.org/gkawka/zend-framework
PHP | 111 lines | 36 code | 14 blank | 61 comment | 3 complexity | 1b1e535945c0b1b5b5da87d860276901 MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * Zend_InfoCard_Cipher_Symmetric_Adapter_Abstract
  24. */
  25. require_once 'Zend/InfoCard/Cipher/Symmetric/Adapter/Abstract.php';
  26. /**
  27. * Zend_InfoCard_Cipher_Symmetric_Aes256cbc_Interface
  28. */
  29. require_once 'Zend/InfoCard/Cipher/Symmetric/Aes256cbc/Interface.php';
  30. /**
  31. * Implements AES256 with CBC encryption implemented using the mCrypt extension
  32. *
  33. * @category Zend
  34. * @package Zend_InfoCard
  35. * @subpackage Zend_InfoCard_Cipher
  36. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_InfoCard_Cipher_Symmetric_Adapter_Aes256cbc
  40. extends Zend_InfoCard_Cipher_Symmetric_Adapter_Abstract
  41. implements Zend_InfoCard_Cipher_Symmetric_Aes256cbc_Interface
  42. {
  43. /**
  44. * The MCRYPT Cipher constant for this encryption
  45. */
  46. const MCRYPT_CIPHER = MCRYPT_RIJNDAEL_128;
  47. /**
  48. * The MCRYPT Mode constant for this encryption
  49. */
  50. const MCRYPT_MODE = MCRYPT_MODE_CBC;
  51. /**
  52. * The default length of the IV to use
  53. */
  54. const IV_LENGTH = 16;
  55. /**
  56. * The object constructor
  57. *
  58. * @throws Zend_InfoCard_Cipher_Exception
  59. */
  60. public function __construct()
  61. {
  62. // Can't test for this
  63. // @codeCoverageIgnoreStart
  64. if(!extension_loaded('mcrypt')) {
  65. require_once 'Zend/InfoCard/Cipher/Exception.php';
  66. throw new Zend_InfoCard_Cipher_Exception("Use of the AES256CBC Cipher requires the mcrypt extension");
  67. }
  68. // @codeCoveregIgnoreEnd
  69. }
  70. /**
  71. * Decrypts data using the AES Algorithm using the mCrypt extension
  72. *
  73. * @throws Zend_InfoCard_Cipher_Exception
  74. * @param string $encryptedData The encrypted data in binary format
  75. * @param string $decryptionKey The decryption key
  76. * @param integer $iv_length The IV length to use
  77. * @return string the decrypted data with any terminating nulls removed
  78. */
  79. public function decrypt($encryptedData, $decryptionKey, $iv_length = null)
  80. {
  81. $iv_length = ($iv_length === null) ? self::IV_LENGTH : $iv_length;
  82. $mcrypt_iv = null;
  83. if($iv_length > 0) {
  84. $mcrypt_iv = substr($encryptedData, 0, $iv_length);
  85. $encryptedData = substr($encryptedData, $iv_length);
  86. }
  87. $decrypted = mcrypt_decrypt(self::MCRYPT_CIPHER, $decryptionKey, $encryptedData, self::MCRYPT_MODE, $mcrypt_iv);
  88. if(!$decrypted) {
  89. require_once 'Zend/InfoCard/Cipher/Exception.php';
  90. throw new Zend_InfoCard_Cipher_Exception("Failed to decrypt data using AES256CBC Algorithm");
  91. }
  92. $decryptedLength = strlen($decrypted);
  93. $paddingLength = substr($decrypted, $decryptedLength -1, 1);
  94. $decrypted = substr($decrypted, 0, $decryptedLength - ord($paddingLength));
  95. return rtrim($decrypted, "\0");
  96. }
  97. }