Prado\Security\TSecurityManager::encrypt PHP Метод

encrypt() публичный Метод

Encrypts data with {@link getEncryptionKey EncryptionKey}.
public encrypt ( $data ) : string
Результат string the encrypted data
    public function encrypt($data)
    {
        $module = $this->openCryptModule();
        $key = $this->substr(md5($this->getEncryptionKey()), 0, mcrypt_enc_get_key_size($module));
        srand();
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_RAND);
        mcrypt_generic_init($module, $key, $iv);
        $encrypted = $iv . mcrypt_generic($module, $data);
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
        return $encrypted;
    }

Usage 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));
     }
 }