/** * Decrypt data for teampass db. * * @param string $string * @param string|null $iv * @param string|null $key * * @return bool|string */ public function decrypt($string, $iv = null, $key = null) { if (empty($key)) { $key = $this->defaultSalt; } if ($key != $this->defaultSalt) { if (strlen($key) < 16) { for ($x = strlen($key) + 1; $x <= 16; ++$x) { $key .= chr(0); } } elseif (strlen($key) > 16) { $key = substr($key, 16); } } $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC); if (empty($iv)) { return ''; } $string = hex2bin(trim($string)); $iv = hex2bin($iv); $crypt->IV($iv); $decrypt = $crypt->decrypt($string); return str_replace(chr(0), '', $decrypt); }
*/ error_reporting(E_ALL | E_STRICT); include dirname(__FILE__) . "/../phpCrypt.php"; use PHP_Crypt\PHP_Crypt; /* * The example below uses Linux/Unix /dev/urandom to create a random * string of bytes. Because AES-128 uses a 128 bit (16 byte) key, we * request 16 bytes in the second parameter. Please read above for the * different constants used to create a key. * * WE CAN USE THE FOLLOWING METHODS OF CREATING AN KEY: * $key = PHP_Crypt::createKey(PHP_Crypt::RAND); // The default, uses PHP's mt_rand() * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_RAND); // unix only, uses /dev/random * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_URAND);// unix only, uses /dev/urandom * $key = PHP_Crypt::createKey(PHP_Crypt::RAND_WIN_COM); // Windows only, uses the com_dotnet extension */ $key = PHP_Crypt::createKey(PHP_Crypt::RAND_DEV_URAND, 16); $text = "This is my secret message."; // now create the phpCrypt object and set the cipher to AES-128, with CTR mode $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CTR); $iv = $crypt->createIV(); $encrypt = $crypt->encrypt($text); $crypt->IV($iv); $decrypt = $crypt->decrypt($encrypt); print "CIPHER: " . $crypt->cipherName() . "\n"; print "MODE: " . $crypt->modeName() . "\n"; print "PLAIN TEXT: {$text}\n"; print "PLAIN TEXT HEX: " . bin2hex($text) . "\n"; print "ENCRYPTED HEX: " . bin2hex($encrypt) . "\n"; print "DECRYPTED: {$decrypt}\n"; print "DECRYPTED HEX: " . bin2hex($decrypt) . "\n";
function cryption($string, $key, $iv, $type) { // manage key origin if (empty($key)) { $key = SALT; } if ($key != SALT) { // check key (AES-128 requires a 16 bytes length key) if (strlen($key) < 16) { for ($x = strlen($key) + 1; $x <= 16; $x++) { $key .= chr(0); } } else { if (strlen($key) > 16) { $key = substr($key, 16); } } } // load crypt $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC); if ($type == "encrypt") { // generate IV and encrypt $iv = $crypt->createIV(); $encrypt = $crypt->encrypt($string); // return return array("string" => bin2hex($encrypt), "iv" => bin2hex($iv)); } else { if ($type == "decrypt") { if (empty($iv)) { return ""; } $string = hex2bin(trim($string)); $iv = hex2bin($iv); // load IV $crypt->IV($iv); // decrypt $decrypt = $crypt->decrypt($string); // return return str_replace(chr(0), "", $decrypt); } } }
/** * Decrypt data for teampass db. * * @param string $encrypted * @param string $iv * @param string $key * * @return bool|string */ public function decrypt($encrypted, $iv, $key = null) { $salt = null === $key ? $this->defaultSalt : $key; // manage key origin if (empty($key)) { $key = $salt; } if ($key != $salt) { // check key (AES-128 requires a 16 bytes length key) if (strlen($key) < 16) { for ($x = strlen($key) + 1; $x <= 16; $x++) { $key .= chr(0); } } else { if (strlen($key) > 16) { $key = substr($key, 16); } } } // load crypt $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC); if (empty($iv)) { return false; } $string = hex2bin($encrypted); $iv = hex2bin($iv); // load IV $crypt->IV($iv); // decrypt $decrypt = $crypt->decrypt($string); // return return str_replace(chr(0), "", $decrypt); }
private function decrypt($encrypted_password, $iv, $salt, $master_password) { // hash the master password $master_password = $this->keygen_s2k($master_password, $salt, 32); // decrypt the password with the hashed master password $crypt = new PHP_Crypt($master_password, PHP_Crypt::CIPHER_AES_256, PHP_Crypt::MODE_CTR); $crypt->IV($iv); $decrypted_password = $crypt->decrypt($encrypted_password); return $decrypted_password; }
// PHPCRYPT SETUP $phpcrypt = new PHP_Crypt($key, $phpcrypt_cipher, $phpcrypt_mode); print "MCRYPT: {$mcrypt_cipher} - {$mcrypt_mode}\n"; print "PHPCRYPT: " . $phpcrypt->cipherName() . " - " . $phpcrypt->modeName() . "\n\n"; /** * ENCRYPT USING mCrypt * DECRYPT USING phpCrypt */ // MCRYPT: ENCRYPT mcrypt_generic_init($td, $key, $iv); $ts_start = microtime(true); $encrypt = mcrypt_generic($td, $text); $m_time = number_format(microtime(true) - $ts_start, 5); mcrypt_generic_deinit($td); // PHPCRYPT: DECRYPT $phpcrypt->IV($iv); $ts_start = microtime(true); $decrypt = $phpcrypt->decrypt($encrypt); $p_time = number_format(microtime(true) - $ts_start, 5); // OUTPUT print "MCRYPT ENCRYPTED (HEX): " . bin2hex($encrypt) . " (length=" . strlen($encrypt) . ", time={$m_time})\n"; print "PHPCRYPT DECRYPTED: {$decrypt} (length=" . strlen($decrypt) . ", time={$p_time})\n"; print "PHPCRYPT DECRYPTED (HEX): " . bin2hex($decrypt) . "\n"; print "\n\n"; /** * ENCRYPT USING phpCrypt * DECRYPT USING mCrypt */ // PHPCRYPT: ENCRYPT $phpcrypt->IV($iv); $ts_start = microtime(true);
function cryption_phpCrypt($string, $key, $iv, $type) { // manage key origin if (empty($key)) { $key = SALT; } if ($key != SALT) { // check key (AES-128 requires a 16 bytes length key) if (strlen($key) < 16) { for ($x = strlen($key) + 1; $x <= 16; $x++) { $key .= chr(0); } } else { if (strlen($key) > 16) { $key = substr($key, 16); } } } // load crypt $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_AES_128, PHP_Crypt::MODE_CBC); if ($type == "encrypt") { // generate IV and encrypt $iv = $crypt->createIV(); $encrypt = $crypt->encrypt($string); // return return array("string" => bin2hex($encrypt), "iv" => bin2hex($iv), "error" => empty($encrypt) ? "ERR_ENCRYPTION_NOT_CORRECT" : ""); } else { if ($type == "decrypt") { // case if IV is empty if (empty($iv)) { return array('string' => "", 'error' => "ERR_ENCRYPTION_NOT_CORRECT"); } // convert try { $string = testHex2Bin(trim($string)); $iv = testHex2Bin($iv); } catch (Exception $e) { // error - $e->getMessage(); return array('string' => "", 'error' => "ERR_ENCRYPTION_NOT_CORRECT"); } // load IV $crypt->IV($iv); // decrypt $decrypt = $crypt->decrypt($string); // return //return str_replace(chr(0), "", $decrypt); return array('string' => str_replace(chr(0), "", $decrypt), 'error' => ""); } } }