Skip to content

Instantly share code, notes, and snippets.

@paragonie-scott
Created December 12, 2016 04:50
Show Gist options
  • Save paragonie-scott/1067c60e212bc755be673501b46e268c to your computer and use it in GitHub Desktop.
Save paragonie-scott/1067c60e212bc755be673501b46e268c to your computer and use it in GitHub Desktop.
RSA Limits Demo
<?php
$publicKey = openssl_pkey_get_public('
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7o9A47JuO3wgZ/lbOIOs
Xc6cVSiCMsrglvORM/54StFRvcrxMi7OjXD6FX5fQpUOQYZfIOFZZMs6kmNXk8xO
hgTmdMJcBWolQ85acfAdWpTpCW29YMvXNARUDb8uJKAApsISnttyCnbvp7zYMdQm
HiTG/+bYaegSXzV3YN+Ej+ZcocubUpLp8Rpzz+xmXep3BrjBycAE9z2IrrV2rlwg
TTxU/B8xmvMsToBQpAbe+Cv130tEHsyW4UL9KZY1M9R+UHFPPmORjBKxSZvjJ1mS
UbUYN6PmMry35wCaFCfQoyTDUxBfxTGYqjaveQv4sxx0uvoiLXHt9cAm5Q8KJ+8d
FwIDAQAB
-----END PUBLIC KEY-----
');
$privateKey = openssl_pkey_get_private('
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA7o9A47JuO3wgZ/lbOIOsXc6cVSiCMsrglvORM/54StFRvcrx
Mi7OjXD6FX5fQpUOQYZfIOFZZMs6kmNXk8xOhgTmdMJcBWolQ85acfAdWpTpCW29
YMvXNARUDb8uJKAApsISnttyCnbvp7zYMdQmHiTG/+bYaegSXzV3YN+Ej+Zcocub
UpLp8Rpzz+xmXep3BrjBycAE9z2IrrV2rlwgTTxU/B8xmvMsToBQpAbe+Cv130tE
HsyW4UL9KZY1M9R+UHFPPmORjBKxSZvjJ1mSUbUYN6PmMry35wCaFCfQoyTDUxBf
xTGYqjaveQv4sxx0uvoiLXHt9cAm5Q8KJ+8dFwIDAQABAoIBAHkWS3iHy/3zjjtY
TV4NL8NZqO5spl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+iQze66XSRp5JL
oGZrU7JzCxuyoeA/4z36UN5WXmeS3bqh6SinrPQKt7rMkK1NQYcDUijPBMt0afO+
LH0HIC1HAtS6Wztd2Taoqwe5Xm75YW0elo4OEqiAfubAC85Ec4zfxw==
-----END RSA PRIVATE KEY-----
');
for ($size = 213; $size < 217; ++$size) {
$message = random_bytes($size);
$ciphertext = '';
$plaintext = '';
openssl_public_encrypt($message, $ciphertext, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
openssl_private_decrypt($ciphertext, $plaintext, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);
var_dump([$size => $message === $plaintext]);
}
@paragonie-scott
Copy link
Author

paragonie-scott commented Dec 12, 2016

DEMO HERE (courtesy 3v4l.org)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment