Encrypt a message with a target users' public key
public static seal ( HiddenString $plaintext, |
||
$plaintext | HiddenString | Message to encrypt |
$publicKey | Public encryption key | |
$encoding | mixed | Which encoding scheme to use? |
return | string | Ciphertext |
public function testSealFail() { $alice = KeyPair::generate(); $message = 'This is for your eyes only'; $sealed = Asymmetric::seal($message, $alice->getPublicKey(), true); // Let's flip one bit, randomly: $r = \Sodium\randombytes_uniform(\mb_strlen($sealed, '8bit')); $amt = 1 << \Sodium\randombytes_uniform(8); $sealed[$r] = \chr(\ord($sealed[$r]) ^ $amt); // This should throw an exception try { $opened = Asymmetric::unseal($sealed, $alice->getSecretKey(), true); $this->assertEquals($opened, $message); throw new Exception('ERROR: THIS SHOULD ALWAYS FAIL'); } catch (CryptoException\InvalidKey $e) { $this->assertTrue($e instanceof CryptoException\InvalidKey); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }
public function testSealFail() { if (\Sodium\library_version_major() < 7 || \Sodium\library_version_major() == 7 && \Sodium\library_version_minor() < 5) { $this->markTestSkipped("Your version of libsodium is too old"); } $alice = KeyFactory::generateEncryptionKeyPair(); $message = 'This is for your eyes only'; $sealed = Asymmetric::seal($message, $alice->getPublicKey(), true); // Let's flip one bit, randomly: $r = \Sodium\randombytes_uniform(\mb_strlen($sealed, '8bit')); $amt = 1 << \Sodium\randombytes_uniform(8); $sealed[$r] = \chr(\ord($sealed[$r]) ^ $amt); // This should throw an exception try { $opened = Asymmetric::unseal($sealed, $alice->getSecretKey(), true); $this->assertEquals($opened, $message); $this->fail('This should have thrown an InvalidMessage exception!'); } catch (CryptoException\InvalidKey $e) { $this->assertTrue($e instanceof CryptoException\InvalidKey); } catch (CryptoException\InvalidMessage $e) { $this->assertTrue($e instanceof CryptoException\InvalidMessage); } }