Last active
January 26, 2018 10:38
-
-
Save LivesInSpb/c49218908c4fcee4acb07595f192bbe0 to your computer and use it in GitHub Desktop.
create rsa keys & encode/decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = 'XCC58-AYA68-75ZUU-19TDZ'; | |
$config = array( | |
'digest_alg' => 'sha512', | |
'private_key_bits' => 4096, | |
'private_key_type' => OPENSSL_KEYTYPE_RSA, | |
); | |
// Create the private and public key | |
$res = openssl_pkey_new($config); | |
// Extract the private key from $res to $privKey | |
openssl_pkey_export($res, $privKey); | |
echo $pubKey; | |
echo $privKey; | |
// Extract the public key from $res to $pubKey | |
$pubKey = openssl_pkey_get_details($res); | |
$pubKey = $pubKey['key']; | |
// Encrypt the data to $encrypted using the public key | |
openssl_public_encrypt($data, $encrypted, $pubKey); | |
echo chunk_split(base64_encode($encrypted)); | |
// Decrypt the data using the private key and store the results in $decrypted | |
openssl_private_decrypt($encrypted, $decrypted, $privKey); | |
echo $decrypted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create public & private keys with length 4096 bits