У меня есть этот код для создания публикуемого c RSA 4096 и секретного ключа для шифрования и дешифрования строки.
Код:
<?php
$config = array(
"config" => "C:/xampp/php/extras/openssl/openssl.cnf",
"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);
// Extract the public key from $res to $pubKey
$pubKey = openssl_pkey_get_details($res);
$pubKey = $pubKey["key"];
$data = 'Hello, World!';
// Encrypt the data to $encrypted using the public key
openssl_public_encrypt($data, $encrypted, $pubKey);
echo $encrypted;
// Decrypt the data using the private key and store the results in $decrypted
openssl_private_decrypt($encrypted, $decrypted, $privKey);
echo $decrypted;
?>
Создает ключи, шифрует data
строка (Hello, World!
), но при попытке расшифровать encrypted
строку возникает ошибка:
Предупреждение : openssl_private_decrypt (): параметр ключа не является действительным закрытым ключом в C: \ xampp \ htdocs \ rsa \ index. php в строке 26