当我保存私钥(不是公钥时,当公钥保存到文件时-很好!)将文件解密的结果为假。
function generate_keys() {
$config = array(
"config" => "C:/xampp/php/extras/openssl/openssl.cnf",
"private_key_type"=>OPENSSL_KEYTYPE_RSA,
"private_key_bits"=>512,
);
$res = openssl_pkey_new($config);
$privKey = '';
openssl_pkey_export($res, $privKey, 'test', $config);
$fpr = fopen("private.txt", "w");
fwrite($fpr, $privKey);
fclose($fpr);
$arr = array(
"countryName" => "UA",
"stateOrProvinceName" => "Kievskaya Oblast",
"localityName" => "Kiev",
"organizationName" => "Organization",
"organizationalUnitName" => "Soft",
"commonName" => "localhost",
"emailAddress" => "ilovesssl@php.net"
);
$csr = openssl_csr_new($arr, $privKey, $config);
$cert = openssl_csr_sign($csr, NULL, $privKey, 365, $config);
openssl_x509_export($cert, $str_cert);
$public_key = openssl_pkey_get_public($str_cert);
$public_key_details = openssl_pkey_get_details($public_key);
$public_key_string = $public_key_details['key'];
$fpr1 = fopen("public.txt","w");
fwrite($fpr1, $public_key_string);
fclose($fpr1);
return array('private'=>$privKey,'public'=>$public_key_string);
}
$keys = generate_keys(); // if get private key from here - works fine!
$path = "public.txt";
$fpr = fopen($path,"r");
$pub_key = fread($fpr,1024);
fclose($fpr);
openssl_public_encrypt('test string', $passcrypt, $pub_key);
}
$path = "private.txt";
$fpr = fopen($path,"r");
$pr_key = fread($fpr, 1024); // if i get key from file result is false!
fclose($fpr);
openssl_private_decrypt($passcrypt, $decrypted, openssl_pkey_get_private($pr_key, 'test'));
echo var_dump($decrypted == "test string");
当我将私钥保存为变量时,结果为true,但是当我将其保存至文件然后从文件中获取时,结果为false。