Decrypts a ciphertext into a plaintext string using AES
public decryptString ( string $stringToDecrypt, boolean $base64encoded = true ) : string | ||
$stringToDecrypt | string | The ciphertext to decrypt. The first 16 bytes of the raw string must contain the IV (initialisation vector). |
$base64encoded | boolean | Should I Base64-decode the data before decryption? |
Результат | string | The plain text string |
/** * @covers FOF30\Encrypt\Aes * * @return void */ public function testCryptProcessEcb() { if (function_exists('mcrypt_module_open')) { $this->aes = new Aes('The quick brown fox jumped over the lazy dog', 256, 'ecb'); // Regular string $str = 'THATISINSANE'; $es = $this->aes->encryptString($str, true); $ds = $this->aes->decryptString($es, true); $ds = rtrim($ds, ""); $this->assertNotEquals($str, $es); $this->assertEquals($str, $ds); // UTF-8 data $str = 'Χρησιμοποιώντας μη λατινικούς χαρακτήρες'; $es = $this->aes->encryptString($str, false); $ds = $this->aes->decryptString($es, false); $ds = rtrim($ds, ""); $this->assertNotEquals($str, $es); $this->assertEquals($str, $ds); // Using an odd sized keystring (using sha256 to convert it to a key) $this->aes = new Aes('The quick brown fox jumped over the lazy dog'); $str = 'This is some very secret stuff that you are not supposed to transmit in clear text'; $es = $this->aes->encryptString($str, true); $ds = $this->aes->decryptString($es, true); $ds = rtrim($ds, ""); $this->assertNotEquals($str, $es); $this->assertEquals($str, $ds); } else { $this->markTestSkipped('mcrypt is not supported on this system'); } }
/** * Decrypts a transparent authentication message using a TOTP * * @param string $encryptedData The encrypted data * * @return array The decrypted data */ private function decryptWithTOTP($encryptedData) { if (empty($this->totpKey)) { $this->cryptoKey = null; return null; } $totp = new Totp($this->timeStep); $period = $totp->getPeriod(); $period--; for ($i = 0; $i <= 2; $i++) { $time = ($period + $i) * $this->timeStep; $otp = $totp->getCode($this->totpKey, $time); $this->cryptoKey = hash('sha256', $this->totpKey . $otp); $aes = new Aes($this->cryptoKey); try { $ret = $aes->decryptString($encryptedData); } catch (\Exception $e) { continue; } $ret = rtrim($ret, ""); $ret = json_decode($ret, true); if (!is_array($ret)) { continue; } if (!array_key_exists('username', $ret)) { continue; } if (!array_key_exists('password', $ret)) { continue; } // Successful decryption! return $ret; } // Obviously if we're here we could not decrypt anything. Bail out. $this->cryptoKey = null; return null; }