decrypt() public static method

Decrypt a string using asymmetric cryptography Wraps SymmetricCrypto::decrypt()
public static decrypt ( string $ciphertext, EncryptionSecretKey $ourPrivateKey, EncryptionPublicKey $theirPublicKey, mixed $encoding = Halite::ENCODE_BASE64URLSAFE ) : HiddenString
$ciphertext string The message to decrypt
$ourPrivateKey EncryptionSecretKey Our private key
$theirPublicKey EncryptionPublicKey Their public key
$encoding mixed Which encoding scheme to use?
return HiddenString The decrypted message
Ejemplo n.º 1
0
 /**
  * @covers Asymmetric::encrypt()
  * @covers Asymmetric::decrypt()
  */
 public function testEncryptFail()
 {
     $alice = KeyFactory::generateEncryptionKeyPair();
     $bob = KeyFactory::generateEncryptionKeyPair();
     $message = Asymmetric::encrypt('test message', $alice->getSecretKey(), $bob->getPublicKey(), true);
     $r = \Sodium\randombytes_uniform(\mb_strlen($message, '8bit'));
     $amt = \Sodium\randombytes_uniform(8);
     $message[$r] = \chr(\ord($message[$r]) ^ 1 << $amt);
     try {
         $plain = Asymmetric::decrypt($message, $bob->getSecretKey(), $alice->getPublicKey(), true);
         $this->assertEquals($plain, $message);
         $this->fail('This should have thrown an InvalidMessage exception!');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }
Ejemplo n.º 2
0
 public function testEncryptFail()
 {
     $alice = Asymmetric::generateKeys();
     $bob = Asymmetric::generateKeys();
     $message = Asymmetric::encrypt('test message', $alice->getSecretKey(), $bob->getPublicKey(), true);
     $r = \Sodium\randombytes_uniform(\mb_strlen($message, '8bit'));
     $amt = \Sodium\randombytes_uniform(8);
     $message[$r] = \chr(\ord($message[$r]) ^ 1 << $amt);
     try {
         $plain = Asymmetric::decrypt($message, $bob->getSecretKey(), $alice->getPublicKey(), true);
         $this->assertEquals($plain, $message);
         throw new \Exception('ERROR: THIS SHOULD ALWAYS FAIL');
     } catch (CryptoException\InvalidMessage $e) {
         $this->assertTrue($e instanceof CryptoException\InvalidMessage);
     }
 }