6 && $i % $Nk == 4) { $temp = self::SubWord($temp); } for ($t = 0; $t < 4; ++$t) { $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; } } return $w; } protected static function SubWord($w) { // apply SBox to 4-byte word w for ($i = 0; $i < 4; ++$i) { $w[$i] = self::$Sbox[$w[$i]]; } return $w; } protected static function RotWord($w) { // rotate 4-byte word w left by one byte $tmp = $w[0]; for ($i = 0; $i < 3; ++$i) { $w[$i] = $w[$i + 1]; } $w[3] = $tmp; return $w; } /* * Unsigned right shift function, since PHP has neither >>> operator nor unsigned ints * * @param a number to be shifted (32-bit integer) * @param b number of bits to shift a to the right (0..31) * @return a right-shifted and zero-filled by b bits */ protected static function urs($a, $b) { $a &= 0xffffffff; $b &= 0x1f; // (bounds check) if ($a & 0x80000000 && $b > 0) { // if left-most bit set $a = ($a >> 1) & 0x7fffffff; // right-shift one bit & clear left-most bit $a = $a >> ($b - 1); // remaining right-shifts } else { // otherwise $a = ($a >> $b); // use normal right-shift } return $a; } /** * Encrypt a text using AES encryption in Counter mode of operation * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf. * * Unicode multi-byte character safe * * @param plaintext source text to be encrypted * @param password the password to use to generate a key * @param nBits number of bits to be used in the key (128, 192, or 256) * * @return encrypted text */ public static function AESEncryptCtr($plaintext, $password, $nBits) { $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { return ''; } // standard allows 128/192/256 bit keys // note PHP (5) gives us plaintext and password in UTF8 encoding! // use AES itself to encrypt password to get cipher key (using plain password as source for // key expansion) - gives us well encrypted key $nBytes = $nBits / 8; // no bytes in key $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long // initialise counter block (NIST SP800-38A �B.2): millisecond time-stamp for nonce in // 1st 8 bytes, block counter in 2nd 8 bytes $counterBlock = array(); $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 $nonceSec = floor($nonce / 1000); $nonceMs = $nonce % 1000; // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes for ($i = 0; $i < 4; ++$i) { $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; } for ($i = 0; $i < 4; ++$i) { $counterBlock[$i + 4] = $nonceMs & 0xff; } // and convert it to a string to go on the front of the ciphertext $ctrTxt = ''; for ($i = 0; $i < 8; ++$i) { $ctrTxt .= chr($counterBlock[$i]); } // generate key schedule - an expansion of the key into distinct Key Rounds for each round $keySchedule = self::KeyExpansion($key); $blockCount = ceil(strlen($plaintext) / $blockSize); $ciphertxt = array(); // ciphertext as array of strings for ($b = 0; $b < $blockCount; ++$b) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) for ($c = 0; $c < 4; ++$c) { $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; } for ($c = 0; $c < 4; ++$c) { $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); } $cipherCntr = self::Cipher($counterBlock, $keySchedule); // -- encrypt counter block -- // block size is reduced on final block $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; $cipherByte = array(); for ($i = 0; $i < $blockLength; ++$i) { // -- xor plaintext with ciphered counter byte-by-byte -- $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); $cipherByte[$i] = chr($cipherByte[$i]); } $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext } // implode is more efficient than repeated string concatenation $ciphertext = $ctrTxt.implode('', $ciphertxt); $ciphertext = base64_encode($ciphertext); return $ciphertext; } /** * Decrypt a text encrypted by AES in counter mode of operation. * * @param ciphertext source text to be decrypted * @param password the password to use to generate a key * @param nBits number of bits to be used in the key (128, 192, or 256) * * @return decrypted text */ public static function AESDecryptCtr($ciphertext, $password, $nBits) { $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { return ''; } // standard allows 128/192/256 bit keys $ciphertext = base64_decode($ciphertext); // use AES to encrypt password (mirroring encrypt routine) $nBytes = $nBits / 8; // no bytes in key $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long // recover nonce from 1st element of ciphertext $counterBlock = array(); $ctrTxt = substr($ciphertext, 0, 8); for ($i = 0; $i < 8; ++$i) { $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); } // generate key schedule $keySchedule = self::KeyExpansion($key); // separate ciphertext into blocks (skipping past initial 8 bytes) $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); $ct = array(); for ($b = 0; $b < $nBlocks; ++$b) { $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); } $ciphertext = $ct; // ciphertext is now array of block-length strings // plaintext will get generated block-by-block into array of block-length strings $plaintxt = array(); for ($b = 0; $b < $nBlocks; ++$b) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) for ($c = 0; $c < 4; ++$c) { $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; } for ($c = 0; $c < 4; ++$c) { $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; } $cipherCntr = self::Cipher($counterBlock, $keySchedule); // encrypt counter block $plaintxtByte = array(); for ($i = 0; $i < strlen($ciphertext[$b]); ++$i) { // -- xor plaintext with ciphered counter byte-by-byte -- $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); $plaintxtByte[$i] = chr($plaintxtByte[$i]); } $plaintxt[$b] = implode('', $plaintxtByte); } // join array of blocks into single plaintext string $plaintext = implode('', $plaintxt); return $plaintext; } /** * AES encryption in CBC mode. This is the standard mode (the CTR methods * actually use Rijndael-128 in CTR mode, which - technically - isn't AES). * The data length is tucked as a 32-bit unsigned integer (little endian) * after the ciphertext. It supports AES-128, AES-192 and AES-256. * * @since 3.0.1 * * @author Nicholas K. Dionysopoulos * * @param string $plaintext The data to encrypt * @param string $password Encryption password * @param int $nBits Encryption key size. Can be 128, 192 or 256 * * @return string The ciphertext */ public static function AESEncryptCBC($plaintext, $password, $nBits = 128) { if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { return false; } // standard allows 128/192/256 bit keys if (!function_exists('mcrypt_module_open')) { return false; } // Try to fetch cached key/iv or create them if they do not exist $lookupKey = $password.'-'.$nBits; if (array_key_exists($lookupKey, self::$passwords)) { $key = self::$passwords[$lookupKey]['key']; $iv = self::$passwords[$lookupKey]['iv']; } else { // use AES itself to encrypt password to get cipher key (using plain password as source for // key expansion) - gives us well encrypted key $nBytes = $nBits / 8; // no bytes in key $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long $newKey = ''; foreach ($key as $int) { $newKey .= chr($int); } $key = $newKey; // Create an Initialization Vector (IV) based on the password, using the same technique as for the key $nBytes = 16; // AES uses a 128 -bit (16 byte) block size, hence the IV size is always 16 bytes $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $iv = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $newIV = ''; foreach ($iv as $int) { $newIV .= chr($int); } $iv = $newIV; self::$passwords[$lookupKey]['key'] = $key; self::$passwords[$lookupKey]['iv'] = $iv; } $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($td, $key, $iv); $ciphertext = mcrypt_generic($td, $plaintext); mcrypt_generic_deinit($td); $ciphertext .= pack('V', strlen($plaintext)); return $ciphertext; } /** * AES decryption in CBC mode. This is the standard mode (the CTR methods * actually use Rijndael-128 in CTR mode, which - technically - isn't AES). * * Supports AES-128, AES-192 and AES-256. It supposes that the last 4 bytes * contained a little-endian unsigned long integer representing the unpadded * data length. * * @since 3.0.1 * * @author Nicholas K. Dionysopoulos * * @param string $ciphertext The data to encrypt * @param string $password Encryption password * @param int $nBits Encryption key size. Can be 128, 192 or 256 * * @return string The plaintext */ public static function AESDecryptCBC($ciphertext, $password, $nBits = 128) { if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { return false; } // standard allows 128/192/256 bit keys if (!function_exists('mcrypt_module_open')) { return false; } // Try to fetch cached key/iv or create them if they do not exist $lookupKey = $password.'-'.$nBits; if (array_key_exists($lookupKey, self::$passwords)) { $key = self::$passwords[$lookupKey]['key']; $iv = self::$passwords[$lookupKey]['iv']; } else { // use AES itself to encrypt password to get cipher key (using plain password as source for // key expansion) - gives us well encrypted key $nBytes = $nBits / 8; // no bytes in key $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $key = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long $newKey = ''; foreach ($key as $int) { $newKey .= chr($int); } $key = $newKey; // Create an Initialization Vector (IV) based on the password, using the same technique as for the key $nBytes = 16; // AES uses a 128 -bit (16 byte) block size, hence the IV size is always 16 bytes $pwBytes = array(); for ($i = 0; $i < $nBytes; ++$i) { $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; } $iv = self::Cipher($pwBytes, self::KeyExpansion($pwBytes)); $newIV = ''; foreach ($iv as $int) { $newIV .= chr($int); } $iv = $newIV; self::$passwords[$lookupKey]['key'] = $key; self::$passwords[$lookupKey]['iv'] = $iv; } // Read the data size $data_size = unpack('V', substr($ciphertext, -4)); // Decrypt $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); mcrypt_generic_init($td, $key, $iv); $plaintext = mdecrypt_generic($td, substr($ciphertext, 0, -4)); mcrypt_generic_deinit($td); // Trim padding, if necessary if (strlen($plaintext) > $data_size) { $plaintext = substr($plaintext, 0, $data_size); } return $plaintext; } }