ParagonIE\EasyRSA\EasyRSA::encrypt PHP Method

encrypt() public static method

Encrypt a message with defuse/php-encryption, using an ephemeral key, then encrypt the key with RSA.
public static encrypt ( string $plaintext, PublicKey $rsaPublicKey ) : string
$plaintext string
$rsaPublicKey PublicKey
return string
    public static function encrypt($plaintext, PublicKey $rsaPublicKey)
    {
        // Random encryption key
        $ephemeral = Key::createNewRandomKey();
        // Encrypt the actual message
        $symmetric = Base64::encode(Crypto::encrypt($plaintext, $ephemeral, true));
        // Use RSA to encrypt the encryption key
        $storeKey = \base64_encode(self::rsaEncrypt($ephemeral->saveToAsciiSafeString(), $rsaPublicKey));
        $packaged = \implode(self::SEPARATOR, array(self::VERSION_TAG, $storeKey, $symmetric));
        $checksum = \substr(\hash('sha256', $packaged), 0, 16);
        // Return the ciphertext
        return $packaged . self::SEPARATOR . $checksum;
    }

Usage Example

Beispiel #1
0
 public function testFailure()
 {
     try {
         KeyPair::generateKeyPair(1024);
         $this->fail('Accepts too small of a key size!');
         return;
     } catch (\Exception $ex) {
         $keyPair = KeyPair::generateKeyPair(2048);
     }
     $secretKey = $keyPair->getPrivateKey();
     $publicKey = $keyPair->getPublicKey();
     $plain = 'Short Message';
     $encrypt = EasyRSA::encrypt($plain, $publicKey);
     $dissect = explode('$', $encrypt);
     // Flip a bit in the key, randomly!
     $dissect[1] = base64_decode($dissect[1]);
     $l = mt_rand(0, strlen($dissect[1]) - 1);
     $dissect[1][$l] = \chr(\ord($dissect[1][$l]) ^ 1 << mt_rand(0, 7));
     $dissect[1] = base64_encode($dissect[1]);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('Checksum collision or logic error.');
         return;
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidChecksumException', $ex);
     }
     $dissect[3] = substr(hash('sha256', implode('$', array_slice($dissect, 0, 3))), 0, 16);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('This should not have passed.');
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidCiphertextException', $ex);
     }
     ///////////////////////////////////////////////////////////////////////
     $dissect = explode('$', $encrypt);
     // Flip a bit in the message, randomly!
     $dissect[2] = base64_decode($dissect[2]);
     $l = mt_rand(0, strlen($dissect[2]) - 1);
     $dissect[2][$l] = \chr(\ord($dissect[2][$l]) ^ 1 << mt_rand(0, 7));
     $dissect[2] = Base64::encode($dissect[2]);
     try {
         $dummy = EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('Checksum collision or logic error.');
         unset($dummy);
         return;
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\ParagonIE\\EasyRSA\\Exception\\InvalidChecksumException', $ex);
     }
     $dissect[3] = substr(hash('sha256', implode('$', array_slice($dissect, 0, 3))), 0, 16);
     try {
         EasyRSA::decrypt(implode('$', $dissect), $secretKey);
         $this->fail('This should not have passed.');
     } catch (\Exception $ex) {
         $this->assertInstanceOf('\\Defuse\\Crypto\\Exception\\WrongKeyOrModifiedCiphertextException', $ex);
     }
 }