<?php
// also see stackoverflow.com/questions/59195251/php-get-private-key-from-a-single-line-private-key
// micmap.org/php-by-example/de/function/openssl_get_publickey
// best stackoverflow.com/questions/15558321/openssl-not-working-on-windows-errors-0x02001003-0x2006d080-0x0e064002
// sandrocirulli.net/how-to-encrypt-and-decrypt-emails-and-files/
echo '<pre>';
function testOpenSSL($openssl_args)
{
$res = openssl_pkey_new($openssl_args); // <-- CONFIG ARRAY
openssl_error_string(); // May throw error even though its working fine!
if (empty($res)) {return false;}
$openssl_args['keysnew']=$res;
//var_dump($res);echo '<br><br>';
// Extract the private key from $res to $privKey
openssl_pkey_export($res, $privKey, NULL, $openssl_args); // <-- CONFIG ARRAY
openssl_error_string(); // May throw error even though its working fine!
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
openssl_error_string(); // May throw error even though its working fine!
if ($pubKey === FALSE){return false;}
$pubKey = $pubKey["key"];
// Encrypt the data to $encrypted using the public key
$data = $openssl_args['data'];
$res = openssl_public_encrypt($data, $encrypted, $pubKey);
if ($res === FALSE){return false;}
#var_dump($res);exit; //bool
// Decrypt the data using the private key and store the results in $decrypted
$res = openssl_private_decrypt($encrypted, $decrypted, $privKey);
if ($res === FALSE){return false;}
//return $decrypted;
$openssl_args['pub_key']=$pubKey;
$openssl_args['priv_key']=$privKey;
$openssl_args['encr']=$encrypted;
$openssl_args['decr']=$decrypted;
return $openssl_args;
}
// try sam openssl.cnf , most error on windows xampp
$try= 5;
$openssl_cnf_path = array(
0=>'C:\xampp\apache\conf\openssl.cnf', //11259 bytes
1=>'C:\xampp\apache\bin\openssl.cnf', //11259 bytes
2=>'C:\xampp\php\windowsXamppPhp\extras\ssl\openssl.cnf', //10909
3=>'C:\xampp\php\extras\ssl\openssl.cnf', //10909 byes
4=>'C:\xampp\php\extras\openssl\openssl.cnf', //9374 bytes
5=>'C:\Program Files\Git\usr\ssl\openssl.cnf', //10909 bytes
6=>'C:\Program Files\Git\mingw64\ssl\openssl.cnf', //10909 bytes
);
$data = ' todo in spin activate theme elementor-child plugin qw_casino ... permalinks postname media Lib. add new upload paysave pic make empty page with template-casino in -child make page4todo in spin activate theme elementor-child plugin qw_casin245'; /** ... permalinks postname media Lib. add new upload paysave pic make empty page with template-casino in -child make page6todo in spin activate theme elementor-child plugin qw_casino .. permalinks postname media Lib.add new upload paysave pic make empty501';
/** mist max str len 501 in RFC3447 can operate on messages of length up to k - 11 octets (k is the octet length of the RSA modulus) so if you are using 2048-bit RSA key then maximum length of the plain data to be encrypted is 245 bytes. /***/
echo 'str len : ', strlen($data);
$openssl_args = array(
"config" => $openssl_cnf_path[$try],
"digest_alg" => "sha512",
//"private_key_bits" => 4096,
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
"try_path_ar" => $openssl_cnf_path,
"data" => $data,
);
// Example
$res = testOpenSSL($openssl_args);
if ($res === FALSE)
{
echo "<h4 style='background-color: red;'>Fail</h4>";
} else {
echo '<h3 style=\'background-color: green; color:white;\'>with $try= '.$try.' decrypted ok: '.$res['decr'].'</h3>';
}
//////////////////////////////////////////
//
echo '<br>vardump res <br>'; var_dump($res);
echo '<br>======================<br>';
$privKey=$res['priv_key'];
$pubKey =$res['pub_key'];
file_put_contents('pri_key.txt',$privKey);
file_put_contents('priv_key.txt',$privKey);
file_put_contents('pub_key.txt',$pubKey);
echo '<br><br><pre>';
/* Encrypt the data using the public key
* The encrypted data is stored in $encrypted */
openssl_public_encrypt($data, $encrypted, $pubKey);
echo '<br>public encrypt: '; var_dump( base64_encode($encrypted));
/* Decrypt the data using the private key and store the
* result in $decrypted. */
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo ' private decrypt: ', $decrypted;
// inverse testOpenSSL
openssl_private_encrypt($data, $encrypted, $privKey);
echo '<br><br> invers<br>pri encrypt: '; var_dump( base64_encode($encrypted));
openssl_public_decrypt($encrypted, $decrypted, $pubKey);
echo ' pub decrypt: ', $decrypted , '<br>';