PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_akeeba/akeeba/utils/encrypt.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 456 lines | 277 code | 59 blank | 120 comment | 64 complexity | ba07cca214260c9474b4cc817a93fdcb MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package AkeebaBackup
  4. * @copyright Copyright (c)2009-2012 Nicholas K. Dionysopoulos
  5. * @license GNU General Public License version 3, or later
  6. * @version $Id: encrypt.php 422 2011-01-31 09:39:33Z nikosdion $
  7. * @since 3.0
  8. */
  9. // Protection against direct access
  10. defined('AKEEBAENGINE') or die('Restricted access');
  11. /**
  12. * AES implementation in PHP (c) Chris Veness 2005-2011.
  13. * Right to use and adapt is granted for under a simple creative commons attribution
  14. * licence. No warranty of any form is offered.
  15. *
  16. * Modified for Akeeba Backup by Nicholas K. Dionysopoulos
  17. */
  18. class AEUtilEncrypt
  19. {
  20. // Sbox is pre-computed multiplicative inverse in GF(2^8) used in SubBytes and KeyExpansion [�5.1.1]
  21. protected static $Sbox =
  22. array(0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
  23. 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
  24. 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  25. 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
  26. 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  27. 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  28. 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
  29. 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
  30. 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
  31. 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  32. 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
  33. 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
  34. 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
  35. 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
  36. 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  37. 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16);
  38. // Rcon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [�5.2]
  39. protected static $Rcon = array(
  40. array(0x00, 0x00, 0x00, 0x00),
  41. array(0x01, 0x00, 0x00, 0x00),
  42. array(0x02, 0x00, 0x00, 0x00),
  43. array(0x04, 0x00, 0x00, 0x00),
  44. array(0x08, 0x00, 0x00, 0x00),
  45. array(0x10, 0x00, 0x00, 0x00),
  46. array(0x20, 0x00, 0x00, 0x00),
  47. array(0x40, 0x00, 0x00, 0x00),
  48. array(0x80, 0x00, 0x00, 0x00),
  49. array(0x1b, 0x00, 0x00, 0x00),
  50. array(0x36, 0x00, 0x00, 0x00) );
  51. protected static $passwords = array();
  52. /**
  53. * AES Cipher function: encrypt 'input' with Rijndael algorithm
  54. *
  55. * @param input message as byte-array (16 bytes)
  56. * @param w key schedule as 2D byte-array (Nr+1 x Nb bytes) -
  57. * generated from the cipher key by KeyExpansion()
  58. * @return ciphertext as byte-array (16 bytes)
  59. */
  60. public static function Cipher($input, $w) { // main Cipher function [�5.1]
  61. $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES)
  62. $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys
  63. $state = array(); // initialise 4xNb byte-array 'state' with input [�3.4]
  64. for ($i=0; $i<4*$Nb; $i++) $state[$i%4][floor($i/4)] = $input[$i];
  65. $state = self::AddRoundKey($state, $w, 0, $Nb);
  66. for ($round=1; $round<$Nr; $round++) { // apply Nr rounds
  67. $state = self::SubBytes($state, $Nb);
  68. $state = self::ShiftRows($state, $Nb);
  69. $state = self::MixColumns($state, $Nb);
  70. $state = self::AddRoundKey($state, $w, $round, $Nb);
  71. }
  72. $state = self::SubBytes($state, $Nb);
  73. $state = self::ShiftRows($state, $Nb);
  74. $state = self::AddRoundKey($state, $w, $Nr, $Nb);
  75. $output = array(4*$Nb); // convert state to 1-d array before returning [�3.4]
  76. for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)];
  77. return $output;
  78. }
  79. protected static function AddRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [�5.1.4]
  80. for ($r=0; $r<4; $r++) {
  81. for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r];
  82. }
  83. return $state;
  84. }
  85. protected static function SubBytes($s, $Nb) { // apply SBox to state S [�5.1.1]
  86. for ($r=0; $r<4; $r++) {
  87. for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$Sbox[$s[$r][$c]];
  88. }
  89. return $s;
  90. }
  91. protected static function ShiftRows($s, $Nb) { // shift row r of state S left by r bytes [�5.1.2]
  92. $t = array(4);
  93. for ($r=1; $r<4; $r++) {
  94. for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy
  95. for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c]; // and copy back
  96. } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES):
  97. return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf
  98. }
  99. protected static function MixColumns($s, $Nb) { // combine bytes of each col of state S [�5.1.3]
  100. for ($c=0; $c<4; $c++) {
  101. $a = array(4); // 'a' is a copy of the current column from 's'
  102. $b = array(4); // 'b' is a�{02} in GF(2^8)
  103. for ($i=0; $i<4; $i++) {
  104. $a[$i] = $s[$i][$c];
  105. $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1;
  106. }
  107. // a[n] ^ b[n] is a�{03} in GF(2^8)
  108. $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3
  109. $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3
  110. $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3
  111. $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3
  112. }
  113. return $s;
  114. }
  115. /**
  116. * Key expansion for Rijndael Cipher(): performs key expansion on cipher key
  117. * to generate a key schedule
  118. *
  119. * @param key cipher key byte-array (16 bytes)
  120. * @return key schedule as 2D byte-array (Nr+1 x Nb bytes)
  121. */
  122. public static function KeyExpansion($key) { // generate Key Schedule from Cipher Key [�5.2]
  123. $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES)
  124. $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys
  125. $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys
  126. $w = array();
  127. $temp = array();
  128. for ($i=0; $i<$Nk; $i++) {
  129. $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]);
  130. $w[$i] = $r;
  131. }
  132. for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) {
  133. $w[$i] = array();
  134. for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t];
  135. if ($i % $Nk == 0) {
  136. $temp = self::SubWord(self::RotWord($temp));
  137. for ($t=0; $t<4; $t++) $temp[$t] ^= self::$Rcon[$i/$Nk][$t];
  138. } else if ($Nk > 6 && $i%$Nk == 4) {
  139. $temp = self::SubWord($temp);
  140. }
  141. for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t];
  142. }
  143. return $w;
  144. }
  145. protected static function SubWord($w) { // apply SBox to 4-byte word w
  146. for ($i=0; $i<4; $i++) $w[$i] = self::$Sbox[$w[$i]];
  147. return $w;
  148. }
  149. protected static function RotWord($w) { // rotate 4-byte word w left by one byte
  150. $tmp = $w[0];
  151. for ($i=0; $i<3; $i++) $w[$i] = $w[$i+1];
  152. $w[3] = $tmp;
  153. return $w;
  154. }
  155. /*
  156. * Unsigned right shift function, since PHP has neither >>> operator nor unsigned ints
  157. *
  158. * @param a number to be shifted (32-bit integer)
  159. * @param b number of bits to shift a to the right (0..31)
  160. * @return a right-shifted and zero-filled by b bits
  161. */
  162. protected static function urs($a, $b) {
  163. $a &= 0xffffffff; $b &= 0x1f; // (bounds check)
  164. if ($a&0x80000000 && $b>0) { // if left-most bit set
  165. $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit
  166. $a = $a >> ($b-1); // remaining right-shifts
  167. } else { // otherwise
  168. $a = ($a>>$b); // use normal right-shift
  169. }
  170. return $a;
  171. }
  172. /**
  173. * Encrypt a text using AES encryption in Counter mode of operation
  174. * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  175. *
  176. * Unicode multi-byte character safe
  177. *
  178. * @param plaintext source text to be encrypted
  179. * @param password the password to use to generate a key
  180. * @param nBits number of bits to be used in the key (128, 192, or 256)
  181. * @return encrypted text
  182. */
  183. public static function AESEncryptCtr($plaintext, $password, $nBits) {
  184. $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES
  185. if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys
  186. // note PHP (5) gives us plaintext and password in UTF8 encoding!
  187. // use AES itself to encrypt password to get cipher key (using plain password as source for
  188. // key expansion) - gives us well encrypted key
  189. $nBytes = $nBits/8; // no bytes in key
  190. $pwBytes = array();
  191. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  192. $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  193. $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long
  194. // initialise counter block (NIST SP800-38A �B.2): millisecond time-stamp for nonce in
  195. // 1st 8 bytes, block counter in 2nd 8 bytes
  196. $counterBlock = array();
  197. $nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970
  198. $nonceSec = floor($nonce/1000);
  199. $nonceMs = $nonce%1000;
  200. // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes
  201. for ($i=0; $i<4; $i++) $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff;
  202. for ($i=0; $i<4; $i++) $counterBlock[$i+4] = $nonceMs & 0xff;
  203. // and convert it to a string to go on the front of the ciphertext
  204. $ctrTxt = '';
  205. for ($i=0; $i<8; $i++) $ctrTxt .= chr($counterBlock[$i]);
  206. // generate key schedule - an expansion of the key into distinct Key Rounds for each round
  207. $keySchedule = self::KeyExpansion($key);
  208. $blockCount = ceil(strlen($plaintext)/$blockSize);
  209. $ciphertxt = array(); // ciphertext as array of strings
  210. for ($b=0; $b<$blockCount; $b++) {
  211. // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes)
  212. // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB)
  213. for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff;
  214. for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8);
  215. $cipherCntr = self::Cipher($counterBlock, $keySchedule); // -- encrypt counter block --
  216. // block size is reduced on final block
  217. $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1;
  218. $cipherByte = array();
  219. for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte --
  220. $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1));
  221. $cipherByte[$i] = chr($cipherByte[$i]);
  222. }
  223. $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext
  224. }
  225. // implode is more efficient than repeated string concatenation
  226. $ciphertext = $ctrTxt . implode('', $ciphertxt);
  227. $ciphertext = base64_encode($ciphertext);
  228. return $ciphertext;
  229. }
  230. /**
  231. * Decrypt a text encrypted by AES in counter mode of operation
  232. *
  233. * @param ciphertext source text to be decrypted
  234. * @param password the password to use to generate a key
  235. * @param nBits number of bits to be used in the key (128, 192, or 256)
  236. * @return decrypted text
  237. */
  238. public static function AESDecryptCtr($ciphertext, $password, $nBits) {
  239. $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES
  240. if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // standard allows 128/192/256 bit keys
  241. $ciphertext = base64_decode($ciphertext);
  242. // use AES to encrypt password (mirroring encrypt routine)
  243. $nBytes = $nBits/8; // no bytes in key
  244. $pwBytes = array();
  245. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  246. $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  247. $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long
  248. // recover nonce from 1st element of ciphertext
  249. $counterBlock = array();
  250. $ctrTxt = substr($ciphertext, 0, 8);
  251. for ($i=0; $i<8; $i++) $counterBlock[$i] = ord(substr($ctrTxt,$i,1));
  252. // generate key schedule
  253. $keySchedule = self::KeyExpansion($key);
  254. // separate ciphertext into blocks (skipping past initial 8 bytes)
  255. $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize);
  256. $ct = array();
  257. for ($b=0; $b<$nBlocks; $b++) $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16);
  258. $ciphertext = $ct; // ciphertext is now array of block-length strings
  259. // plaintext will get generated block-by-block into array of block-length strings
  260. $plaintxt = array();
  261. for ($b=0; $b<$nBlocks; $b++) {
  262. // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes)
  263. for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff;
  264. for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff;
  265. $cipherCntr = self::Cipher($counterBlock, $keySchedule); // encrypt counter block
  266. $plaintxtByte = array();
  267. for ($i=0; $i<strlen($ciphertext[$b]); $i++) {
  268. // -- xor plaintext with ciphered counter byte-by-byte --
  269. $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1));
  270. $plaintxtByte[$i] = chr($plaintxtByte[$i]);
  271. }
  272. $plaintxt[$b] = implode('', $plaintxtByte);
  273. }
  274. // join array of blocks into single plaintext string
  275. $plaintext = implode('',$plaintxt);
  276. return $plaintext;
  277. }
  278. /**
  279. * AES encryption in CBC mode. This is the standard mode (the CTR methods
  280. * actually use Rijndael-128 in CTR mode, which - technically - isn't AES).
  281. * The data length is tucked as a 32-bit unsigned integer (little endian)
  282. * after the ciphertext. It supports AES-128, AES-192 and AES-256.
  283. *
  284. * @since 3.0.1
  285. * @author Nicholas K. Dionysopoulos
  286. *
  287. * @param string $plaintext The data to encrypt
  288. * @param string $password Encryption password
  289. * @param int $nBits Encryption key size. Can be 128, 192 or 256
  290. * @return string The ciphertext
  291. */
  292. public static function AESEncryptCBC($plaintext, $password, $nBits = 128)
  293. {
  294. if (!($nBits==128 || $nBits==192 || $nBits==256)) return false; // standard allows 128/192/256 bit keys
  295. if(!function_exists('mcrypt_module_open')) return false;
  296. // Try to fetch cached key/iv or create them if they do not exist
  297. $lookupKey = $password.'-'.$nBits;
  298. if(array_key_exists($lookupKey, self::$passwords))
  299. {
  300. $key = self::$passwords[$lookupKey]['key'];
  301. $iv = self::$passwords[$lookupKey]['iv'];
  302. }
  303. else
  304. {
  305. // use AES itself to encrypt password to get cipher key (using plain password as source for
  306. // key expansion) - gives us well encrypted key
  307. $nBytes = $nBits/8; // no bytes in key
  308. $pwBytes = array();
  309. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  310. $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  311. $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long
  312. $newKey = '';
  313. foreach($key as $int) { $newKey .= chr($int); }
  314. $key = $newKey;
  315. // Create an Initialization Vector (IV) based on the password, using the same technique as for the key
  316. $nBytes = 16; // AES uses a 128 -bit (16 byte) block size, hence the IV size is always 16 bytes
  317. $pwBytes = array();
  318. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  319. $iv = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  320. $newIV = '';
  321. foreach($iv as $int) { $newIV .= chr($int); }
  322. $iv = $newIV;
  323. self::$passwords[$lookupKey]['key'] = $key;
  324. self::$passwords[$lookupKey]['iv'] = $iv;
  325. }
  326. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  327. mcrypt_generic_init($td, $key, $iv);
  328. $ciphertext = mcrypt_generic($td, $plaintext);
  329. mcrypt_generic_deinit($td);
  330. $ciphertext .= pack('V', strlen($plaintext));
  331. return $ciphertext;
  332. }
  333. /**
  334. * AES decryption in CBC mode. This is the standard mode (the CTR methods
  335. * actually use Rijndael-128 in CTR mode, which - technically - isn't AES).
  336. *
  337. * Supports AES-128, AES-192 and AES-256. It supposes that the last 4 bytes
  338. * contained a little-endian unsigned long integer representing the unpadded
  339. * data length.
  340. *
  341. * @since 3.0.1
  342. * @author Nicholas K. Dionysopoulos
  343. *
  344. * @param string $ciphertext The data to encrypt
  345. * @param string $password Encryption password
  346. * @param int $nBits Encryption key size. Can be 128, 192 or 256
  347. * @return string The plaintext
  348. */
  349. public static function AESDecryptCBC($ciphertext, $password, $nBits = 128)
  350. {
  351. if (!($nBits==128 || $nBits==192 || $nBits==256)) return false; // standard allows 128/192/256 bit keys
  352. if(!function_exists('mcrypt_module_open')) return false;
  353. // Try to fetch cached key/iv or create them if they do not exist
  354. $lookupKey = $password.'-'.$nBits;
  355. if(array_key_exists($lookupKey, self::$passwords))
  356. {
  357. $key = self::$passwords[$lookupKey]['key'];
  358. $iv = self::$passwords[$lookupKey]['iv'];
  359. }
  360. else
  361. {
  362. // use AES itself to encrypt password to get cipher key (using plain password as source for
  363. // key expansion) - gives us well encrypted key
  364. $nBytes = $nBits/8; // no bytes in key
  365. $pwBytes = array();
  366. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  367. $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  368. $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long
  369. $newKey = '';
  370. foreach($key as $int) { $newKey .= chr($int); }
  371. $key = $newKey;
  372. // Create an Initialization Vector (IV) based on the password, using the same technique as for the key
  373. $nBytes = 16; // AES uses a 128 -bit (16 byte) block size, hence the IV size is always 16 bytes
  374. $pwBytes = array();
  375. for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff;
  376. $iv = self::Cipher($pwBytes, self::KeyExpansion($pwBytes));
  377. $newIV = '';
  378. foreach($iv as $int) { $newIV .= chr($int); }
  379. $iv = $newIV;
  380. self::$passwords[$lookupKey]['key'] = $key;
  381. self::$passwords[$lookupKey]['iv'] = $iv;
  382. }
  383. // Read the data size
  384. $data_size = unpack('V', substr($ciphertext,-4) );
  385. // Decrypt
  386. $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  387. mcrypt_generic_init($td, $key, $iv);
  388. $plaintext = mdecrypt_generic($td, substr($ciphertext,0,-4));
  389. mcrypt_generic_deinit($td);
  390. // Trim padding, if necessary
  391. if(strlen($plaintext) > $data_size)
  392. {
  393. $plaintext = substr($plaintext, 0, $data_size);
  394. }
  395. return $plaintext;
  396. }
  397. }