<?php var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_CBC)); var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_ECB)); var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_STREAM)); var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_NOFB)); var_dump(mcrypt_module_is_block_mode(MCRYPT_MODE_OFB));
/** * This method decrypts ciphertext into plaintext. * * @param \PSL\CipherText $cipherTextObject * @param binary $key * @return binary */ public static function decrypt(CipherText $cipherTextObject, $key) { // TODO: Accept JSON encoded string? // The decryption. $cipher = $cipherTextObject->cipher; $operationMode = $cipherTextObject->operationMode; $cipherText = $cipherTextObject->cipherText; $iv = $cipherTextObject->iv; // Make sure we have everything. if ($iv === null || $cipher === null || $operationMode === null || $cipherText === null) { throw new Exception("Could not decrypt because one or more details of these were missing: cipher, block mode, cipher text, iv. Please specify all of them."); } // Make sure we can actually decrypt using these details. self::isCipherable($cipher, $key, $operationMode, true); if (($plainText = (string) @mcrypt_decrypt($cipher, $key, $cipherText, $operationMode, $iv)) == false) { throw new Exception("Could not decrypt with a cipher '{$cipher}' using a block mode '{$operationMode}'."); } // Handling the padding for block cipher modes such as CBC and ECB. if (@mcrypt_module_is_block_mode($operationMode) === true) { $textSize = strlen($plainText); $amountOfPadding = ord($plainText[$textSize - 1]); $plainText = substr($plainText, 0, $textSize - $amountOfPadding); } return (string) $plainText; }
VERIFY($encrypted !== "This is very important data"); mcrypt_generic_deinit($td); mcrypt_generic_init($td, $key, $iv); $decrypted = mdecrypt_generic($td, $encrypted); mcrypt_generic_end($td); mcrypt_module_close($td); VS($decrypted, "This is very important data"); VERIFY(in_array("blowfish", mcrypt_list_algorithms())); VERIFY(in_array("cbc", mcrypt_list_modes())); VS(mcrypt_module_get_algo_block_size("blowfish"), 8); VS(mcrypt_module_get_algo_key_size("blowfish"), 56); VS(mcrypt_module_get_supported_key_sizes("blowfish"), array()); VS(mcrypt_module_get_supported_key_sizes("twofish"), array(16, 24, 32)); VS(mcrypt_module_is_block_algorithm_mode("cbc"), true); VS(mcrypt_module_is_block_algorithm("blowfish"), true); VS(mcrypt_module_is_block_mode("cbc"), true); VS(mcrypt_module_self_test(MCRYPT_RIJNDAEL_128), true); VS(mcrypt_module_self_test("bogus"), false); $text = "boggles the inivisble monkey will rule the world"; $key = "very secret key"; $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $enc = mcrypt_encrypt(MCRYPT_XTEA, $key, $text, MCRYPT_MODE_ECB, $iv); VS(bin2hex($enc), "f522c62002fa16129c8576bcddc6dd0f7ea81991103ba42962d94c8bfff3ee660d53b187d7e989540abf5a729c2f7baf"); $crypttext = mcrypt_decrypt(MCRYPT_XTEA, $key, $enc, MCRYPT_MODE_ECB, $iv); VS($crypttext, $text); ////////////////////////////////////////////////////////////////////// $key = "123456789012345678901234567890123456789012345678901234567890"; $CC = "4007000000027"; $encrypted = mcrypt_cbc(MCRYPT_RIJNDAEL_128, substr($key, 0, 32), $CC, MCRYPT_ENCRYPT, substr($key, 32, 16)); $decrypted = mcrypt_cbc(MCRYPT_RIJNDAEL_128, substr($key, 0, 32), $encrypted, MCRYPT_DECRYPT, substr($key, 32, 16));