encrypt() public static method

Encrypt a string using asymmetric cryptography Wraps SymmetricCrypto::encrypt()
public static encrypt ( HiddenString $plaintext, EncryptionSecretKey $ourPrivateKey, EncryptionPublicKey $theirPublicKey, mixed $encoding = Halite::ENCODE_BASE64URLSAFE ) : string
$plaintext HiddenString The message to encrypt
$ourPrivateKey EncryptionSecretKey Our private key
$theirPublicKey EncryptionPublicKey Their public key
$encoding mixed Which encoding scheme to use?
return string Ciphertext
Example #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);
     }
 }
Example #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);
     }
 }