Dcrypt\Aes::encrypt PHP Method

encrypt() public static method

Encrypt plaintext
public static encrypt ( string $plaintext, string $password, integer $cost ) : string
$plaintext string Plaintext string to encrypt.
$password string Password used to encrypt data.
$cost integer Number of HMAC iterations to perform on key
return string
    public static function encrypt($plaintext, $password, $cost = 0)
    {
        // Generate IV of appropriate size.
        $iv = Random::bytes(self::IVSIZE);
        // Derive key from password
        $key = self::key($password, $iv, $cost, self::RIJNDA, self::mode());
        // Encrypt the plaintext
        $message = \openssl_encrypt($plaintext, static::CIPHER, $key, 1, $iv);
        // Create the cypher text prefix (iv + checksum)
        $prefix = $iv . self::checksum($message, $iv, $key, self::RIJNDA, self::mode());
        // Return prefix + cyphertext
        return $prefix . $message;
    }

Usage Example

Example #1
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testEngine()
 {
     $input = 'AAAAAAAA';
     $key = 'AAAAAAAA';
     $encrypted = Aes::encrypt($input, $key);
     $this->assertEquals($input, Aes::decrypt($encrypted, $key));
     // Perform a validation by replacing a random byte to make sure
     // the decryption fails. After enough successful runs,
     // all areas of the cypher text will have been tested
     // for integrity
     $corrupt = self::swaprandbyte($encrypted);
     Aes::decrypt($corrupt, $key);
 }
All Usage Examples Of Dcrypt\Aes::encrypt