Skip to content

Instantly share code, notes, and snippets.

@pmeulen
pmeulen / public_encrypt_decrypt.php
Last active November 17, 2025 21:17
Functions for encrypting and decrypting data with RSA public key crypto using the PHP openssl_* functions.
<?php
/**
* Encrypt $plaintext using $rsa_public_key
*
* To decrypt the data the RSA private that corresponds to the $rsa_public_key is required.
*
* Because public key crypto is not suitable for arbitrary length data, encryption is done in two steps
* 1. Generate a random symmetric key and encrypt the plaintext with that key