decrypt() public static method

Decrypt a file using symmetric-key authenticated encryption.
public static decrypt ( string | resource $input, string | resource $output, EncryptionKey $key ) : boolean
$input string | resource File name or file handle
$output string | resource File name or file handle
$key EncryptionKey Symmetric encryption key
return boolean TRUE if successful
Example #1
0
 public function testEncryptFail()
 {
     \touch(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', 0777);
     \touch(__DIR__ . '/tmp/paragon_avatar.decrypt_fail.png');
     \chmod(__DIR__ . '/tmp/paragon_avatar.decrypt_fail.png', 0777);
     $key = new EncryptionKey(\str_repeat('B', 32));
     File::encrypt(__DIR__ . '/tmp/paragon_avatar.png', __DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', $key);
     $fp = \fopen(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', 'ab');
     \fwrite($fp, \Sodium\randombytes_buf(1));
     fclose($fp);
     try {
         File::decrypt(__DIR__ . '/tmp/paragon_avatar.encrypt_fail.png', __DIR__ . '/tmp/paragon_avatar.decrypt_fail.png', $key);
         $this->fail('Possible authentication bypass in File::decrypt()!');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }
Example #2
0
 /**
  * @covers File::encrypt()
  * @covers File::decrypt()
  */
 public function testEncryptSmallFail()
 {
     $msg = 'File is too small to have been encrypted by Halite.';
     $key = new EncryptionKey(\str_repeat('B', 32));
     \file_put_contents(__DIR__ . '/tmp/empty.encrypted.txt', '');
     try {
         File::decrypt(__DIR__ . '/tmp/empty.encrypted.txt', __DIR__ . '/tmp/empty.decrypted.txt', $key);
         $this->fail("This should scream bloody murder");
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertEquals($e->getMessage(), $msg);
     }
     \file_put_contents(__DIR__ . '/tmp/empty.encrypted.txt', "1A");
     try {
         File::decrypt(__DIR__ . '/tmp/empty.encrypted.txt', __DIR__ . '/tmp/empty.decrypted.txt', $key);
         $this->fail("This should scream bloody murder");
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertEquals($e->getMessage(), $msg);
     }
     \file_put_contents(__DIR__ . '/tmp/empty.encrypted.txt', "1A" . \str_repeat("", 87));
     try {
         File::decrypt(__DIR__ . '/tmp/empty.encrypted.txt', __DIR__ . '/tmp/empty.decrypted.txt', $key);
         $this->fail("This should scream bloody murder");
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertEquals($e->getMessage(), $msg);
     }
     \unlink(__DIR__ . '/tmp/empty.encrypted.txt');
     \unlink(__DIR__ . '/tmp/empty.decrypted.txt');
 }