PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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