Prado\Security\TSecurityManager::decrypt PHP Method

decrypt() public method

Decrypts data with {@link getEncryptionKey EncryptionKey}.
public decrypt ( $data ) : string
return string the decrypted data
    public function decrypt($data)
    {
        $module = $this->openCryptModule();
        $key = $this->substr(md5($this->getEncryptionKey()), 0, mcrypt_enc_get_key_size($module));
        $ivSize = mcrypt_enc_get_iv_size($module);
        $iv = $this->substr($data, 0, $ivSize);
        mcrypt_generic_init($module, $key, $iv);
        $decrypted = mdecrypt_generic($module, $this->substr($data, $ivSize, $this->strlen($data)));
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        return $decrypted;
    }

Usage Example

Example #1
0
 public function testEncryptDecrypt()
 {
     $sec = new TSecurityManager();
     $sec->init(null);
     // loop through different string size
     $testText = md5('a text (not) full of entrophy');
     for ($i = 1; $i < strlen($testText); $i++) {
         $sec->setEncryptionKey('aKey');
         $plainText = substr($testText, 0, $i);
         try {
             $encrypted = $sec->encrypt($plainText);
         } catch (TNotSupportedException $e) {
             self::markTestSkipped('mcrypt extension not loaded');
             return;
         }
         $decrypted = $sec->decrypt($encrypted);
         // the decrypted string is padded with \0
         $decrypted = strstr($decrypted, "", TRUE);
         self::assertEquals($plainText, $decrypted);
         // try change key
         $sec->setEncryptionKey('anotherKey');
         self::assertNotEquals($plainText, $sec->decrypt($encrypted));
     }
 }