Skip to content

Instantly share code, notes, and snippets.

@vietdien2005
Created April 9, 2017 09:29
Show Gist options
  • Save vietdien2005/cc08542af644c0bc9d3a66a0cf187211 to your computer and use it in GitHub Desktop.
Save vietdien2005/cc08542af644c0bc9d3a66a0cf187211 to your computer and use it in GitHub Desktop.
  • Create your public key and private key
// Config RSA
$config = [
    "digest_alg"       => "sha512",
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
];

// Create the private and public key
$res = openssl_pkey_new($config);

// Extract the private key from $res to $privateKey
openssl_pkey_export($res, $privateKey);

// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$publicKey = $pubKey['key'];
  • Encrypt:
openssl_public_encrypt($data, $encrypted, $rsa->publicKey);
  • Decrypt:
openssl_private_decrypt($encrypted, $decrypted, $rsa->privateKey);
  • Put data encrypted through URL:
$url = urlencode(base64_encode($encrypted));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment