public static decrypt ( string $cyphertext, string $password, integer $cost, string $cipher = MCRYPT_RIJNDAEL_128, string $mode = MCRYPT_MODE_CBC, string $algo = 'sha256' ) : string | boolean | ||
$cyphertext | string | Cypher text to decrypt |
$password | string | Password that should be used to decrypt input data |
$cost | integer | Number of HMAC iterations to perform on key |
$cipher | string | Mcrypt cipher |
$mode | string | Mcrypt mode |
$algo | string | Hashing algorithm to use for internal operations |
return | string | boolean | Returns false on checksum validation failure |
public static function decrypt($cyphertext, $password, $cost = 0, $cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC, $algo = 'sha256')
{
// Determine that size of the IV in bytes
$ivsize = \mcrypt_get_iv_size($cipher, $mode);
// Find the IV at the beginning of the cypher text
$iv = Str::substr($cyphertext, 0, $ivsize);
// Gather the checksum portion of the cypher text
$chksum = Str::substr($cyphertext, $ivsize, Str::hashSize($algo));
// Gather message portion of cyphertext after iv and checksum
$message = Str::substr($cyphertext, $ivsize + Str::hashSize($algo));
// Derive key from password
$key = self::key($password, $iv, $cost, $cipher, $mode, $algo);
// Calculate verification checksum
$verify = self::checksum($message, $iv, $key, $cipher, $mode, $algo);
// If checksum could not be verified return false
self::checksumVerify($verify, $chksum);
// Decrypt unpad return
return Pkcs7::unpad(\mcrypt_decrypt($cipher, $key, $message, $mode, $iv));
}
public function testCrossCompatability() { // If PHP 7.1, skip this test if (self::mcryptDeprecated()) { $this->assertTrue(true); return; } $k = 'asdf'; $p = '1234'; $c = Aes::encrypt($p, $k); $this->assertEquals($p, Mcrypt::decrypt($c, $k)); }