php - OpenSSL won't create private keys? -
okay, well, first time working encryption on project. using hosting provider ssl, want encrypt portions of database sensitive. this, told use openssl. testing on localhost (wamp), , have installed openssl , turned on php , apache ssl mods. okay, i've been following tutorials and, using several suggested methods, have been able generate public key , store file. reason, can't seem generate private key. post 2 versions of code i've tried:
// generate private key $privatekey = openssl_pkey_new(array( 'private_key_bits' => 1024, 'private_key_type' => openssl_keytype_rsa, )); // write private key file openssl_pkey_export_to_file($privatekey, 'private.key'); // generate public key private key $publickey = openssl_pkey_get_details($privatekey); // write public key file file_put_contents('public.key', $publickey['key']); // clear key echo $privatekey; ?>
this generates public.key file, provides me warnings "openssl_pkey_export_to_file(): cannot key parameter 1:" , " openssl_pkey_get_details() expects parameter 1 resource, boolean."
i tried alternative method:
$config = array( "config" => "e:/wamp/bin/apache/apache2.2.22/conf/openssl.cnf", "digest_alg" => "sha512", "private_key_bits" => 1024, "private_key_type" => openssl_keytype_rsa, ); // create private , public key $res = openssl_pkey_new($config); // extract private key $res $privkey openssl_pkey_export($res, $privkey, null); echo "private key: ".$privkey; // extract public key $res $pubkey $pubkey = openssl_pkey_get_details($res); $pubkey = $pubkey["key"]; echo "public key: ".$pubkey; $data = 'plaintext data goes here'; echo "data: ".$data; // encrypt data $encrypted using public key openssl_public_encrypt($data, $encrypted, $pubkey); echo "encrypted: ".$encrypted; // decrypt data using private key , store results in $decrypted openssl_private_decrypt($encrypted, $decrypted, $privkey); echo "decrypted: ".$decrypted;
this supposed echo everything, unfortunately result blank private key, fine public key, plaintext, , encrypted text, , error when trying decrypt: "openssl_private_decrypt(): key parameter not valid private key"
clearly, i'm having problem private key creation. i've searched internet thoroughly , haven't been able fix it, though i've implemented simple code seems work else.
thanks in advance, elie zeitouni
i think might have easier time phpseclib, pure php rsa implementation. following example create 1024-bit rsa private / public key:
<?php include('crypt/rsa.php'); $rsa = new crypt_rsa(); extract($rsa->createkey()); echo $privatekey . '<br/>' . $publickey; ?>
Comments
Post a Comment